From f90a706967aa3e2088c638abc44c2ef48654db9c Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Sun, 5 Jan 2025 12:10:37 +0100 Subject: [PATCH] feat: use flag package even better --- cmd/gogitlabber/input.go | 65 ++++++++++++++++++++-------------------- cmd/gogitlabber/main.go | 5 ---- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/cmd/gogitlabber/input.go b/cmd/gogitlabber/input.go index 9723127..0c8b8d8 100644 --- a/cmd/gogitlabber/input.go +++ b/cmd/gogitlabber/input.go @@ -7,13 +7,6 @@ import ( "strings" ) -func loadEnvironmentVariables() error { - gitlabHost = os.Getenv("GITLAB_HOSTNAME") - gitlabToken = os.Getenv("GITLAB_API_TOKEN") - repoDestinationPre = os.Getenv("GOGITLABBER_DESTINATION") - return nil -} - func manageArguments() { var archivedFlag = flag.String("archived", "excluded", "to include archived repositories (any|excluded|exclusive)\nenv = GOGITLABBER_ARCHIVED\n") @@ -29,17 +22,40 @@ func manageArguments() { gitlabToken = *tokenFlag gitlabHost = *hostFlag - // fail if destination is unknown - if repoDestinationPre == "" { - fmt.Println("Fatal: No destination found.") + // use environment variable if set, otherwise use flag value + if envHost := os.Getenv("GITLAB_HOSTNAME"); envHost != "" { + gitlabHost = envHost + } + + if envToken := os.Getenv("GITLAB_API_TOKEN"); envToken != "" { + gitlabToken = envToken + } + + if envRepoDest := os.Getenv("GOGITLABBER_DESTINATION"); envRepoDest != "" { + repoDestinationPre = envRepoDest + } + + if envArchived := os.Getenv("GOGITLABBER_ARCHIVED"); envArchived != "" { + includeArchived = envArchived + } + + // fail if no configuration found + if gitlabHost == "" { + fmt.Println("Fatal: No GitLab Host found.") flag.PrintDefaults() - fmt.Println("") os.Exit(1) } - // add slash 🎩🎸 if not provided - if !strings.HasSuffix(repoDestinationPre, "/") { - repoDestinationPre += "/" + if gitlabToken == "" { + fmt.Println("Fatal: No GitLab API Token found.") + flag.PrintDefaults() + os.Exit(1) + } + + if repoDestinationPre == "" { + fmt.Println("Fatal: No destination found.") + flag.PrintDefaults() + os.Exit(1) } // --archive options: @@ -58,24 +74,9 @@ func manageArguments() { os.Exit(1) } - // use environment variable if set, otherwise use flag value - if envHost := os.Getenv("GITLAB_HOSTNAME"); envHost != "" { - gitlabHost = envHost + // add slash 🎩🎸 if not provided + if !strings.HasSuffix(repoDestinationPre, "/") { + repoDestinationPre += "/" } - if envToken := os.Getenv("GITLAB_API_TOKEN"); envToken != "" { - gitlabToken = envToken - } - - if gitlabHost == "" { - fmt.Println("Fatal: No GitLab Host found.") - flag.PrintDefaults() - os.Exit(1) - } - - if gitlabToken == "" { - fmt.Println("Fatal: No GitLab API Token found.") - flag.PrintDefaults() - os.Exit(1) - } } diff --git a/cmd/gogitlabber/main.go b/cmd/gogitlabber/main.go index 525abfd..07324e5 100644 --- a/cmd/gogitlabber/main.go +++ b/cmd/gogitlabber/main.go @@ -20,11 +20,6 @@ type Repository struct { func main() { - // environment variables < arguments - if err := loadEnvironmentVariables(); err != nil { - log.Fatalf("Error loading environment variables: %v", err) - } - // manage all argument magic manageArguments()