feat: improved error handling
This commit is contained in:
parent
8247c4c7e4
commit
f9c1ffa0c7
4 changed files with 28 additions and 31 deletions
|
|
@ -10,10 +10,8 @@ import (
|
||||||
func checkoutRepositories(repositories []Repository) {
|
func checkoutRepositories(repositories []Repository) {
|
||||||
for _, repo := range repositories {
|
for _, repo := range repositories {
|
||||||
|
|
||||||
// get repository name
|
// get repository name + create repo destination
|
||||||
repoName := string(repo.PathWithNamespace)
|
repoName := string(repo.PathWithNamespace)
|
||||||
|
|
||||||
// create repository destination
|
|
||||||
repoDestination := repoDestinationPre + repoName
|
repoDestination := repoDestinationPre + repoName
|
||||||
|
|
||||||
// make gitlab url
|
// make gitlab url
|
||||||
|
|
@ -33,7 +31,7 @@ func checkoutRepositories(repositories []Repository) {
|
||||||
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)
|
||||||
|
|
@ -47,7 +45,7 @@ func checkoutRepositories(repositories []Repository) {
|
||||||
}
|
}
|
||||||
_, err := cloneRepository(repoDestination, url)
|
_, err := cloneRepository(repoDestination, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %v", err)
|
log.Printf("error: %v\n", err)
|
||||||
}
|
}
|
||||||
clonedCount = clonedCount + 1
|
clonedCount = clonedCount + 1
|
||||||
progressBarAdd(1)
|
progressBarAdd(1)
|
||||||
|
|
@ -95,12 +93,12 @@ func pullRepository(repoName string, repoDestination string) {
|
||||||
errorCount = errorCount + 1
|
errorCount = errorCount + 1
|
||||||
pulledCount = pulledCount - 1
|
pulledCount = pulledCount - 1
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(string(pullOutput), "You have unstaged changes"):
|
case strings.Contains(string(pullOutput), "You have unstaged changes"):
|
||||||
pullErrorMsg = append(pullErrorMsg, repoDestination)
|
pullErrorMsg = append(pullErrorMsg, repoDestination)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
log.Printf("pull error: %v", err)
|
log.Printf("error: pulling %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fetchRepositoriesGitlab() ([]Repository) {
|
func fetchRepositoriesGitlab() []Repository {
|
||||||
|
|
||||||
// default options
|
// default options
|
||||||
membership := "membership=true"
|
membership := "membership=true"
|
||||||
|
|
@ -29,7 +30,7 @@ func fetchRepositoriesGitlab() ([]Repository) {
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("creating request: %v", err)
|
log.Fatalf("fatal: creating request: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
|
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
|
||||||
|
|
@ -37,21 +38,21 @@ func fetchRepositoriesGitlab() ([]Repository) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("making request: %v", err)
|
log.Fatalf("fatal: making request: %v\n", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
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
|
var repositories []Repository
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
|
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 {
|
if len(repositories) < 1 {
|
||||||
fmt.Errorf("no repositories found")
|
log.Println("warning: no repositories found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return repositories
|
return repositories
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -23,45 +23,45 @@ func manageArguments() {
|
||||||
gitlabToken = *tokenFlag
|
gitlabToken = *tokenFlag
|
||||||
gitlabHost = *hostFlag
|
gitlabHost = *hostFlag
|
||||||
|
|
||||||
// manage gitlab api option
|
// manage gitlab api option
|
||||||
switch envToken := os.Getenv("GITLAB_API_TOKEN"); {
|
switch envToken := os.Getenv("GITLAB_API_TOKEN"); {
|
||||||
case envToken != "":
|
case envToken != "":
|
||||||
gitlabToken = envToken
|
gitlabToken = envToken
|
||||||
default:
|
default:
|
||||||
fmt.Println("fatal: No GitLab API Token found.")
|
log.Printf("no gitlab api token found")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage gitlab url option
|
// manage gitlab url option
|
||||||
switch envHost := os.Getenv("GITLAB_URL"); {
|
switch envHost := os.Getenv("GITLAB_URL"); {
|
||||||
case envHost != "":
|
case envHost != "":
|
||||||
gitlabHost = envHost
|
gitlabHost = envHost
|
||||||
default:
|
default:
|
||||||
fmt.Println("fatal: No GitLab Host found.")
|
log.Fatalf("no gitlab host found")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage destination option
|
// manage destination option
|
||||||
switch envRepoDest := os.Getenv("GOGITLABBER_DESTINATION"); {
|
switch envRepoDest := os.Getenv("GOGITLABBER_DESTINATION"); {
|
||||||
case envRepoDest != "":
|
case envRepoDest != "":
|
||||||
repoDestinationPre = envRepoDest
|
repoDestinationPre = envRepoDest
|
||||||
default:
|
default:
|
||||||
fmt.Println("fatal: No destination found.")
|
log.Fatalf("no destination found")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add slash 🎩🎸 if not provided
|
// add slash 🎩🎸 if not provided
|
||||||
switch {
|
switch {
|
||||||
case !strings.HasSuffix(repoDestinationPre, "/"):
|
case !strings.HasSuffix(repoDestinationPre, "/"):
|
||||||
repoDestinationPre += "/"
|
repoDestinationPre += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage archived option
|
// manage archived option
|
||||||
switch envArchived := os.Getenv("GOGITLABBER_ARCHIVED"); {
|
switch envArchived := os.Getenv("GOGITLABBER_ARCHIVED"); {
|
||||||
case envArchived == "":
|
case envArchived == "":
|
||||||
includeArchived = "excluded"
|
includeArchived = "excluded"
|
||||||
|
|
||||||
case envArchived == "any":
|
case envArchived == "any":
|
||||||
|
|
@ -73,8 +73,8 @@ func manageArguments() {
|
||||||
case envArchived == "excluded":
|
case envArchived == "excluded":
|
||||||
includeArchived = envArchived
|
includeArchived = envArchived
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Println("fatal: Wrong archive option found.")
|
log.Fatalf("wrong archive option found")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func verifyGitAvailable() {
|
func verifyGitAvailable() {
|
||||||
_, err := exec.LookPath("git")
|
_, err := exec.LookPath("git")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error: could not find git in path")
|
log.Fatal("could not find git in path")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue