feat: improved error handling

This commit is contained in:
Simon Cornet 2025-02-26 07:54:35 +01:00
commit 722a657676

View file

@ -1,18 +1,18 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os/exec" "os/exec"
"strings" "strings"
) )
func checkoutRepositories(repositories []Repository) { func checkoutRepositories(repositories []Repository) {
for _, repo := range repositories { for _, repo := range repositories {
// create clone // create clone
repoName := string(repo.PathWithNamespace) repoName := string(repo.PathWithNamespace)
url := getGitlabURL(gitlabToken, gitlabHost, repoName) url := getGitlabURL(gitlabToken, gitlabHost, repoName)
// create repository destination // create repository destination
repoDestination := repoDestinationPre + repoName repoDestination := repoDestinationPre + repoName
@ -35,18 +35,14 @@ func checkoutRepositories(repositories []Repository) {
descriptionPrefix := descriptionPrefixPre + repoName + " ..." descriptionPrefix := descriptionPrefixPre + repoName + " ..."
bar.Describe(descriptionPrefix) bar.Describe(descriptionPrefix)
_, err := pullRepositories(repoDestination) pullRepositories(repoDestination)
if err != nil {
continue
}
pulledCount = pulledCount + 1 pulledCount = pulledCount + 1
continue continue
} }
// in case cloning failed and the directory does not exist // in case cloning failed and the directory does not exist
// print the clone error and continue // print the clone error and continue
log.Printf("\n❌ error cloning %s: %v\n%s\n", repoName, err, cloneOutput) log.Printf("failed to clone %s: %v", repoName, err)
errorCount = errorCount + 1 errorCount = errorCount + 1
bar.Add(1) bar.Add(1)
continue continue
@ -59,7 +55,6 @@ func checkoutRepositories(repositories []Repository) {
} }
func cloneRepository(repoDestination string, gitlabUrl string) (string, error) { func cloneRepository(repoDestination string, gitlabUrl string) (string, error) {
cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination) cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination)
cloneOutput, err := cloneCmd.CombinedOutput() cloneOutput, err := cloneCmd.CombinedOutput()
@ -67,20 +62,17 @@ func cloneRepository(repoDestination string, gitlabUrl string) (string, error) {
} }
func findRemote(repoDestination string) (string, error) { func findRemote(repoDestination string) (string, error) {
remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show") remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show")
remoteOutput, err := remoteCmd.CombinedOutput() remoteOutput, err := remoteCmd.CombinedOutput()
remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
if err != nil { if err != nil {
log.Printf("\n❌ error finding remote for: %s\n", err) return "", fmt.Errorf("finding remote: %v", err)
} }
return remote, err remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
return remote, nil
} }
func pullRepositories(repoDestination string) (string, error) { func pullRepositories(repoDestination string) (string, error) {
remote, err := findRemote(repoDestination) remote, err := findRemote(repoDestination)
pullCmd := exec.Command("git", "-C", repoDestination, "pull", remote) pullCmd := exec.Command("git", "-C", repoDestination, "pull", remote)
pullOutput, err := pullCmd.CombinedOutput() pullOutput, err := pullCmd.CombinedOutput()