2022-05-08 09:00:22 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
2022-08-07 10:22:21 +00:00
|
|
|
"strconv"
|
2022-05-08 09:00:22 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
const (
|
|
|
|
defaultCheckTimeout = 500 * time.Millisecond
|
|
|
|
defaultAttemptCount = 20
|
|
|
|
)
|
2022-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
set.UintVar(
|
|
|
|
&cfg.CheckAttempts,
|
|
|
|
"check-attempts",
|
|
|
|
LookupEnvOr("NURSE_CHECK_ATTEMPTS", defaultAttemptCount, parseUint),
|
|
|
|
"Number of attempts for a check",
|
|
|
|
)
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:40:32 +00:00
|
|
|
//nolint:ireturn // false positive
|
2022-05-08 09:00:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:40:32 +00:00
|
|
|
//nolint:ireturn // false positive
|
2022-05-08 09:00:22 +00:00
|
|
|
func Identity[T any](in T) (T, error) {
|
|
|
|
return in, nil
|
|
|
|
}
|
2022-08-07 10:22:21 +00:00
|
|
|
|
|
|
|
func parseUint(val string) (uint, error) {
|
|
|
|
parsed, err := strconv.ParseUint(val, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint(parsed), nil
|
|
|
|
}
|