feat: using github.com/scornet256/go-logger

This commit is contained in:
Simon Cornet 2025-03-06 12:03:41 +01:00
commit cf33cc31f4
10 changed files with 61 additions and 110 deletions

View file

@ -2,10 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"gogitlabber/cmd/gogitlabber/logging"
"os/exec" "os/exec"
"strings" "strings"
"sync" "sync"
"github.com/scornet256/go-logger"
) )
// add a mutex to safely increment shared counters // add a mutex to safely increment shared counters
@ -38,7 +39,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
repoDestination := repoDestinationPre + repoName repoDestination := repoDestinationPre + repoName
// log activity // log activity
logging.Print("Starting on repository: "+repoName, nil) logger.Print("Starting on repository: "+repoName, nil)
// make gitlab url // make gitlab url
url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName) url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName)
@ -47,7 +48,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
checkRepo := func(repoDestination string) string { checkRepo := func(repoDestination string) string {
checkCmd := exec.Command("git", "-C", repoDestination, "remote", "-v") checkCmd := exec.Command("git", "-C", repoDestination, "remote", "-v")
checkOutput, _ := checkCmd.CombinedOutput() checkOutput, _ := checkCmd.CombinedOutput()
logging.Print("Checking status for repository: "+repoName, nil) logger.Print("Checking status for repository: "+repoName, nil)
return string(checkOutput) return string(checkOutput)
} }
@ -59,19 +60,19 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
case strings.Contains(string(repoStatus), "No such file or directory"): case strings.Contains(string(repoStatus), "No such file or directory"):
// log activity // log activity
logging.Print("Decided to clone repository: "+repoName, nil) logger.Print("Decided to clone repository: "+repoName, nil)
// clone the repo // clone the repo
cloneRepository := func(repoDestination string, url string) (string, error) { cloneRepository := func(repoDestination string, url string) (string, error) {
cloneCmd := exec.Command("git", "clone", url, repoDestination) cloneCmd := exec.Command("git", "clone", url, repoDestination)
cloneOutput, err := cloneCmd.CombinedOutput() cloneOutput, err := cloneCmd.CombinedOutput()
logging.Print("Cloning repository: "+repoName+" to "+repoDestination, nil) logger.Print("Cloning repository: "+repoName+" to "+repoDestination, nil)
return string(cloneOutput), err return string(cloneOutput), err
} }
_, err := cloneRepository(repoDestination, url) _, err := cloneRepository(repoDestination, url)
if err != nil { if err != nil {
logging.Print("ERROR: %v\n", err) logger.Print("ERROR: %v\n", err)
} }
// set a lock, increment counters, update progressbar and unlock // set a lock, increment counters, update progressbar and unlock
@ -88,7 +89,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
// pull the latest // pull the latest
case strings.Contains(string(repoStatus), url): case strings.Contains(string(repoStatus), url):
logging.Print("Decided to pull repository: "+repoName, nil) logger.Print("Decided to pull repository: "+repoName, nil)
pullRepository(repoName, repoDestination) pullRepository(repoName, repoDestination)
if !debug { if !debug {
descriptionPrefixPre := "Pulling repository " descriptionPrefixPre := "Pulling repository "
@ -98,8 +99,8 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
} }
default: default:
logging.Print("ERROR: decided not to clone or pull repository: "+repoName, nil) logger.Print("ERROR: decided not to clone or pull repository: "+repoName, nil)
logging.Print("ERROR: this is why: "+repoStatus, nil) logger.Print("ERROR: this is why: "+repoStatus, nil)
// set a lock, increment counters and unlock // set a lock, increment counters and unlock
mu.Lock() mu.Lock()
@ -119,7 +120,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
func pullRepository(repoName string, repoDestination string) { func pullRepository(repoName string, repoDestination string) {
// log activity // log activity
logging.Print("Pulling repository: "+repoName+" at "+repoDestination, nil) logger.Print("Pulling repository: "+repoName+" at "+repoDestination, nil)
// find remote // find remote
findRemote := func(repoDestination string) (string, error) { findRemote := func(repoDestination string) (string, error) {
@ -129,9 +130,9 @@ func pullRepository(repoName string, repoDestination string) {
return "", fmt.Errorf("finding remote: %v\n", err) return "", fmt.Errorf("finding remote: %v\n", err)
} }
logging.Print("Finding remote for repository: "+repoName+" at "+repoDestination, nil) logger.Print("Finding remote for repository: "+repoName+" at "+repoDestination, nil)
remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0] remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
logging.Print("Found remote; "+remote+" for repository: "+repoName+" at "+repoDestination, nil) logger.Print("Found remote; "+remote+" for repository: "+repoName+" at "+repoDestination, nil)
return remote, nil return remote, nil
} }
remote, _ := findRemote(repoDestination) remote, _ := findRemote(repoDestination)
@ -156,13 +157,13 @@ func pullRepository(repoName string, repoDestination string) {
switch { switch {
case strings.Contains(string(pullOutput), "You have unstaged changes"): case strings.Contains(string(pullOutput), "You have unstaged changes"):
pullErrorMsg = append(pullErrorMsg, repoDestination) pullErrorMsg = append(pullErrorMsg, repoDestination)
logging.Print("Found unstaged changes for repository: "+repoName+" at "+repoDestination, nil) logger.Print("Found unstaged changes for repository: "+repoName+" at "+repoDestination, nil)
default: default:
logging.Print("ERROR: pulling "+repoName, nil) logger.Print("ERROR: pulling "+repoName, nil)
} }
} }
// log activity // log activity
logging.Print("Pulled repository: "+repoName+" at "+repoDestination, nil) logger.Print("Pulled repository: "+repoName+" at "+repoDestination, nil)
} }

View file

@ -3,8 +3,9 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gogitlabber/cmd/gogitlabber/logging"
"net/http" "net/http"
"github.com/scornet256/go-logger"
) )
func fetchRepositoriesGitlab() ([]Repository, error) { func fetchRepositoriesGitlab() ([]Repository, error) {
@ -28,16 +29,16 @@ func fetchRepositoriesGitlab() ([]Repository, error) {
url := fmt.Sprintf("https://%s/api/v4/projects?%s&%s&%s%s", url := fmt.Sprintf("https://%s/api/v4/projects?%s&%s&%s%s",
gitlabHost, membership, order, perpage, archived) gitlabHost, membership, order, perpage, archived)
logging.Print("HTTP: Creating API request", nil) logger.Print("HTTP: Creating API request", nil)
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("ERROR: creating request: %v\n", err) return nil, fmt.Errorf("ERROR: creating request: %v\n", err)
} }
logging.Print("HTTP: Adding PRIVATE-TOKEN header to API request", nil) logger.Print("HTTP: Adding PRIVATE-TOKEN header to API request", nil)
req.Header.Set("PRIVATE-TOKEN", gitlabToken) req.Header.Set("PRIVATE-TOKEN", gitlabToken)
logging.Print("HTTP: Making request", nil) logger.Print("HTTP: Making request", nil)
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
@ -49,7 +50,7 @@ func fetchRepositoriesGitlab() ([]Repository, error) {
return nil, fmt.Errorf("ERROR: API request failed with status: %d\n", resp.StatusCode) return nil, fmt.Errorf("ERROR: API request failed with status: %d\n", resp.StatusCode)
} }
logging.Print("HTTP: Decoding JSON response", nil) logger.Print("HTTP: Decoding JSON response", nil)
var repositories []Repository var repositories []Repository
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil { if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
return nil, fmt.Errorf("ERROR: decoding response: %v\n", err) return nil, fmt.Errorf("ERROR: decoding response: %v\n", err)
@ -61,19 +62,19 @@ func fetchRepositoriesGitlab() ([]Repository, error) {
repoCount := len(repositories) repoCount := len(repositories)
logging.Print("BAR: Resetting the progressbar", nil) logger.Print("BAR: Resetting the progressbar", nil)
if !debug { if !debug {
err = bar.Set(0) err = bar.Set(0)
if err != nil { if err != nil {
logging.Fatal("Could not reset the progressbar", err) logger.Fatal("Could not reset the progressbar", err)
} }
} }
logging.Print("BAR: Increasing the max value of the progressbar", nil) logger.Print("BAR: Increasing the max value of the progressbar", nil)
if !debug { if !debug {
bar.ChangeMax(repoCount) bar.ChangeMax(repoCount)
} }
logging.Print("HTTP: Returning repositories found", nil) logger.Print("HTTP: Returning repositories found", nil)
return repositories, nil return repositories, nil
} }

View file

@ -2,10 +2,11 @@ package main
import ( import (
"flag" "flag"
"gogitlabber/cmd/gogitlabber/logging"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/scornet256/go-logger"
) )
// set default values and override values from environment variables // set default values and override values from environment variables
@ -24,7 +25,7 @@ func setDefaultsFromEnv() {
if debugVal, err := strconv.ParseBool(envDebug); err == nil { if debugVal, err := strconv.ParseBool(envDebug); err == nil {
debug = debugVal debug = debugVal
} else { } else {
logging.Print("Warning: Invalid debug value in environment, using default", nil) logger.Print("Warning: Invalid debug value in environment, using default", nil)
} }
} }
@ -44,7 +45,7 @@ func setDefaultsFromEnv() {
if concurrencyVal, err := strconv.Atoi(envConcurrency); err == nil { if concurrencyVal, err := strconv.Atoi(envConcurrency); err == nil {
concurrency = concurrencyVal concurrency = concurrencyVal
} else { } else {
logging.Print("Warning: Invalid concurrency value in environment, using default", nil) logger.Print("Warning: Invalid concurrency value in environment, using default", nil)
} }
} }
@ -53,7 +54,7 @@ func setDefaultsFromEnv() {
case "any", "exclusive", "excluded": case "any", "exclusive", "excluded":
includeArchived = envArchived includeArchived = envArchived
default: default:
logging.Print("Warning: Invalid archived value in environment, using default", nil) logger.Print("Warning: Invalid archived value in environment, using default", nil)
} }
} }
} }
@ -112,7 +113,7 @@ func manageArguments() {
// validate required parameters // validate required parameters
if gitlabToken == "" { if gitlabToken == "" {
flag.Usage() flag.Usage()
logging.Fatal("Configuration: Gitlab API Token not found", nil) logger.Fatal("Configuration: Gitlab API Token not found", nil)
} }
// validate archived option // validate archived option
@ -120,15 +121,15 @@ func manageArguments() {
case "any", "exclusive", "excluded": case "any", "exclusive", "excluded":
default: default:
flag.Usage() flag.Usage()
logging.Fatal("Configuration: Invalid archive option: "+includeArchived, nil) logger.Fatal("Configuration: Invalid archive option: "+includeArchived, nil)
} }
// log configuration // log configuration
logging.Print("Configuration: Using GitLab host: "+gitlabHost, nil) logger.Print("Configuration: Using GitLab host: "+gitlabHost, nil)
logging.Print("Configuration: Using destination: "+repoDestinationPre, nil) logger.Print("Configuration: Using destination: "+repoDestinationPre, nil)
logging.Print("Configuration: Using concurrency: "+strconv.Itoa(concurrency), nil) logger.Print("Configuration: Using concurrency: "+strconv.Itoa(concurrency), nil)
logging.Print("Configuration: Using archived option: "+includeArchived, nil) logger.Print("Configuration: Using archived option: "+includeArchived, nil)
if debug { if debug {
logging.Print("Configuration: Debug mode enabled", nil) logger.Print("Configuration: Debug mode enabled", nil)
} }
} }

View file

@ -1,21 +0,0 @@
package logging
import (
"io"
"log"
)
var debug bool
// Enables debug logging if set to true. Default is false.
func SetDebug(debugSetting bool) {
debug = debugSetting
if !debug {
log.SetOutput(io.Discard)
}
}
// Returns the current debug value as a boolean.
func GetDebug() bool {
return debug
}

View file

@ -1,25 +0,0 @@
package logging
import (
"log"
)
// Prints the formatted log, taking both a message (string) and optionally
// an error as inputs.
func Print(message string, err error) {
if debug {
log.Printf(applicationName+" | LOG: %v\n", message)
if err != nil {
log.Printf(applicationName+" | ERROR: %v\n", err)
}
}
}
// Prints the fatal error and exits the application. Takes both the message and
// optionally an error as inputs.
func Fatal(message string, err error) {
log.Fatalf(applicationName+" | FATAL: %v\n", message)
if err != nil {
log.Fatalf(applicationName+" | ERROR: %v\n", err)
}
}

View file

@ -1,15 +0,0 @@
package logging
var applicationName = ""
// Sets the application name prefix used in the logoutput. Example:
// <applicationName> | ...
func SetAppName(name string) {
applicationName = name
}
// Returns the logging prefix name as string.
func GetAppName() string {
return applicationName
}

View file

@ -1,7 +1,7 @@
package main package main
import ( import (
"gogitlabber/cmd/gogitlabber/logging" "github.com/scornet256/go-logger"
) )
// userdata // userdata
@ -26,21 +26,21 @@ type Repository struct {
func main() { func main() {
// set appname for logging // set appname for logger
logging.SetAppName("gogitlabber") logger.SetAppName("gogitlabber")
// manage all argument magic // manage all argument magic
manageArguments() manageArguments()
// set debugging // set debugging
logging.SetDebug(debug) logger.SetDebug(debug)
// check for git // check for git
err := verifyGitAvailable() err := verifyGitAvailable()
if err != nil { if err != nil {
logging.Fatal("VALIDATION: git not found in path", err) logger.Fatal("VALIDATION: git not found in path", err)
} }
logging.Print("VALIDATION: git found in path", nil) logger.Print("VALIDATION: git found in path", nil)
// make initial progressbar // make initial progressbar
if !debug { if !debug {
@ -50,7 +50,7 @@ func main() {
// fetch repository information from gitlab // fetch repository information from gitlab
repositories, err := fetchRepositoriesGitlab() repositories, err := fetchRepositoriesGitlab()
if err != nil { if err != nil {
logging.Fatal("Fetching repositories failed", err) logger.Fatal("Fetching repositories failed", err)
} }
// manage found repositories // manage found repositories

View file

@ -2,10 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"gogitlabber/cmd/gogitlabber/logging"
"github.com/k0kubun/go-ansi" "github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
"github.com/scornet256/go-logger"
) )
var bar *progressbar.ProgressBar var bar *progressbar.ProgressBar
@ -32,18 +32,18 @@ func progressBar() {
) )
// initialize progressbar // initialize progressbar
logging.Print("Initialize progressbar", nil) logger.Print("Initialize progressbar", nil)
err := bar.RenderBlank() err := bar.RenderBlank()
progressBarAdd(1) progressBarAdd(1)
if err != nil { if err != nil {
logging.Fatal("Initialization of the progressbar failed", err) logger.Fatal("Initialization of the progressbar failed", err)
} }
} }
func progressBarAdd(amount int) { func progressBarAdd(amount int) {
logging.Print("BAR: Progressing the bar", nil) logger.Print("BAR: Progressing the bar", nil)
if err := bar.Add(amount); err != nil { if err := bar.Add(amount); err != nil {
logging.Print("BAR: Could not update the bar", err) logger.Print("BAR: Could not update the bar", err)
} }
} }

5
go.mod
View file

@ -5,12 +5,13 @@ go 1.24.0
require ( require (
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/schollz/progressbar/v3 v3.18.0 github.com/schollz/progressbar/v3 v3.18.0
github.com/scornet256/go-logger v0.0.0-20250306110019-822a77b239f9
) )
require ( require (
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.7 // indirect github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.30.0 // indirect golang.org/x/sys v0.31.0 // indirect
golang.org/x/term v0.29.0 // indirect golang.org/x/term v0.30.0 // indirect
) )

8
go.sum
View file

@ -10,12 +10,20 @@ github.com/schollz/progressbar/v3 v3.17.1 h1:bI1MTaoQO+v5kzklBjYNRQLoVpe0zbyRZNK
github.com/schollz/progressbar/v3 v3.17.1/go.mod h1:RzqpnsPQNjUyIgdglUjRLgD7sVnxN1wpmBMV+UiEbL4= github.com/schollz/progressbar/v3 v3.17.1/go.mod h1:RzqpnsPQNjUyIgdglUjRLgD7sVnxN1wpmBMV+UiEbL4=
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA= github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec= github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
github.com/scornet256/go-logger v0.0.0-20250306105431-80d01ed762fe h1:q9RoYlAxWoCD6GuflKKAfT584+tRzGEeJXBO1jUlJtI=
github.com/scornet256/go-logger v0.0.0-20250306105431-80d01ed762fe/go.mod h1:GptTzXTPlyNj2mZjhRyWfmP4EDb1Ca2osDpooBy6MmI=
github.com/scornet256/go-logger v0.0.0-20250306110019-822a77b239f9 h1:oHcJkJ1kyptkzoiyplglByhjk3DOt6EVllMeS/00hSI=
github.com/scornet256/go-logger v0.0.0-20250306110019-822a77b239f9/go.mod h1:GptTzXTPlyNj2mZjhRyWfmP4EDb1Ca2osDpooBy6MmI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU= golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s= golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=