fix: moved logging functions to its own package

This commit is contained in:
Simon Cornet 2025-03-06 08:47:02 +01:00
commit 13602ae71c
6 changed files with 64 additions and 55 deletions

View file

@ -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)
}