From 838300e99ac6d80c424352e3b0ed79362650b2da Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Wed, 1 Jan 2025 23:58:19 +0100 Subject: [PATCH] feat: added usage information --- main.go | 65 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index 65744bf..8845c6a 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,5 @@ package main + import ( "encoding/json" "fmt" @@ -30,7 +31,7 @@ func main() { log.Fatalf("Error loading environment variables: %v", err) } - // manage all argument magic + // manage all argument magic manageArguments() // fetch repository information from gitlab @@ -45,15 +46,9 @@ func main() { } func loadEnvironmentVariables() error { - gitlabToken = os.Getenv("GITLAB_API_KEY") - if gitlabToken == "" { - return fmt.Errorf("GITLAB_API_KEY environment variable is not set") - } - gitlabHost = os.Getenv("GITLAB_HOSTNAME") - if gitlabHost == "" { - return fmt.Errorf("GITLAB_HOSTNAME environment variable is not set") - } + gitlabToken = os.Getenv("GITLAB_API_TOKEN") + repoDestinationPre = os.Getenv("GOGITLABBER_DESTINATION") return nil } @@ -61,8 +56,7 @@ func manageArguments() { // require at least the destination argument if len(os.Args) <= 1 { - fmt.Println("Usage: gogitlabber --destination=") - fmt.Println("Example: gogitlabber --destination=$HOME/repos") + printUsage() os.Exit(1) } @@ -83,12 +77,23 @@ func manageArguments() { gitlabHost = strings.TrimPrefix(arg, "--gitlab-url=") default: - fmt.Println("Usage: gogitlabber --destination=") - fmt.Println("Example: gogitlabber --destination=$HOME/repos") + printUsage() os.Exit(1) } } + // fail if destination is unknown + if repoDestinationPre == "" { + fmt.Println("Fatal: No destination found.") + printUsage() + os.Exit(1) + } + + // add slash 🎩🎸 if not provided + if !strings.HasSuffix(repoDestinationPre, "/") { + repoDestinationPre += "/" + } + // --archive options: // - any (fetch both) // - only (fetch archived only) @@ -104,17 +109,17 @@ func manageArguments() { os.Exit(1) } - // fail if destination is unknown - if repoDestinationPre == "" { - fmt.Println("Fatal: No destination found.") - fmt.Println("Example: gogitlabber --destination=$HOME/repos") - fmt.Println("Usage: gogitlabber --destination=$HOME/repos") + // verify GitLab input + if gitlabHost == "" { + fmt.Println("Fatal: No GitLab server configured.") + printUsage() os.Exit(1) - } + } - // add slash if not provided to directory - if !strings.HasSuffix(repoDestinationPre, "/") { - repoDestinationPre += "/" + if gitlabToken == "" { + fmt.Println("Fatal: No GitLab API Token found.") + printUsage() + os.Exit(1) } } @@ -235,10 +240,24 @@ func pullRepositories(repoDestination string) { } func printPullerror(pullError []string) { - if len(pullError) > 0 { for _, repo := range pullError { fmt.Printf("❕%s has unstaged changes.\n", repo) } } } + +func printUsage() { + fmt.Println("Usage: gogitlabber") + fmt.Println(" --archived=(any|excluded|only)") + fmt.Println(" --destination=$HOME/Documents") + fmt.Println(" --gitlab-url=gitlab.example.com") + fmt.Println(" --gitlab-token=") + fmt.Println("") + fmt.Println("You can also set these environment variables:") + fmt.Println(" GOGITLABBER_ARCHIVED=(any|excluded|only)") + fmt.Println(" GOGITLABBER_DESTINATION=$HOME/Documents") + fmt.Println(" GITLAB_API_TOKEN=") + fmt.Println(" GITLAB_URL=gitlab.example.com") + fmt.Println("") +}