From 13602ae71c4f6a14e9a0d33682ea902fbefdc876 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Thu, 6 Mar 2025 08:47:02 +0100 Subject: [PATCH] fix: moved logging functions to its own package --- cmd/gogitlabber/git.go | 29 +++++++++++++++-------------- cmd/gogitlabber/gitlab.go | 13 +++++++------ cmd/gogitlabber/input.go | 21 +++++++++++---------- cmd/gogitlabber/logging/logging.go | 23 +++++++++++++++++++++++ cmd/gogitlabber/main.go | 7 ++++--- cmd/gogitlabber/output.go | 26 ++++---------------------- 6 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 cmd/gogitlabber/logging/logging.go diff --git a/cmd/gogitlabber/git.go b/cmd/gogitlabber/git.go index b112ad0..8fbb00b 100644 --- a/cmd/gogitlabber/git.go +++ b/cmd/gogitlabber/git.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "gogitlabber/cmd/gogitlabber/logging" "os/exec" "strings" "sync" @@ -37,7 +38,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) { repoDestination := repoDestinationPre + repoName // log activity - logPrint("Starting on repository: "+repoName, nil) + logging.LogPrint(debug, "Starting on repository: "+repoName, nil) // make gitlab url url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName) @@ -46,7 +47,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) + logging.LogPrint(debug, "Checking status for repository: "+repoName, nil) return string(checkOutput) } @@ -58,19 +59,19 @@ func checkoutRepositories(repositories []Repository, concurrency int) { case strings.Contains(string(repoStatus), "No such file or directory"): // log activity - logPrint("Decided to clone repository: "+repoName, nil) + logging.LogPrint(debug, "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) + logging.LogPrint(debug, "Cloning repository: "+repoName+" to "+repoDestination, nil) return string(cloneOutput), err } _, err := cloneRepository(repoDestination, url) if err != nil { - logPrint("ERROR: %v\n", err) + logging.LogPrint(debug, "ERROR: %v\n", err) } // set a lock, increment counters, update progressbar and unlock @@ -87,7 +88,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) { // pull the latest case strings.Contains(string(repoStatus), url): - logPrint("Decided to pull repository: "+repoName, nil) + logging.LogPrint(debug, "Decided to pull repository: "+repoName, nil) pullRepository(repoName, repoDestination) if !debug { descriptionPrefixPre := "Pulling repository " @@ -97,8 +98,8 @@ func checkoutRepositories(repositories []Repository, concurrency int) { } default: - logPrint("ERROR: decided not to clone or pull repository: "+repoName, nil) - logPrint("ERROR: this is why: "+repoStatus, nil) + logging.LogPrint(debug, "ERROR: decided not to clone or pull repository: "+repoName, nil) + logging.LogPrint(debug, "ERROR: this is why: "+repoStatus, nil) // set a lock, increment counters and unlock mu.Lock() @@ -118,7 +119,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) { func pullRepository(repoName string, repoDestination string) { // log activity - logPrint("Pulling repository: "+repoName+" at "+repoDestination, nil) + logging.LogPrint(debug, "Pulling repository: "+repoName+" at "+repoDestination, nil) // find remote findRemote := func(repoDestination string) (string, error) { @@ -128,9 +129,9 @@ func pullRepository(repoName string, repoDestination string) { return "", fmt.Errorf("finding remote: %v\n", err) } - logPrint("Finding remote for repository: "+repoName+" at "+repoDestination, nil) + logging.LogPrint(debug, "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) + logging.LogPrint(debug, "Found remote; "+remote+" for repository: "+repoName+" at "+repoDestination, nil) return remote, nil } remote, _ := findRemote(repoDestination) @@ -155,13 +156,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) + logging.LogPrint(debug, "Found unstaged changes for repository: "+repoName+" at "+repoDestination, nil) default: - logPrint("ERROR: pulling "+repoName, nil) + logging.LogPrint(debug, "ERROR: pulling "+repoName, nil) } } // log activity - logPrint("Pulled repository: "+repoName+" at "+repoDestination, nil) + logging.LogPrint(debug, "Pulled repository: "+repoName+" at "+repoDestination, nil) } diff --git a/cmd/gogitlabber/gitlab.go b/cmd/gogitlabber/gitlab.go index 5be7e4d..c3d9d51 100644 --- a/cmd/gogitlabber/gitlab.go +++ b/cmd/gogitlabber/gitlab.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "gogitlabber/cmd/gogitlabber/logging" "net/http" ) @@ -27,16 +28,16 @@ func fetchRepositoriesGitlab() ([]Repository, error) { url := fmt.Sprintf("https://%s/api/v4/projects?%s&%s&%s%s", gitlabHost, membership, order, perpage, archived) - logPrint("HTTP: Creating API request", nil) + logging.LogPrint(debug, "HTTP: 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("HTTP: Adding PRIVATE-TOKEN header to API request", nil) + logging.LogPrint(debug, "HTTP: Adding PRIVATE-TOKEN header to API request", nil) req.Header.Set("PRIVATE-TOKEN", gitlabToken) - logPrint("HTTP: Making request", nil) + logging.LogPrint(debug, "HTTP: Making request", nil) client := &http.Client{} resp, err := client.Do(req) if err != nil { @@ -48,7 +49,7 @@ func fetchRepositoriesGitlab() ([]Repository, error) { return nil, fmt.Errorf("ERROR: API request failed with status: %d\n", resp.StatusCode) } - logPrint("HTTP: Decoding JSON response", nil) + logging.LogPrint(debug, "HTTP: 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) @@ -61,10 +62,10 @@ func fetchRepositoriesGitlab() ([]Repository, error) { repoCount := len(repositories) err = bar.Set(0) if err != nil { - logFatal("Could not reset the progressbar", err) + logging.LogFatal("Could not reset the progressbar", err) } bar.ChangeMax(repoCount) - logPrint("HTTP: Returning repositories found", nil) + logging.LogPrint(debug, "HTTP: Returning repositories found", nil) return repositories, nil } diff --git a/cmd/gogitlabber/input.go b/cmd/gogitlabber/input.go index 1ab7cf6..465de00 100644 --- a/cmd/gogitlabber/input.go +++ b/cmd/gogitlabber/input.go @@ -2,6 +2,7 @@ package main import ( "flag" + "gogitlabber/cmd/gogitlabber/logging" "os" "strconv" "strings" @@ -23,7 +24,7 @@ func setDefaultsFromEnv() { if debugVal, err := strconv.ParseBool(envDebug); err == nil { debug = debugVal } else { - logPrint("Warning: Invalid debug value in environment, using default", nil) + logging.LogPrint(debug, "Warning: Invalid debug value in environment, using default", nil) } } @@ -43,7 +44,7 @@ func setDefaultsFromEnv() { if concurrencyVal, err := strconv.Atoi(envConcurrency); err == nil { concurrency = concurrencyVal } else { - logPrint("Warning: Invalid concurrency value in environment, using default", nil) + logging.LogPrint(debug, "Warning: Invalid concurrency value in environment, using default", nil) } } @@ -52,7 +53,7 @@ func setDefaultsFromEnv() { case "any", "exclusive", "excluded": includeArchived = envArchived default: - logPrint("Warning: Invalid archived value in environment, using default", nil) + logging.LogPrint(debug, "Warning: Invalid archived value in environment, using default", nil) } } } @@ -111,7 +112,7 @@ func manageArguments() { // validate required parameters if gitlabToken == "" { flag.Usage() - logFatal("Configuration: Gitlab API Token not found", nil) + logging.LogFatal("Configuration: Gitlab API Token not found", nil) } // validate archived option @@ -119,15 +120,15 @@ func manageArguments() { case "any", "exclusive", "excluded": default: flag.Usage() - logFatal("Configuration: Invalid archive option: "+includeArchived, nil) + logging.LogFatal("Configuration: Invalid archive option: "+includeArchived, nil) } // log configuration - logPrint("Configuration: Using GitLab host: "+gitlabHost, nil) - logPrint("Configuration: Using destination: "+repoDestinationPre, nil) - logPrint("Configuration: Using concurrency: "+strconv.Itoa(concurrency), nil) - logPrint("Configuration: Using archived option: "+includeArchived, nil) + logging.LogPrint(debug, "Configuration: Using GitLab host: "+gitlabHost, nil) + logging.LogPrint(debug, "Configuration: Using destination: "+repoDestinationPre, nil) + logging.LogPrint(debug, "Configuration: Using concurrency: "+strconv.Itoa(concurrency), nil) + logging.LogPrint(debug, "Configuration: Using archived option: "+includeArchived, nil) if debug { - logPrint("Configuration: Debug mode enabled", nil) + logging.LogPrint(debug, "Configuration: Debug mode enabled", nil) } } diff --git a/cmd/gogitlabber/logging/logging.go b/cmd/gogitlabber/logging/logging.go new file mode 100644 index 0000000..5732540 --- /dev/null +++ b/cmd/gogitlabber/logging/logging.go @@ -0,0 +1,23 @@ +package logging + +import ( + "log" +) + +func LogPrint(debug bool, message string, err error) { + if debug { + if err != nil { + log.Printf("gogitlabber | DEBUG: %v error: %v\n", message, err) + } + if err == nil { + log.Printf("gogitlabber | DEBUG: %v\n", message) + } + } +} + +func LogFatal(message string, err error) { + if err != nil { + log.Fatalf("gogitlabber | FATAL: %v error: %v\n", message, err) + } + log.Fatalf("gogitlabber | FATAL: %v\n", message) +} diff --git a/cmd/gogitlabber/main.go b/cmd/gogitlabber/main.go index a214354..fa7788f 100644 --- a/cmd/gogitlabber/main.go +++ b/cmd/gogitlabber/main.go @@ -1,6 +1,7 @@ package main import ( + "gogitlabber/cmd/gogitlabber/logging" "io" "log" ) @@ -33,9 +34,9 @@ func main() { // check for git err := verifyGitAvailable() if err != nil { - logFatal("git not found in path: %v", err) + logging.LogFatal("git not found in path: %v", err) } - logPrint("VALIDATION: git found in path", nil) + logging.LogPrint(debug, "VALIDATION: git found in path", nil) // make initial progressbar if !debug { @@ -46,7 +47,7 @@ func main() { // fetch repository information from gitlab repositories, err := fetchRepositoriesGitlab() if err != nil { - logFatal("FATAL: %v", err) + logging.LogFatal("FATAL: %v", err) } // manage found repositories diff --git a/cmd/gogitlabber/output.go b/cmd/gogitlabber/output.go index 5fcb74c..4ac67e7 100644 --- a/cmd/gogitlabber/output.go +++ b/cmd/gogitlabber/output.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "log" + "gogitlabber/cmd/gogitlabber/logging" "github.com/k0kubun/go-ansi" "github.com/schollz/progressbar/v3" @@ -32,17 +32,17 @@ func progressBar() { ) // initialize progressbar - logPrint("Initialize progressbar", nil) + logging.LogPrint(debug, "Initialize progressbar", nil) err := bar.RenderBlank() progressBarAdd(1) if err != nil { - logFatal("Initialization of the progressbar failed", err) + logging.LogFatal("Initialization of the progressbar failed", err) } } func progressBarAdd(amount int) { if err := bar.Add(amount); err != nil { - logPrint("ERROR: Progress bar update error: %v\n", err) + logging.LogPrint(debug, "ERROR: Progress bar update error: %v\n", err) } } @@ -67,21 +67,3 @@ func printPullError(pullErrorMsg []string) { } } } - -func logPrint(message string, err error) { - if debug { - if err != nil { - log.Printf("gogitlabber | DEBUG: %v error: %v\n", message, err) - } - if err == nil { - log.Printf("gogitlabber | DEBUG: %v\n", message) - } - } -} - -func logFatal(message string, err error) { - if err != nil { - log.Fatalf("gogitlabber | FATAL: %v error: %v\n", message, err) - } - log.Fatalf("gogitlabber | FATAL: %v\n", message) -}