fix: config flags > environment variables > defaults
This commit is contained in:
parent
10a6ed6df3
commit
49f65abe4a
1 changed files with 78 additions and 86 deletions
|
|
@ -2,48 +2,100 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// set default values and override values from environment variables
|
||||||
|
func setDefaultsFromEnv() {
|
||||||
|
|
||||||
|
// set default values
|
||||||
|
debug = false
|
||||||
|
concurrency = 15
|
||||||
|
gitlabHost = "gitlab.com"
|
||||||
|
gitlabToken = ""
|
||||||
|
includeArchived = "excluded"
|
||||||
|
repoDestinationPre = "$HOME/Documents"
|
||||||
|
|
||||||
|
// override with environment variables if present
|
||||||
|
if envDebug := os.Getenv("GOGITLABBER_DEBUG"); envDebug != "" {
|
||||||
|
if debugVal, err := strconv.ParseBool(envDebug); err == nil {
|
||||||
|
debug = debugVal
|
||||||
|
} else {
|
||||||
|
logPrint("Warning: Invalid debug value in environment, using default", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if envToken := os.Getenv("GITLAB_API_TOKEN"); envToken != "" {
|
||||||
|
gitlabToken = envToken
|
||||||
|
}
|
||||||
|
|
||||||
|
if envHost := os.Getenv("GITLAB_URL"); envHost != "" {
|
||||||
|
gitlabHost = envHost
|
||||||
|
}
|
||||||
|
|
||||||
|
if envRepoDest := os.Getenv("GOGITLABBER_DESTINATION"); envRepoDest != "" {
|
||||||
|
repoDestinationPre = envRepoDest
|
||||||
|
}
|
||||||
|
|
||||||
|
if envConcurrency := os.Getenv("GOGITLABBER_CONCURRENCY"); envConcurrency != "" {
|
||||||
|
if concurrencyVal, err := strconv.Atoi(envConcurrency); err == nil {
|
||||||
|
concurrency = concurrencyVal
|
||||||
|
} else {
|
||||||
|
logPrint("Warning: Invalid concurrency value in environment, using default", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if envArchived := os.Getenv("GOGITLABBER_ARCHIVED"); envArchived != "" {
|
||||||
|
switch envArchived {
|
||||||
|
case "any", "exclusive", "excluded":
|
||||||
|
includeArchived = envArchived
|
||||||
|
default:
|
||||||
|
logPrint("Warning: Invalid archived value in environment, using default", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func manageArguments() {
|
func manageArguments() {
|
||||||
|
|
||||||
// configuration vars
|
// set defaults from environment variables
|
||||||
|
setDefaultsFromEnv()
|
||||||
|
|
||||||
|
// define flags (which will override environment variables)
|
||||||
var archivedFlag = flag.String(
|
var archivedFlag = flag.String(
|
||||||
"archived",
|
"archived",
|
||||||
"excluded",
|
includeArchived,
|
||||||
"To include archived repositories (any|excluded|exclusive)\n example: -archived=any\nenv = GOGITLABBER_ARCHIVED\n")
|
"To include archived repositories (any|excluded|exclusive)\n example: -archived=any\nenv = GOGITLABBER_ARCHIVED\n")
|
||||||
|
|
||||||
var concurrencyFlag = flag.Int(
|
var concurrencyFlag = flag.Int(
|
||||||
"concurrency",
|
"concurrency",
|
||||||
15,
|
concurrency,
|
||||||
"Specify repository concurrency\n example: -concurrency=15\nenv = GOGITLABBER_CONCURRENCY\n")
|
"Specify repository concurrency\n example: -concurrency=15\nenv = GOGITLABBER_CONCURRENCY\n")
|
||||||
|
|
||||||
var destinationFlag = flag.String(
|
var destinationFlag = flag.String(
|
||||||
"destination",
|
"destination",
|
||||||
"$HOME/Documents",
|
repoDestinationPre,
|
||||||
"Specify where to check the repositories out\n example: -destination=$HOME/repos\nenv = GOGITLABBER_DESTINATION\n")
|
"Specify where to check the repositories out\n example: -destination=$HOME/repos\nenv = GOGITLABBER_DESTINATION\n")
|
||||||
|
|
||||||
var hostFlag = flag.String(
|
var hostFlag = flag.String(
|
||||||
"gitlab-url",
|
"gitlab-url",
|
||||||
"gitlab.com",
|
gitlabHost,
|
||||||
"Specify GitLab host\n example: -gitlab-url=gitlab.com\nenv = GITLAB_URL\n")
|
"Specify GitLab host\n example: -gitlab-url=gitlab.com\nenv = GITLAB_URL\n")
|
||||||
|
|
||||||
var tokenFlag = flag.String(
|
var tokenFlag = flag.String(
|
||||||
"gitlab-api-token",
|
"gitlab-api-token",
|
||||||
"",
|
gitlabToken,
|
||||||
"Specify GitLab API token\n example: -gitlab-api=glpat-xxxx\nenv = GITLAB_API_TOKEN\n")
|
"Specify GitLab API token\n example: -gitlab-api=glpat-xxxx\nenv = GITLAB_API_TOKEN\n")
|
||||||
|
|
||||||
var debugFlag = flag.Bool(
|
var debugFlag = flag.Bool(
|
||||||
"debug",
|
"debug",
|
||||||
false,
|
debug,
|
||||||
"Toggle debug mode\n example: -debug=true\nenv = GOGITLABBER_DEBUG\n")
|
"Toggle debug mode\n example: -debug=true\nenv = GOGITLABBER_DEBUG\n")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// assign the parsed values to your variables
|
// Override with flag values (higher precedence)
|
||||||
concurrency = *concurrencyFlag
|
concurrency = *concurrencyFlag
|
||||||
gitlabHost = *hostFlag
|
gitlabHost = *hostFlag
|
||||||
gitlabToken = *tokenFlag
|
gitlabToken = *tokenFlag
|
||||||
|
|
@ -51,91 +103,31 @@ func manageArguments() {
|
||||||
repoDestinationPre = *destinationFlag
|
repoDestinationPre = *destinationFlag
|
||||||
debug = *debugFlag
|
debug = *debugFlag
|
||||||
|
|
||||||
// manage verosity option
|
|
||||||
switch envDebug := os.Getenv("GOGITLABBER_DEBUG"); {
|
|
||||||
case envDebug != "":
|
|
||||||
var err error
|
|
||||||
debug, err = strconv.ParseBool(envDebug)
|
|
||||||
logPrint("Configuration: debug option found", nil)
|
|
||||||
if err != nil {
|
|
||||||
logFatal("FATAL: config; not a valid bool", nil)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
debug = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// manage gitlab api option
|
|
||||||
switch envToken := os.Getenv("GITLAB_API_TOKEN"); {
|
|
||||||
case envToken != "":
|
|
||||||
gitlabToken = envToken
|
|
||||||
logPrint("Configuration: Gitlab API Token found", nil)
|
|
||||||
default:
|
|
||||||
flag.Usage()
|
|
||||||
logFatal("Configuration: Giltab API Token not found", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// manage gitlab url option
|
|
||||||
switch envHost := os.Getenv("GITLAB_URL"); {
|
|
||||||
case envHost != "":
|
|
||||||
gitlabHost = envHost
|
|
||||||
logPrint("Configuration: Gitlab host found", nil)
|
|
||||||
default:
|
|
||||||
flag.Usage()
|
|
||||||
logFatal("Configuration: Gitlab host not found", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// manage destination option
|
|
||||||
switch envRepoDest := os.Getenv("GOGITLABBER_DESTINATION"); {
|
|
||||||
case envRepoDest != "":
|
|
||||||
repoDestinationPre = envRepoDest
|
|
||||||
logPrint("Configuration: destination found", nil)
|
|
||||||
default:
|
|
||||||
flag.Usage()
|
|
||||||
logFatal("Configuration: destination not found", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// add slash 🎩🎸 if not provided
|
// add slash 🎩🎸 if not provided
|
||||||
switch {
|
if !strings.HasSuffix(repoDestinationPre, "/") {
|
||||||
case !strings.HasSuffix(repoDestinationPre, "/"):
|
|
||||||
repoDestinationPre += "/"
|
repoDestinationPre += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage concurrency option
|
// validate required parameters
|
||||||
switch envConcurrency := os.Getenv("GOGITLABBER_CONCURRENCY"); {
|
if gitlabToken == "" {
|
||||||
case envConcurrency == "":
|
|
||||||
concurrency = 15
|
|
||||||
case envConcurrency != "":
|
|
||||||
concurrencyValue, err := strconv.Atoi(envConcurrency)
|
|
||||||
if err != nil {
|
|
||||||
logFatal("invalid concurrency value found in environment: %v", err)
|
|
||||||
}
|
|
||||||
concurrency = concurrencyValue
|
|
||||||
logPrint("Configuration: concurrency option found", nil)
|
|
||||||
default:
|
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
log.Fatalln("FATAL: config; concurrency not found")
|
logFatal("Configuration: Gitlab API Token not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// manage archived option
|
// validate archived option
|
||||||
switch envArchived := os.Getenv("GOGITLABBER_ARCHIVED"); {
|
switch includeArchived {
|
||||||
case envArchived == "":
|
case "any", "exclusive", "excluded":
|
||||||
includeArchived = "excluded"
|
|
||||||
logPrint("Configuration: archive option found", nil)
|
|
||||||
|
|
||||||
case envArchived == "any":
|
|
||||||
includeArchived = envArchived
|
|
||||||
logPrint("Configuration: archive option found", nil)
|
|
||||||
|
|
||||||
case envArchived == "exclusive":
|
|
||||||
includeArchived = envArchived
|
|
||||||
logPrint("Configuration: archive option found", nil)
|
|
||||||
|
|
||||||
case envArchived == "excluded":
|
|
||||||
includeArchived = envArchived
|
|
||||||
logPrint("Configuration: archive option found", nil)
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
logFatal("FATAL: Configuration: no or wrong archive option found", nil)
|
logFatal("Configuration: Invalid archive option: "+includeArchived, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log configuration
|
||||||
|
logPrint("Configuration: Using GitLab host: "+gitlabHost, nil)
|
||||||
|
logPrint("Configuration: Using destination: "+repoDestinationPre, nil)
|
||||||
|
logPrint("Configuration: Using concurrency: "+strconv.Itoa(concurrency), nil)
|
||||||
|
logPrint("Configuration: Using archived option: "+includeArchived, nil)
|
||||||
|
if debug {
|
||||||
|
logPrint("Configuration: Debug mode enabled", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue