feat: various small fixes including error handling
This commit is contained in:
parent
913707eb29
commit
b18685919e
3 changed files with 17 additions and 17 deletions
|
|
@ -3,11 +3,10 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func fetchRepositoriesGitlab() []Repository {
|
||||
func fetchRepositoriesGitlab() ([]Repository, error) {
|
||||
|
||||
// default options
|
||||
membership := "membership=true"
|
||||
|
|
@ -30,7 +29,7 @@ func fetchRepositoriesGitlab() []Repository {
|
|||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
log.Fatalf("fatal: creating request: %v\n", err)
|
||||
return nil, fmt.Errorf("creating request: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
|
||||
|
|
@ -38,22 +37,22 @@ func fetchRepositoriesGitlab() []Repository {
|
|||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatalf("fatal: making request: %v\n", err)
|
||||
return nil, fmt.Errorf("making request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Fatalf("fatal: api request failed with status: %d\n", resp.StatusCode)
|
||||
return nil, fmt.Errorf("API request failed with status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var repositories []Repository
|
||||
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
|
||||
log.Fatalf("fatal: decoding response: %v\n", err)
|
||||
return nil, fmt.Errorf("decoding response: %v", err)
|
||||
}
|
||||
|
||||
if len(repositories) < 1 {
|
||||
log.Println("warning: no repositories found")
|
||||
return repositories, fmt.Errorf("no repositories found")
|
||||
}
|
||||
|
||||
return repositories
|
||||
return repositories, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,8 @@ func manageArguments() {
|
|||
case envToken != "":
|
||||
gitlabToken = envToken
|
||||
default:
|
||||
flag.Usage()
|
||||
log.Printf("no gitlab api token found")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// manage gitlab url option
|
||||
|
|
@ -38,9 +37,8 @@ func manageArguments() {
|
|||
case envHost != "":
|
||||
gitlabHost = envHost
|
||||
default:
|
||||
flag.Usage()
|
||||
log.Fatalf("no gitlab host found")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// manage destination option
|
||||
|
|
@ -48,9 +46,8 @@ func manageArguments() {
|
|||
case envRepoDest != "":
|
||||
repoDestinationPre = envRepoDest
|
||||
default:
|
||||
flag.Usage()
|
||||
log.Fatalf("no destination found")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// add slash 🎩🎸 if not provided
|
||||
|
|
@ -74,8 +71,7 @@ func manageArguments() {
|
|||
includeArchived = envArchived
|
||||
|
||||
default:
|
||||
flag.Usage()
|
||||
log.Fatalf("wrong archive option found")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
// userdata
|
||||
var repoDestinationPre string
|
||||
var includeArchived string
|
||||
|
|
@ -27,7 +29,10 @@ func main() {
|
|||
verifyGitAvailable()
|
||||
|
||||
// fetch repository information from gitlab
|
||||
repositories := fetchRepositoriesGitlab()
|
||||
repositories, err := fetchRepositoriesGitlab()
|
||||
if err != nil {
|
||||
log.Fatalf("fatal: %v", err)
|
||||
}
|
||||
|
||||
// manage found repositories
|
||||
progressBar(repositories)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue