style: use linter to style the code

This commit is contained in:
Simon Cornet 2025-03-04 15:10:29 +01:00
commit 4db46dd96f

View file

@ -8,82 +8,95 @@ import (
"sync" "sync"
) )
// add a mutex to safely increment shared counters
var mu sync.Mutex var mu sync.Mutex
func checkoutRepositories(repositories []Repository, concurrency int) { func checkoutRepositories(repositories []Repository, concurrency int) {
var wg sync.WaitGroup // create a waitgroup + semaphore channel
semaphore := make(chan struct{}, concurrency) var wg sync.WaitGroup
semaphore := make(chan struct{}, concurrency)
// manage all repositories found
for _, repo := range repositories { for _, repo := range repositories {
wg.Add(1) // increment waitgroup counter + acquire semaphore slot
semaphore <- struct{}{} wg.Add(1)
semaphore <- struct{}{}
go func(repo Repository) { // start go routine per repo
go func(repo Repository) {
defer func() { // ensure we release the semaphore and close the goroutine
<-semaphore defer func() {
wg.Done() <-semaphore
}() wg.Done()
}()
// get repository name + create repo destination // get repository name + create repo destination
repoName := string(repo.PathWithNamespace) repoName := string(repo.PathWithNamespace)
repoDestination := repoDestinationPre + repoName repoDestination := repoDestinationPre + repoName
// 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)
// check current status of repoDestination // check current status of repoDestination
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()
return string(checkOutput) return string(checkOutput)
} }
repoStatus := checkRepo(repoDestination) repoStatus := checkRepo(repoDestination)
// report error if not cloned or pulled repository // report error if not cloned or pulled repository
// clone repository if it does not exist // clone repository if it does not exist
switch { switch {
case strings.Contains(string(repoStatus), "No such file or directory"): case strings.Contains(string(repoStatus), "No such file or directory"):
// update the progress bar // update the progress bar
descriptionPrefixPre := "Cloning repository " descriptionPrefixPre := "Cloning repository "
descriptionPrefix := descriptionPrefixPre + repoName + " ..." descriptionPrefix := descriptionPrefixPre + repoName + " ..."
bar.Describe(descriptionPrefix) bar.Describe(descriptionPrefix)
// 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()
return string(cloneOutput), err return string(cloneOutput), err
} }
_, err := cloneRepository(repoDestination, url) _, err := cloneRepository(repoDestination, url)
if err != nil { if err != nil {
log.Printf("ERROR: %v\n", err) log.Printf("ERROR: %v\n", err)
} }
mu.Lock()
clonedCount++
mu.Unlock()
progressBarAdd(1)
// pull the latest // set a lock, increment counters and unlock
case strings.Contains(string(repoStatus), url): mu.Lock()
pullRepository(repoName, repoDestination) clonedCount++
progressBarAdd(1) mu.Unlock()
progressBarAdd(1)
default: // pull the latest
log.Printf("ERROR: decided not to clone or pull repository %v\n", repoName) case strings.Contains(string(repoStatus), url):
log.Printf("ERROR: this is why: %v\n", repoStatus) pullRepository(repoName, repoDestination)
mu.Lock() progressBarAdd(1)
errorCount++
mu.Unlock() default:
progressBarAdd(1) log.Printf("ERROR: decided not to clone or pull repository %v\n", repoName)
} log.Printf("ERROR: this is why: %v\n", repoStatus)
}(repo)
} // set a lock, increment counters and unlock
wg.Wait() mu.Lock()
errorCount++
mu.Unlock()
progressBarAdd(1)
}
}(repo)
}
// wait for goroutines
wg.Wait()
} }
func pullRepository(repoName string, repoDestination string) { func pullRepository(repoName string, repoDestination string) {
@ -109,11 +122,19 @@ func pullRepository(repoName string, repoDestination string) {
// pull repository // pull repository
pullCmd := exec.Command("git", "-C", repoDestination, "pull", remote) pullCmd := exec.Command("git", "-C", repoDestination, "pull", remote)
pullOutput, err := pullCmd.CombinedOutput() pullOutput, err := pullCmd.CombinedOutput()
pulledCount = pulledCount + 1
// set a lock, increment counters and unlock
mu.Lock()
pulledCount++
mu.Unlock()
if err != nil { if err != nil {
errorCount = errorCount + 1
pulledCount = pulledCount - 1 // set a lock, increment counters and unlock
mu.Lock()
errorCount++
pulledCount--
mu.Unlock()
switch { switch {
case strings.Contains(string(pullOutput), "You have unstaged changes"): case strings.Contains(string(pullOutput), "You have unstaged changes"):