feat: improved error handling

This commit is contained in:
Simon Cornet 2025-03-04 13:21:29 +01:00
commit f9c1ffa0c7
4 changed files with 28 additions and 31 deletions

View file

@ -10,10 +10,8 @@ import (
func checkoutRepositories(repositories []Repository) {
for _, repo := range repositories {
// get repository name
// get repository name + create repo destination
repoName := string(repo.PathWithNamespace)
// create repository destination
repoDestination := repoDestinationPre + repoName
// make gitlab url
@ -33,7 +31,7 @@ func checkoutRepositories(repositories []Repository) {
switch {
case strings.Contains(string(repoStatus), "No such file or directory"):
// update the progress bar
// update the progress bar
descriptionPrefixPre := "Cloning repository "
descriptionPrefix := descriptionPrefixPre + repoName + " ..."
bar.Describe(descriptionPrefix)
@ -47,7 +45,7 @@ func checkoutRepositories(repositories []Repository) {
}
_, err := cloneRepository(repoDestination, url)
if err != nil {
log.Printf("error: %v", err)
log.Printf("error: %v\n", err)
}
clonedCount = clonedCount + 1
progressBarAdd(1)
@ -95,12 +93,12 @@ func pullRepository(repoName string, repoDestination string) {
errorCount = errorCount + 1
pulledCount = pulledCount - 1
switch {
case strings.Contains(string(pullOutput), "You have unstaged changes"):
switch {
case strings.Contains(string(pullOutput), "You have unstaged changes"):
pullErrorMsg = append(pullErrorMsg, repoDestination)
default:
log.Printf("pull error: %v", err)
default:
log.Printf("error: pulling %v\n", err)
}
}
}

View file

@ -3,10 +3,11 @@ package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func fetchRepositoriesGitlab() ([]Repository) {
func fetchRepositoriesGitlab() []Repository {
// default options
membership := "membership=true"
@ -29,7 +30,7 @@ func fetchRepositoriesGitlab() ([]Repository) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Errorf("creating request: %v", err)
log.Fatalf("fatal: creating request: %v\n", err)
}
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
@ -37,21 +38,21 @@ func fetchRepositoriesGitlab() ([]Repository) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Errorf("making request: %v", err)
log.Fatalf("fatal: making request: %v\n", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Errorf("API request failed with status: %d", resp.StatusCode)
log.Fatalf("fatal: api request failed with status: %d\n", resp.StatusCode)
}
var repositories []Repository
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
fmt.Errorf("decoding response: %v", err)
log.Fatalf("fatal: decoding response: %v\n", err)
}
if len(repositories) < 1 {
fmt.Errorf("no repositories found")
log.Println("warning: no repositories found")
}
return repositories

View file

@ -2,7 +2,7 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
@ -23,45 +23,45 @@ func manageArguments() {
gitlabToken = *tokenFlag
gitlabHost = *hostFlag
// manage gitlab api option
// manage gitlab api option
switch envToken := os.Getenv("GITLAB_API_TOKEN"); {
case envToken != "":
gitlabToken = envToken
default:
fmt.Println("fatal: No GitLab API Token found.")
log.Printf("no gitlab api token found")
flag.PrintDefaults()
os.Exit(1)
}
// manage gitlab url option
// manage gitlab url option
switch envHost := os.Getenv("GITLAB_URL"); {
case envHost != "":
gitlabHost = envHost
default:
fmt.Println("fatal: No GitLab Host found.")
log.Fatalf("no gitlab host found")
flag.PrintDefaults()
os.Exit(1)
}
// manage destination option
// manage destination option
switch envRepoDest := os.Getenv("GOGITLABBER_DESTINATION"); {
case envRepoDest != "":
repoDestinationPre = envRepoDest
default:
fmt.Println("fatal: No destination found.")
log.Fatalf("no destination found")
flag.PrintDefaults()
os.Exit(1)
}
// add slash 🎩🎸 if not provided
switch {
case !strings.HasSuffix(repoDestinationPre, "/"):
switch {
case !strings.HasSuffix(repoDestinationPre, "/"):
repoDestinationPre += "/"
}
// manage archived option
// manage archived option
switch envArchived := os.Getenv("GOGITLABBER_ARCHIVED"); {
case envArchived == "":
case envArchived == "":
includeArchived = "excluded"
case envArchived == "any":
@ -73,8 +73,8 @@ func manageArguments() {
case envArchived == "excluded":
includeArchived = envArchived
default:
fmt.Println("fatal: Wrong archive option found.")
default:
log.Fatalf("wrong archive option found")
flag.PrintDefaults()
os.Exit(1)
}

View file

@ -2,14 +2,12 @@ package main
import (
"log"
"os"
"os/exec"
)
func verifyGitAvailable() {
_, err := exec.LookPath("git")
if err != nil {
log.Fatalf("Error: could not find git in path")
os.Exit(1)
log.Fatal("could not find git in path")
}
}