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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fetchRepositoriesGitlab() []Repository {
|
func fetchRepositoriesGitlab() ([]Repository, error) {
|
||||||
|
|
||||||
// default options
|
// default options
|
||||||
membership := "membership=true"
|
membership := "membership=true"
|
||||||
|
|
@ -30,7 +29,7 @@ func fetchRepositoriesGitlab() []Repository {
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != 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)
|
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
|
||||||
|
|
@ -38,22 +37,22 @@ 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 {
|
||||||
log.Fatalf("fatal: making request: %v\n", err)
|
return nil, fmt.Errorf("making request: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
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
|
var repositories []Repository
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
|
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 {
|
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 != "":
|
case envToken != "":
|
||||||
gitlabToken = envToken
|
gitlabToken = envToken
|
||||||
default:
|
default:
|
||||||
|
flag.Usage()
|
||||||
log.Printf("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
|
||||||
|
|
@ -38,9 +37,8 @@ func manageArguments() {
|
||||||
case envHost != "":
|
case envHost != "":
|
||||||
gitlabHost = envHost
|
gitlabHost = envHost
|
||||||
default:
|
default:
|
||||||
|
flag.Usage()
|
||||||
log.Fatalf("no gitlab host found")
|
log.Fatalf("no gitlab host found")
|
||||||
flag.PrintDefaults()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage destination option
|
// manage destination option
|
||||||
|
|
@ -48,9 +46,8 @@ func manageArguments() {
|
||||||
case envRepoDest != "":
|
case envRepoDest != "":
|
||||||
repoDestinationPre = envRepoDest
|
repoDestinationPre = envRepoDest
|
||||||
default:
|
default:
|
||||||
|
flag.Usage()
|
||||||
log.Fatalf("no destination found")
|
log.Fatalf("no destination found")
|
||||||
flag.PrintDefaults()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add slash 🎩🎸 if not provided
|
// add slash 🎩🎸 if not provided
|
||||||
|
|
@ -74,8 +71,7 @@ func manageArguments() {
|
||||||
includeArchived = envArchived
|
includeArchived = envArchived
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
flag.Usage()
|
||||||
log.Fatalf("wrong archive option found")
|
log.Fatalf("wrong archive option found")
|
||||||
flag.PrintDefaults()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
// userdata
|
// userdata
|
||||||
var repoDestinationPre string
|
var repoDestinationPre string
|
||||||
var includeArchived string
|
var includeArchived string
|
||||||
|
|
@ -27,7 +29,10 @@ func main() {
|
||||||
verifyGitAvailable()
|
verifyGitAvailable()
|
||||||
|
|
||||||
// fetch repository information from gitlab
|
// fetch repository information from gitlab
|
||||||
repositories := fetchRepositoriesGitlab()
|
repositories, err := fetchRepositoriesGitlab()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("fatal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// manage found repositories
|
// manage found repositories
|
||||||
progressBar(repositories)
|
progressBar(repositories)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue