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,19 +8,26 @@ 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) {
// create a waitgroup + semaphore channel
var wg sync.WaitGroup var wg sync.WaitGroup
semaphore := make(chan struct{}, concurrency) semaphore := make(chan struct{}, concurrency)
// manage all repositories found
for _, repo := range repositories { for _, repo := range repositories {
// increment waitgroup counter + acquire semaphore slot
wg.Add(1) wg.Add(1)
semaphore <- struct{}{} semaphore <- struct{}{}
// start go routine per repo
go func(repo Repository) { go func(repo Repository) {
// ensure we release the semaphore and close the goroutine
defer func() { defer func() {
<-semaphore <-semaphore
wg.Done() wg.Done()
@ -63,6 +70,8 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
if err != nil { if err != nil {
log.Printf("ERROR: %v\n", err) log.Printf("ERROR: %v\n", err)
} }
// set a lock, increment counters and unlock
mu.Lock() mu.Lock()
clonedCount++ clonedCount++
mu.Unlock() mu.Unlock()
@ -76,6 +85,8 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
default: default:
log.Printf("ERROR: decided not to clone or pull repository %v\n", repoName) log.Printf("ERROR: decided not to clone or pull repository %v\n", repoName)
log.Printf("ERROR: this is why: %v\n", repoStatus) log.Printf("ERROR: this is why: %v\n", repoStatus)
// set a lock, increment counters and unlock
mu.Lock() mu.Lock()
errorCount++ errorCount++
mu.Unlock() mu.Unlock()
@ -83,6 +94,8 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
} }
}(repo) }(repo)
} }
// wait for goroutines
wg.Wait() wg.Wait()
} }
@ -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"):