From 5207524381f9f912c13dd50c8accfc8d0a21c165 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Wed, 5 Mar 2025 10:23:13 +0100 Subject: [PATCH] feat: improved logging --- cmd/gogitlabber/git.go | 20 +++++++++++++++++++- cmd/gogitlabber/gitlab.go | 5 +++++ cmd/gogitlabber/main.go | 5 ++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd/gogitlabber/git.go b/cmd/gogitlabber/git.go index e30bc8b..0186556 100644 --- a/cmd/gogitlabber/git.go +++ b/cmd/gogitlabber/git.go @@ -36,6 +36,9 @@ func checkoutRepositories(repositories []Repository, concurrency int) { repoName := string(repo.PathWithNamespace) repoDestination := repoDestinationPre + repoName + // log activity + logPrint("Starting on repository: "+repoName, nil) + // make gitlab url url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName) @@ -43,6 +46,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) { checkRepo := func(repoDestination string) string { checkCmd := exec.Command("git", "-C", repoDestination, "remote", "-v") checkOutput, _ := checkCmd.CombinedOutput() + logPrint("Checking status for repository: "+repoName, nil) return string(checkOutput) } @@ -53,10 +57,14 @@ func checkoutRepositories(repositories []Repository, concurrency int) { switch { case strings.Contains(string(repoStatus), "No such file or directory"): + // log activity + logPrint("Decided to clone repository: "+repoName, nil) + // clone the repo cloneRepository := func(repoDestination string, url string) (string, error) { cloneCmd := exec.Command("git", "clone", url, repoDestination) cloneOutput, err := cloneCmd.CombinedOutput() + logPrint("Cloning repository: "+repoName+" to "+repoDestination, nil) return string(cloneOutput), err } @@ -79,6 +87,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) { // pull the latest case strings.Contains(string(repoStatus), url): + logPrint("Decided to pull repository: "+repoName, nil) pullRepository(repoName, repoDestination) if !verbose { descriptionPrefixPre := "Pulling repository " @@ -88,7 +97,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) { } default: - logPrint("ERROR: decided not to clone or pull repository"+repoName, nil) + logPrint("ERROR: decided not to clone or pull repository: "+repoName, nil) logPrint("ERROR: this is why: "+repoStatus, nil) // set a lock, increment counters and unlock @@ -108,6 +117,9 @@ func checkoutRepositories(repositories []Repository, concurrency int) { func pullRepository(repoName string, repoDestination string) { + // log activity + logPrint("Pulling repository: "+repoName+" at "+repoDestination, nil) + // find remote findRemote := func(repoDestination string) (string, error) { remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show") @@ -116,7 +128,9 @@ func pullRepository(repoName string, repoDestination string) { return "", fmt.Errorf("finding remote: %v\n", err) } + logPrint("Finding remote for repository: "+repoName+" at "+repoDestination, nil) remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0] + logPrint("Found remote; "+remote+" for repository: "+repoName+" at "+repoDestination, nil) return remote, nil } remote, _ := findRemote(repoDestination) @@ -141,9 +155,13 @@ func pullRepository(repoName string, repoDestination string) { switch { case strings.Contains(string(pullOutput), "You have unstaged changes"): pullErrorMsg = append(pullErrorMsg, repoDestination) + logPrint("Found unstaged changes for repository: "+repoName+" at "+repoDestination, nil) default: logPrint("ERROR: pulling "+repoName, nil) } } + + // log activity + logPrint("Pulled repository: "+repoName+" at "+repoDestination, nil) } diff --git a/cmd/gogitlabber/gitlab.go b/cmd/gogitlabber/gitlab.go index 05efa74..9aa417a 100644 --- a/cmd/gogitlabber/gitlab.go +++ b/cmd/gogitlabber/gitlab.go @@ -27,13 +27,16 @@ func fetchRepositoriesGitlab() ([]Repository, error) { url := fmt.Sprintf("https://%s/api/v4/projects?%s&%s&%s%s", gitlabHost, membership, order, perpage, archived) + logPrint("Creating API request", nil) req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, fmt.Errorf("ERROR: creating request: %v\n", err) } + logPrint("Adding PRIVATE-TOKEN header to API request", nil) req.Header.Set("PRIVATE-TOKEN", gitlabToken) + logPrint("Making request", nil) client := &http.Client{} resp, err := client.Do(req) if err != nil { @@ -45,6 +48,7 @@ func fetchRepositoriesGitlab() ([]Repository, error) { return nil, fmt.Errorf("ERROR: API request failed with status: %d\n", resp.StatusCode) } + logPrint("Decoding JSON response", nil) var repositories []Repository if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil { return nil, fmt.Errorf("ERROR: decoding response: %v\n", err) @@ -54,5 +58,6 @@ func fetchRepositoriesGitlab() ([]Repository, error) { return repositories, fmt.Errorf("ERROR: no repositories found\n") } + logPrint("Returning repositories found", nil) return repositories, nil } diff --git a/cmd/gogitlabber/main.go b/cmd/gogitlabber/main.go index 981c04b..5a200d2 100644 --- a/cmd/gogitlabber/main.go +++ b/cmd/gogitlabber/main.go @@ -33,16 +33,15 @@ func main() { // check for git err := verifyGitAvailable() if err != nil { - logFatal("FATAL: git not found in path: %v", err) + logFatal("git not found in path: %v", err) } - logPrint("Git is available. Proceeding with the program.", nil) + logPrint("VALIDATION: git found in path", nil) // fetch repository information from gitlab repositories, err := fetchRepositoriesGitlab() if err != nil { logFatal("FATAL: %v", err) } - logPrint("Logged into GitLab, Repositories found. Proceeding with the program.", nil) // print progressbar ony if not in verbose mode if !verbose {