|
1 | | -// Package env provides utilities for reading environment variables with defaults and type safety. |
| 1 | +// Package env contains a list of environment variables used to modify the behaviour of the CLI. |
2 | 2 | package env |
3 | 3 |
|
4 | | -import ( |
5 | | - "os" |
6 | | - "strconv" |
7 | | - "time" |
8 | | -) |
9 | | - |
10 | | -// GetBool reads a boolean environment variable. |
11 | | -// Returns defaultValue if the variable is not set or invalid. |
12 | | -func GetBool(key string, defaultValue bool) bool { |
13 | | - value := os.Getenv(key) |
14 | | - if value == "" { |
15 | | - return defaultValue |
16 | | - } |
17 | | - |
18 | | - switch value { |
19 | | - case "1", "true", "TRUE", "True", "yes", "YES", "Yes": |
20 | | - return true |
21 | | - case "0", "false", "FALSE", "False", "no", "NO", "No": |
22 | | - return false |
23 | | - default: |
24 | | - return defaultValue |
25 | | - } |
26 | | -} |
27 | | - |
28 | | -// GetString reads a string environment variable. |
29 | | -// Returns defaultValue if the variable is not set. |
30 | | -func GetString(key string, defaultValue string) string { |
31 | | - value := os.Getenv(key) |
32 | | - if value == "" { |
33 | | - return defaultValue |
34 | | - } |
35 | | - return value |
36 | | -} |
37 | | - |
38 | | -// GetInt reads an integer environment variable. |
39 | | -// Returns defaultValue if the variable is not set or invalid. |
40 | | -func GetInt(key string, defaultValue int) int { |
41 | | - value := os.Getenv(key) |
42 | | - if value == "" { |
43 | | - return defaultValue |
44 | | - } |
| 4 | +const ( |
| 5 | + // RunBenchmarks if set to "true" will enable benchmark execution |
| 6 | + RunBenchmarks = "CLI_RUN_BENCHMARKS" |
45 | 7 |
|
46 | | - intValue, err := strconv.Atoi(value) |
47 | | - if err != nil { |
48 | | - return defaultValue |
49 | | - } |
| 8 | + // BenchTrace if set to "true" will enable detailed benchmark statistics (min, median, mean, p95, max) |
| 9 | + BenchTrace = "CLI_BENCH_TRACE" |
50 | 10 |
|
51 | | - return intValue |
52 | | -} |
| 11 | + // UpdateCassettes if set to "true" will trigger the cassettes to be recorded |
| 12 | + UpdateCassettes = "CLI_UPDATE_CASSETTES" |
53 | 13 |
|
54 | | -// GetDuration reads a duration environment variable. |
55 | | -// Accepts values like "5s", "10m", "1h30m". |
56 | | -// Returns defaultValue if the variable is not set or invalid. |
57 | | -func GetDuration(key string, defaultValue time.Duration) time.Duration { |
58 | | - value := os.Getenv(key) |
59 | | - if value == "" { |
60 | | - return defaultValue |
61 | | - } |
62 | | - |
63 | | - duration, err := time.ParseDuration(value) |
64 | | - if err != nil { |
65 | | - return defaultValue |
66 | | - } |
67 | | - |
68 | | - return duration |
69 | | -} |
70 | | - |
71 | | -// IsSet returns true if the environment variable is set (even if empty). |
72 | | -func IsSet(key string) bool { |
73 | | - _, exists := os.LookupEnv(key) |
74 | | - return exists |
75 | | -} |
76 | | - |
77 | | -// MustGetString reads a string environment variable and panics if not set. |
78 | | -// Use this for required configuration values. |
79 | | -func MustGetString(key string) string { |
80 | | - value := os.Getenv(key) |
81 | | - if value == "" { |
82 | | - panic("environment variable " + key + " is required but not set") |
83 | | - } |
84 | | - return value |
85 | | -} |
| 14 | + // UpdateGoldens if set to "true" will update golden files during tests |
| 15 | + UpdateGoldens = "CLI_UPDATE_GOLDENS" |
| 16 | +) |
0 commit comments