Skip to content

Commit a5fe4be

Browse files
committed
refactor(env): simplify package to only contain constant definitions
1 parent ab17503 commit a5fe4be

File tree

3 files changed

+16
-243
lines changed

3 files changed

+16
-243
lines changed

internal/env/env.go

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,16 @@
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.
22
package env
33

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"
457

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"
5010

51-
return intValue
52-
}
11+
// UpdateCassettes if set to "true" will trigger the cassettes to be recorded
12+
UpdateCassettes = "CLI_UPDATE_CASSETTES"
5313

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+
)

internal/env/env_test.go

Lines changed: 0 additions & 158 deletions
This file was deleted.

internal/namespaces/rdb/v1/custom_benchmark_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type benchmarkStats struct {
9595

9696
func newBenchmarkStats() *benchmarkStats {
9797
return &benchmarkStats{
98-
enabled: env.GetBool("CLI_BENCH_TRACE", false),
98+
enabled: os.Getenv(env.BenchTrace) == "true",
9999
timings: make([]time.Duration, 0, 1000),
100100
}
101101
}
@@ -248,7 +248,7 @@ func cleanupSharedInstance() {
248248
}
249249

250250
func BenchmarkInstanceGet(b *testing.B) {
251-
if !env.GetBool("CLI_RUN_BENCHMARKS", false) {
251+
if os.Getenv(env.RunBenchmarks) != "true" {
252252
b.Skip("Skipping benchmark. Set CLI_RUN_BENCHMARKS=true to run.")
253253
}
254254

@@ -284,7 +284,7 @@ func BenchmarkInstanceGet(b *testing.B) {
284284
}
285285

286286
func BenchmarkBackupGet(b *testing.B) {
287-
if !env.GetBool("CLI_RUN_BENCHMARKS", false) {
287+
if os.Getenv(env.RunBenchmarks) != "true" {
288288
b.Skip("Skipping benchmark. Set CLI_RUN_BENCHMARKS=true to run.")
289289
}
290290

@@ -353,7 +353,7 @@ func BenchmarkBackupGet(b *testing.B) {
353353
}
354354

355355
func BenchmarkBackupList(b *testing.B) {
356-
if !env.GetBool("CLI_RUN_BENCHMARKS", false) {
356+
if os.Getenv(env.RunBenchmarks) != "true" {
357357
b.Skip("Skipping benchmark. Set CLI_RUN_BENCHMARKS=true to run.")
358358
}
359359

@@ -435,7 +435,7 @@ func BenchmarkBackupList(b *testing.B) {
435435
}
436436

437437
func BenchmarkDatabaseList(b *testing.B) {
438-
if !env.GetBool("CLI_RUN_BENCHMARKS", false) {
438+
if os.Getenv(env.RunBenchmarks) != "true" {
439439
b.Skip("Skipping benchmark. Set CLI_RUN_BENCHMARKS=true to run.")
440440
}
441441

0 commit comments

Comments
 (0)