|
1 | 1 | package env |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
| 6 | + "reflect" |
5 | 7 | "strconv" |
| 8 | + "time" |
6 | 9 |
|
7 | 10 | "github.com/go-logr/logr" |
8 | 11 | ) |
9 | 12 |
|
10 | | -// getEnvFloat gets a float64 from an environment variable with a default value |
11 | | -func GetEnvFloat(key string, defaultVal float64, logger logr.Logger) float64 { |
12 | | - val, exists := os.LookupEnv(key) |
13 | | - if !exists { |
14 | | - logger.Info("Environment variable not set, using default value", |
15 | | - "key", key, "defaultValue", defaultVal) |
16 | | - return defaultVal |
17 | | - } |
18 | | - |
19 | | - floatVal, err := strconv.ParseFloat(val, 64) |
| 13 | +// parseEnvWithValue attempts to parse a string value using the provided |
| 14 | +// parser. |
| 15 | +// It logs success or failure and returns the parsed value or the default |
| 16 | +// value. |
| 17 | +// This helper is used when the environment variable is confirmed to exist. |
| 18 | +func parseEnvWithValue[T any](key string, valueStr string, defaultVal T, |
| 19 | + parser func(string) (T, error), logger logr.Logger) T { |
| 20 | + parsedVal, err := parser(valueStr) |
20 | 21 | if err != nil { |
21 | | - logger.Info("Failed to parse environment variable as float, using default value", |
22 | | - "key", key, "value", val, "error", err, "defaultValue", defaultVal) |
| 22 | + logger.Info(fmt.Sprintf("Failed to parse environment variable as %s, using default value", reflect.TypeOf(defaultVal)), |
| 23 | + "key", key, "rawValue", valueStr, "error", err, |
| 24 | + "defaultValue", defaultVal) |
23 | 25 | return defaultVal |
24 | 26 | } |
25 | 27 |
|
26 | 28 | logger.Info("Successfully loaded environment variable", |
27 | | - "key", key, "value", floatVal) |
28 | | - return floatVal |
| 29 | + "key", key, "value", parsedVal) |
| 30 | + return parsedVal |
29 | 31 | } |
30 | 32 |
|
31 | | -// getEnvInt gets an int from an environment variable with a default value |
32 | | -func GetEnvInt(key string, defaultVal int, logger logr.Logger) int { |
33 | | - val, exists := os.LookupEnv(key) |
| 33 | +// getEnvWithParser retrieves an environment variable. If set, it uses the |
| 34 | +// provided parser to convert it. Otherwise, it returns the default value. |
| 35 | +// It delegates to parseEnvWithValue for the actual parsing and detailed |
| 36 | +// logging once the variable is confirmed to exist. |
| 37 | +func getEnvWithParser[T any](key string, defaultVal T, |
| 38 | + parser func(string) (T, error), logger logr.Logger) T { |
| 39 | + valueStr, exists := os.LookupEnv(key) |
34 | 40 | if !exists { |
35 | 41 | logger.Info("Environment variable not set, using default value", |
36 | 42 | "key", key, "defaultValue", defaultVal) |
37 | 43 | return defaultVal |
38 | 44 | } |
| 45 | + return parseEnvWithValue(key, valueStr, defaultVal, parser, logger) |
| 46 | +} |
39 | 47 |
|
40 | | - intVal, err := strconv.Atoi(val) |
41 | | - if err != nil { |
42 | | - logger.Info("Failed to parse environment variable as int, using default value", |
43 | | - "key", key, "value", val, "error", err, "defaultValue", defaultVal) |
44 | | - return defaultVal |
45 | | - } |
| 48 | +// GetEnvFloat gets a float64 from an environment variable with a default |
| 49 | +// value. |
| 50 | +func GetEnvFloat(key string, defaultVal float64, logger logr.Logger) float64 { |
| 51 | + parser := func(s string) (float64, error) { return strconv.ParseFloat(s, 64) } |
| 52 | + return getEnvWithParser(key, defaultVal, parser, logger) |
| 53 | +} |
46 | 54 |
|
47 | | - logger.Info("Successfully loaded environment variable", |
48 | | - "key", key, "value", intVal) |
49 | | - return intVal |
| 55 | +// GetEnvInt gets an int from an environment variable with a default value. |
| 56 | +func GetEnvInt(key string, defaultVal int, logger logr.Logger) int { |
| 57 | + return getEnvWithParser(key, defaultVal, strconv.Atoi, logger) |
| 58 | +} |
| 59 | + |
| 60 | +// GetEnvDuration gets a time.Duration from an environment variable with a |
| 61 | +// default value. |
| 62 | +func GetEnvDuration(key string, defaultVal time.Duration, logger logr.Logger) time.Duration { |
| 63 | + return getEnvWithParser(key, defaultVal, time.ParseDuration, logger) |
50 | 64 | } |
51 | 65 |
|
52 | | -// GetEnvString gets a string from an environment variable with a default value |
| 66 | +// GetEnvString gets a string from an environment variable with a default |
| 67 | +// value. |
53 | 68 | func GetEnvString(key string, defaultVal string, logger logr.Logger) string { |
54 | | - val, exists := os.LookupEnv(key) |
55 | | - if !exists { |
56 | | - logger.Info("Environment variable not set, using default value", |
57 | | - "key", key, "defaultValue", defaultVal) |
58 | | - return defaultVal |
59 | | - } |
60 | | - return val |
| 69 | + parser := func(s string) (string, error) { return s, nil } |
| 70 | + return getEnvWithParser(key, defaultVal, parser, logger) |
61 | 71 | } |
0 commit comments