Adds a GetEnvIntWithRange utility function (#102)

* Adds a GetEnvIntWithRange utility function

Signed-off-by: Elena Kolevska <elena@kolevska.com>

* Fixes linter

Signed-off-by: Elena Kolevska <elena@kolevska.com>

* Update to using time duration instead of int for seconds

Signed-off-by: Elena Kolevska <elena@kolevska.com>

---------

Signed-off-by: Elena Kolevska <elena@kolevska.com>
This commit is contained in:
Elena Kolevska 2024-09-09 22:50:17 +01:00 committed by GitHub
parent 502671bade
commit 3823663aa4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 112 additions and 0 deletions

29
utils/env.go Normal file
View File

@ -0,0 +1,29 @@
package utils
import (
"fmt"
"os"
"time"
)
// GetEnvDurationWithRange returns the time.Duration value of the environment variable specified by `envVar`.
// If the environment variable is not set, it returns `defaultValue`.
// If the value is set but is not valid (not a valid time.Duration or falls outside the specified range
// [minValue, maxValue] inclusively), it returns `defaultValue` and an error.
func GetEnvDurationWithRange(envVar string, defaultValue, min, max time.Duration) (time.Duration, error) {
v := os.Getenv(envVar)
if v == "" {
return defaultValue, nil
}
val, err := time.ParseDuration(v)
if err != nil {
return defaultValue, fmt.Errorf("invalid time.Duration value %s for the %s env variable: %w", val, envVar, err)
}
if val < min || val > max {
return defaultValue, fmt.Errorf("invalid value for the %s env variable: value should be between %s and %s, got %s", envVar, min, max, val)
}
return val, nil
}

83
utils/env_test.go Normal file
View File

@ -0,0 +1,83 @@
package utils
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGetEnvIntWithRangeWrongValues(t *testing.T) {
testValues := []struct {
name string
envVarVal string
min time.Duration
max time.Duration
error string
}{
{
"should error if value is not a valid time.Duration",
"0.5",
time.Second,
2 * time.Second,
"invalid time.Duration value 0s for the MY_ENV env variable",
},
{
"should error if value is lower than 1s",
"0s",
time.Second,
10 * time.Second,
"value should be between 1s and 10s",
},
{
"should error if value is higher than 10s",
"2m",
time.Second,
10 * time.Second,
"value should be between 1s and 10s",
},
}
defaultValue := 3 * time.Second
for _, tt := range testValues {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("MY_ENV", tt.envVarVal)
val, err := GetEnvDurationWithRange("MY_ENV", defaultValue, tt.min, tt.max)
require.Error(t, err)
require.Contains(t, err.Error(), tt.error)
require.Equal(t, defaultValue, val)
})
}
}
func TestGetEnvDurationWithRangeValidValues(t *testing.T) {
testValues := []struct {
name string
envVarVal string
result time.Duration
}{
{
"should return default value if env variable is not set",
"",
3 * time.Second,
},
{
"should return result is env variable value is valid",
"4s",
4 * time.Second,
},
}
for _, tt := range testValues {
t.Run(tt.name, func(t *testing.T) {
if tt.envVarVal != "" {
t.Setenv("MY_ENV", tt.envVarVal)
}
val, err := GetEnvDurationWithRange("MY_ENV", 3*time.Second, time.Second, 5*time.Second)
require.NoError(t, err)
require.Equal(t, tt.result, val)
})
}
}