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
|
||||||
|
|
@ -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)
|
||||||
|
|
@ -100,7 +98,7 @@ func pullRepository(repoName string, repoDestination string) {
|
||||||
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"
|
||||||
)
|
)
|
||||||
|
|
@ -28,7 +28,7 @@ func manageArguments() {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ func manageArguments() {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ func manageArguments() {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +74,7 @@ func manageArguments() {
|
||||||
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