41 lines
783 B
Go
41 lines
783 B
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const defaultCheckTimeout = 500 * time.Millisecond
|
|
|
|
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",
|
|
)
|
|
|
|
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
|
|
}
|