Clean up flag-related messages

This commit is contained in:
Tim Hockin 2024-06-08 14:10:53 -07:00
parent 750e20e6da
commit fbc717e620
No known key found for this signature in database
2 changed files with 44 additions and 44 deletions

16
env.go
View File

@ -60,7 +60,7 @@ func envBoolOrError(def bool, key string, alts ...string) (bool, error) {
if err == nil { if err == nil {
return parsed, nil return parsed, nil
} }
return false, fmt.Errorf("ERROR: invalid bool env %s=%q: %w", key, val, err) return false, fmt.Errorf("invalid bool env %s=%q: %w", key, val, err)
} }
if val := os.Getenv(key); val != "" { if val := os.Getenv(key); val != "" {
@ -77,7 +77,7 @@ func envBoolOrError(def bool, key string, alts ...string) (bool, error) {
func envBool(def bool, key string, alts ...string) bool { func envBool(def bool, key string, alts ...string) bool {
val, err := envBoolOrError(def, key, alts...) val, err := envBoolOrError(def, key, alts...)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintf(os.Stderr, "FATAL: %v\n", err)
os.Exit(1) os.Exit(1)
return false return false
} }
@ -90,7 +90,7 @@ func envIntOrError(def int, key string, alts ...string) (int, error) {
if err == nil { if err == nil {
return int(parsed), nil return int(parsed), nil
} }
return 0, fmt.Errorf("ERROR: invalid int env %s=%q: %w", key, val, err) return 0, fmt.Errorf("invalid int env %s=%q: %w", key, val, err)
} }
if val := os.Getenv(key); val != "" { if val := os.Getenv(key); val != "" {
@ -107,7 +107,7 @@ func envIntOrError(def int, key string, alts ...string) (int, error) {
func envInt(def int, key string, alts ...string) int { func envInt(def int, key string, alts ...string) int {
val, err := envIntOrError(def, key, alts...) val, err := envIntOrError(def, key, alts...)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintf(os.Stderr, "FATAL: %v\n", err)
os.Exit(1) os.Exit(1)
return 0 return 0
} }
@ -120,7 +120,7 @@ func envFloatOrError(def float64, key string, alts ...string) (float64, error) {
if err == nil { if err == nil {
return parsed, nil return parsed, nil
} }
return 0, fmt.Errorf("ERROR: invalid float env %s=%q: %w", key, val, err) return 0, fmt.Errorf("invalid float env %s=%q: %w", key, val, err)
} }
if val := os.Getenv(key); val != "" { if val := os.Getenv(key); val != "" {
@ -137,7 +137,7 @@ func envFloatOrError(def float64, key string, alts ...string) (float64, error) {
func envFloat(def float64, key string, alts ...string) float64 { func envFloat(def float64, key string, alts ...string) float64 {
val, err := envFloatOrError(def, key, alts...) val, err := envFloatOrError(def, key, alts...)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintf(os.Stderr, "FATAL: %v\n", err)
os.Exit(1) os.Exit(1)
return 0 return 0
} }
@ -150,7 +150,7 @@ func envDurationOrError(def time.Duration, key string, alts ...string) (time.Dur
if err == nil { if err == nil {
return parsed, nil return parsed, nil
} }
return 0, fmt.Errorf("ERROR: invalid duration env %s=%q: %w", key, val, err) return 0, fmt.Errorf("invalid duration env %s=%q: %w", key, val, err)
} }
if val := os.Getenv(key); val != "" { if val := os.Getenv(key); val != "" {
@ -167,7 +167,7 @@ func envDurationOrError(def time.Duration, key string, alts ...string) (time.Dur
func envDuration(def time.Duration, key string, alts ...string) time.Duration { func envDuration(def time.Duration, key string, alts ...string) time.Duration {
val, err := envDurationOrError(def, key, alts...) val, err := envDurationOrError(def, key, alts...)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintf(os.Stderr, "FATAL: %v\n", err)
os.Exit(1) os.Exit(1)
return 0 return 0
} }

72
main.go
View File

@ -131,7 +131,7 @@ func main() {
if err == nil { if err == nil {
os.Exit(code) os.Exit(code)
} }
fmt.Fprintf(os.Stderr, "ERROR: unhandled pid1 error: %v\n", err) fmt.Fprintf(os.Stderr, "FATAL: unhandled pid1 error: %v\n", err)
os.Exit(127) os.Exit(127)
} }
@ -352,7 +352,7 @@ func main() {
} }
var absRoot absPath var absRoot absPath
if abs, err := absPath(*flRoot).Canonical(); err != nil { if abs, err := absPath(*flRoot).Canonical(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't absolutize --root: %v\n", err) fmt.Fprintf(os.Stderr, "FATAL: can't absolutize --root: %v\n", err)
os.Exit(1) os.Exit(1)
} else { } else {
absRoot = abs absRoot = abs
@ -370,7 +370,7 @@ func main() {
cmdRunner := cmd.NewRunner(log) cmdRunner := cmd.NewRunner(log)
if *flRepo == "" { if *flRepo == "" {
handleConfigError(log, true, "ERROR: --repo must be specified") fatalConfigError(log, true, "required flag: --repo must be specified")
} }
switch { switch {
@ -378,32 +378,32 @@ func main() {
// Back-compat // Back-compat
log.V(0).Info("setting --ref from deprecated --branch") log.V(0).Info("setting --ref from deprecated --branch")
*flRef = *flDeprecatedBranch *flRef = *flDeprecatedBranch
case *flDeprecatedRev != "": case *flDeprecatedRev != "" && *flDeprecatedBranch == "":
// Back-compat // Back-compat
log.V(0).Info("setting --ref from deprecated --rev") log.V(0).Info("setting --ref from deprecated --rev")
*flRef = *flDeprecatedRev *flRef = *flDeprecatedRev
case *flDeprecatedBranch != "" && *flDeprecatedRev != "": case *flDeprecatedBranch != "" && *flDeprecatedRev != "":
handleConfigError(log, true, "ERROR: can't set --ref from deprecated --branch and --rev") fatalConfigError(log, true, "deprecated flag combo: can't set --ref from deprecated --branch and --rev (one or the other is OK)")
} }
if *flRef == "" { if *flRef == "" {
handleConfigError(log, true, "ERROR: --ref must be specified") fatalConfigError(log, true, "required flag: --ref must be specified")
} }
if *flDepth < 0 { // 0 means "no limit" if *flDepth < 0 { // 0 means "no limit"
handleConfigError(log, true, "ERROR: --depth must be greater than or equal to 0") fatalConfigError(log, true, "invalid flag: --depth must be greater than or equal to 0")
} }
switch submodulesMode(*flSubmodules) { switch submodulesMode(*flSubmodules) {
case submodulesRecursive, submodulesShallow, submodulesOff: case submodulesRecursive, submodulesShallow, submodulesOff:
default: default:
handleConfigError(log, true, "ERROR: --submodules must be one of %q, %q, or %q", submodulesRecursive, submodulesShallow, submodulesOff) fatalConfigError(log, true, "invalid flag: --submodules must be one of %q, %q, or %q", submodulesRecursive, submodulesShallow, submodulesOff)
} }
switch *flGitGC { switch *flGitGC {
case gcAuto, gcAlways, gcAggressive, gcOff: case gcAuto, gcAlways, gcAggressive, gcOff:
default: default:
handleConfigError(log, true, "ERROR: --git-gc must be one of %q, %q, %q, or %q", gcAuto, gcAlways, gcAggressive, gcOff) fatalConfigError(log, true, "invalid flag: --git-gc must be one of %q, %q, %q, or %q", gcAuto, gcAlways, gcAggressive, gcOff)
} }
if *flDeprecatedDest != "" { if *flDeprecatedDest != "" {
@ -422,11 +422,11 @@ func main() {
*flPeriod = time.Duration(int(*flDeprecatedWait*1000)) * time.Millisecond *flPeriod = time.Duration(int(*flDeprecatedWait*1000)) * time.Millisecond
} }
if *flPeriod < 10*time.Millisecond { if *flPeriod < 10*time.Millisecond {
handleConfigError(log, true, "ERROR: --period must be at least 10ms") fatalConfigError(log, true, "invalid flag: --period must be at least 10ms")
} }
if *flDeprecatedChmod != 0 { if *flDeprecatedChmod != 0 {
handleConfigError(log, true, "ERROR: --change-permissions is no longer supported") fatalConfigError(log, true, "deprecated flag: --change-permissions is no longer supported")
} }
var syncSig syscall.Signal var syncSig syscall.Signal
@ -443,7 +443,7 @@ func main() {
} }
} }
if syncSig == 0 { if syncSig == 0 {
handleConfigError(log, true, "ERROR: --sync-on-signal must be a valid signal name or number") fatalConfigError(log, true, "invalid flag: --sync-on-signal must be a valid signal name or number")
} }
} }
@ -453,7 +453,7 @@ func main() {
*flSyncTimeout = time.Duration(*flDeprecatedTimeout) * time.Second *flSyncTimeout = time.Duration(*flDeprecatedTimeout) * time.Second
} }
if *flSyncTimeout < 10*time.Millisecond { if *flSyncTimeout < 10*time.Millisecond {
handleConfigError(log, true, "ERROR: --sync-timeout must be at least 10ms") fatalConfigError(log, true, "invalid flag: --sync-timeout must be at least 10ms")
} }
if *flDeprecatedMaxSyncFailures != 0 { if *flDeprecatedMaxSyncFailures != 0 {
@ -469,10 +469,10 @@ func main() {
} }
if *flExechookCommand != "" { if *flExechookCommand != "" {
if *flExechookTimeout < time.Second { if *flExechookTimeout < time.Second {
handleConfigError(log, true, "ERROR: --exechook-timeout must be at least 1s") fatalConfigError(log, true, "invalid flag: --exechook-timeout must be at least 1s")
} }
if *flExechookBackoff < time.Second { if *flExechookBackoff < time.Second {
handleConfigError(log, true, "ERROR: --exechook-backoff must be at least 1s") fatalConfigError(log, true, "invalid flag: --exechook-backoff must be at least 1s")
} }
} }
@ -482,60 +482,60 @@ func main() {
*flWebhookStatusSuccess = 0 *flWebhookStatusSuccess = 0
} }
if *flWebhookStatusSuccess < 0 { if *flWebhookStatusSuccess < 0 {
handleConfigError(log, true, "ERROR: --webhook-success-status must be a valid HTTP code or 0") fatalConfigError(log, true, "invalid flag: --webhook-success-status must be a valid HTTP code or 0")
} }
if *flWebhookTimeout < time.Second { if *flWebhookTimeout < time.Second {
handleConfigError(log, true, "ERROR: --webhook-timeout must be at least 1s") fatalConfigError(log, true, "invalid flag: --webhook-timeout must be at least 1s")
} }
if *flWebhookBackoff < time.Second { if *flWebhookBackoff < time.Second {
handleConfigError(log, true, "ERROR: --webhook-backoff must be at least 1s") fatalConfigError(log, true, "invalid flag: --webhook-backoff must be at least 1s")
} }
} }
if *flUsername != "" { if *flUsername != "" {
if *flPassword == "" && *flPasswordFile == "" { if *flPassword == "" && *flPasswordFile == "" {
handleConfigError(log, true, "ERROR: --password or --password-file must be specified when --username is specified") fatalConfigError(log, true, "required flag: --password or --password-file must be specified when --username is specified")
} }
if *flPassword != "" && *flPasswordFile != "" { if *flPassword != "" && *flPasswordFile != "" {
handleConfigError(log, true, "ERROR: only one of --password and --password-file may be specified") fatalConfigError(log, true, "invalid flag: only one of --password and --password-file may be specified")
} }
if u, err := url.Parse(*flRepo); err == nil { // it may not even parse as a URL, that's OK if u, err := url.Parse(*flRepo); err == nil { // it may not even parse as a URL, that's OK
if u.User != nil { if u.User != nil {
handleConfigError(log, true, "ERROR: credentials may not be specified in --repo when --username is specified") fatalConfigError(log, true, "invalid flag: credentials may not be specified in --repo when --username is specified")
} }
} }
} else { } else {
if *flPassword != "" { if *flPassword != "" {
handleConfigError(log, true, "ERROR: --password may only be specified when --username is specified") fatalConfigError(log, true, "invalid flag: --password may only be specified when --username is specified")
} }
if *flPasswordFile != "" { if *flPasswordFile != "" {
handleConfigError(log, true, "ERROR: --password-file may only be specified when --username is specified") fatalConfigError(log, true, "invalid flag: --password-file may only be specified when --username is specified")
} }
} }
if len(*flCredentials) > 0 { if len(*flCredentials) > 0 {
for _, cred := range *flCredentials { for _, cred := range *flCredentials {
if cred.URL == "" { if cred.URL == "" {
handleConfigError(log, true, "ERROR: --credential URL must be specified") fatalConfigError(log, true, "invalid flag: --credential URL must be specified")
} }
if cred.Username == "" { if cred.Username == "" {
handleConfigError(log, true, "ERROR: --credential username must be specified") fatalConfigError(log, true, "invalid flag: --credential username must be specified")
} }
if cred.Password == "" && cred.PasswordFile == "" { if cred.Password == "" && cred.PasswordFile == "" {
handleConfigError(log, true, "ERROR: --credential password or password-file must be set") fatalConfigError(log, true, "invalid flag: --credential password or password-file must be specified")
} }
if cred.Password != "" && cred.PasswordFile != "" { if cred.Password != "" && cred.PasswordFile != "" {
handleConfigError(log, true, "ERROR: only one of --credential password and password-file may be specified") fatalConfigError(log, true, "invalid flag: only one of --credential password and password-file may be specified")
} }
} }
} }
if *flHTTPBind == "" { if *flHTTPBind == "" {
if *flHTTPMetrics { if *flHTTPMetrics {
handleConfigError(log, true, "ERROR: --http-bind must be specified when --http-metrics is set") fatalConfigError(log, true, "required flag: --http-bind must be specified when --http-metrics is set")
} }
if *flHTTPprof { if *flHTTPprof {
handleConfigError(log, true, "ERROR: --http-bind must be specified when --http-pprof is set") fatalConfigError(log, true, "required flag: --http-bind must be specified when --http-pprof is set")
} }
} }
@ -552,7 +552,7 @@ func main() {
"flags", logSafeFlags(*flVerbose)) "flags", logSafeFlags(*flVerbose))
if _, err := exec.LookPath(*flGitCmd); err != nil { if _, err := exec.LookPath(*flGitCmd); err != nil {
log.Error(err, "ERROR: git executable not found", "git", *flGitCmd) log.Error(err, "FATAL: git executable not found", "git", *flGitCmd)
os.Exit(1) os.Exit(1)
} }
@ -568,13 +568,13 @@ func main() {
// very early so that we can normalize the path even when there are // very early so that we can normalize the path even when there are
// symlinks in play. // symlinks in play.
if err := os.MkdirAll(absRoot.String(), defaultDirMode); err != nil { if err := os.MkdirAll(absRoot.String(), defaultDirMode); err != nil {
log.Error(err, "ERROR: can't make root dir", "path", absRoot) log.Error(err, "FATAL: can't make root dir", "path", absRoot)
os.Exit(1) os.Exit(1)
} }
// Get rid of symlinks in the root path to avoid getting confused about // Get rid of symlinks in the root path to avoid getting confused about
// them later. The path must exist for EvalSymlinks to work. // them later. The path must exist for EvalSymlinks to work.
if delinked, err := filepath.EvalSymlinks(absRoot.String()); err != nil { if delinked, err := filepath.EvalSymlinks(absRoot.String()); err != nil {
log.Error(err, "ERROR: can't normalize root path", "path", absRoot) log.Error(err, "FATAL: can't normalize root path", "path", absRoot)
os.Exit(1) os.Exit(1)
} else { } else {
absRoot = absPath(delinked) absRoot = absPath(delinked)
@ -617,7 +617,7 @@ func main() {
if *flAddUser { if *flAddUser {
if err := addUser(); err != nil { if err := addUser(); err != nil {
log.Error(err, "ERROR: can't add user") log.Error(err, "FATAL: can't add user")
os.Exit(1) os.Exit(1)
} }
} }
@ -653,7 +653,7 @@ func main() {
// Don't pollute the user's .gitconfig if this is being run directly. // Don't pollute the user's .gitconfig if this is being run directly.
if f, err := os.CreateTemp("", "git-sync.gitconfig.*"); err != nil { if f, err := os.CreateTemp("", "git-sync.gitconfig.*"); err != nil {
log.Error(err, "ERROR: can't create gitconfig file") log.Error(err, "FATAL: can't create gitconfig file")
os.Exit(1) os.Exit(1)
} else { } else {
gitConfig := f.Name() gitConfig := f.Name()
@ -1062,10 +1062,10 @@ func sleepForever() {
os.Exit(0) os.Exit(0)
} }
// handleConfigError prints the error to the standard error, prints the usage // fatalConfigError prints the error to the standard error, prints the usage
// if the `printUsage` flag is true, exports the error to the error file and // if the `printUsage` flag is true, exports the error to the error file and
// exits the process with the exit code. // exits the process with the exit code.
func handleConfigError(log *logging.Logger, printUsage bool, format string, a ...interface{}) { func fatalConfigError(log *logging.Logger, printUsage bool, format string, a ...interface{}) {
s := fmt.Sprintf(format, a...) s := fmt.Sprintf(format, a...)
fmt.Fprintln(os.Stderr, s) fmt.Fprintln(os.Stderr, s)
if printUsage { if printUsage {