nurse/config/flags.go
Peter Kurfer df6f6d55cb
All checks were successful
agola/nurse/Test and lint The run finished successfully
fix(lint): clean linter issues
2022-09-22 14:04:53 +02:00

62 lines
1.2 KiB
Go

package config
import (
"flag"
"os"
"strconv"
"time"
)
const (
defaultCheckTimeout = 500 * time.Millisecond
defaultAttemptCount = 20
)
func ConfigureFlags(cfg *Nurse) *flag.FlagSet {
set := flag.NewFlagSet("nurse", flag.ContinueOnError)
set.DurationVar(
&cfg.CheckTimeout,
"check-timeout",
LookupEnvOr("NURSE_CHECK_TIMEOUT", defaultCheckTimeout, time.ParseDuration),
"Timeout when running checks",
)
set.UintVar(
&cfg.CheckAttempts,
"check-attempts",
LookupEnvOr("NURSE_CHECK_ATTEMPTS", defaultAttemptCount, parseUint),
"Number of attempts for a check",
)
return set
}
//nolint:ireturn // false positive
func LookupEnvOr[T any](envKey string, fallback T, parse func(envVal string) (T, error)) T {
envVal := os.Getenv(envKey)
if envVal == "" {
return fallback
}
if parsed, err := parse(envVal); err != nil {
return fallback
} else {
return parsed
}
}
//nolint:ireturn // false positive
func Identity[T any](in T) (T, error) {
return in, nil
}
func parseUint(val string) (uint, error) {
const baseDecimal = 10
const int32BitSize = 32
parsed, err := strconv.ParseUint(val, baseDecimal, int32BitSize)
if err != nil {
return 0, err
}
return uint(parsed), nil
}