feat: use flag package
This commit is contained in:
parent
ec230004e4
commit
72b172133a
2 changed files with 30 additions and 51 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -15,38 +16,24 @@ func loadEnvironmentVariables() error {
|
||||||
|
|
||||||
func manageArguments() {
|
func manageArguments() {
|
||||||
|
|
||||||
// require at least the destination argument
|
var archivedFlag = flag.String("archived", "excluded", "to include archived repositories (any|excluded|exclusive)\nenv = GOGITLABBER_ARCHIVED\n")
|
||||||
if len(os.Args) <= 1 {
|
var destinationFlag = flag.String("destination", "", "where to check the repositories out\nenv = GOGITLABBER_DESTINATION")
|
||||||
printUsage()
|
var tokenFlag = flag.String("gitlab-api-token", "", "gitlab api token; example glpat-xxxx\nenv = GITLAB_API_TOKEN")
|
||||||
os.Exit(1)
|
var hostFlag = flag.String("gitlab-url", "", "gitlab host; example gitlab.example.com\nenv = GITLAB_HOSTNAME")
|
||||||
}
|
|
||||||
|
|
||||||
// parse arguments
|
flag.Parse()
|
||||||
for _, arg := range os.Args[1:] {
|
|
||||||
switch {
|
|
||||||
|
|
||||||
case strings.HasPrefix(arg, "--archived="):
|
// assign the parsed values to your variables
|
||||||
includeArchived = strings.TrimPrefix(arg, "--archived=")
|
includeArchived = *archivedFlag
|
||||||
|
repoDestinationPre = *destinationFlag
|
||||||
case strings.HasPrefix(arg, "--destination="):
|
gitlabToken = *tokenFlag
|
||||||
repoDestinationPre = strings.TrimPrefix(arg, "--destination=")
|
gitlabHost = *hostFlag
|
||||||
|
|
||||||
case strings.HasPrefix(arg, "--gitlab-api-token="):
|
|
||||||
gitlabToken = strings.TrimPrefix(arg, "--gitlab-api-token=")
|
|
||||||
|
|
||||||
case strings.HasPrefix(arg, "--gitlab-url="):
|
|
||||||
gitlabHost = strings.TrimPrefix(arg, "--gitlab-url=")
|
|
||||||
|
|
||||||
default:
|
|
||||||
printUsage()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fail if destination is unknown
|
// fail if destination is unknown
|
||||||
if repoDestinationPre == "" {
|
if repoDestinationPre == "" {
|
||||||
fmt.Println("Fatal: No destination found.")
|
fmt.Println("Fatal: No destination found.")
|
||||||
printUsage()
|
flag.PrintDefaults()
|
||||||
|
fmt.Println("")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,31 +43,39 @@ func manageArguments() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// --archive options:
|
// --archive options:
|
||||||
// - any (fetch both)
|
// - any (fetch both)
|
||||||
// - only (fetch archived only)
|
// - exclusive (fetch archived exclusive)
|
||||||
// - excluded (fetch non-archived only - default)
|
// - excluded (fetch non-archived exclusive - default)
|
||||||
if includeArchived == "" {
|
if includeArchived == "" {
|
||||||
includeArchived = "excluded"
|
includeArchived = "excluded"
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeArchived != "any" &&
|
if includeArchived != "any" &&
|
||||||
includeArchived != "only" &&
|
includeArchived != "exclusive" &&
|
||||||
includeArchived != "excluded" {
|
includeArchived != "excluded" {
|
||||||
fmt.Println("Fatal: Wrong archive option found.")
|
fmt.Println("Fatal: Wrong archive option found.")
|
||||||
printUsage()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify GitLab input
|
// 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 gitlabHost == "" {
|
if gitlabHost == "" {
|
||||||
fmt.Println("Fatal: No GitLab server configured.")
|
fmt.Println("Fatal: No GitLab Host found.")
|
||||||
printUsage()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gitlabToken == "" {
|
if gitlabToken == "" {
|
||||||
fmt.Println("Fatal: No GitLab API Token found.")
|
fmt.Println("Fatal: No GitLab API Token found.")
|
||||||
printUsage()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -39,18 +38,3 @@ func main() {
|
||||||
checkoutRepositories(repositories)
|
checkoutRepositories(repositories)
|
||||||
printPullerror(pullError)
|
printPullerror(pullError)
|
||||||
}
|
}
|
||||||
|
|
||||||
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=<supersecrettoken>")
|
|
||||||
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=<supersecrettoken>")
|
|
||||||
fmt.Println(" GITLAB_URL=gitlab.example.com")
|
|
||||||
fmt.Println("")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue