Skip to content

Commit 90959eb

Browse files
authored
feat: Add GetDurationVal to handle optional duration config from env (#1110)
Signed-off-by: Denis Karpelevich <[email protected]>
1 parent 72c1c3e commit 90959eb

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

registry-scanner/pkg/env/env.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log"
1011
)
@@ -35,6 +36,20 @@ func GetStringVal(envVar string, defaultValue string) string {
3536
}
3637
}
3738

39+
// GetDurationVal retrieves a time.Duration value from given environment envVar
40+
// Returns default value if envVar is not set or if the provided value is invalid.
41+
func GetDurationVal(envVar string, defaultValue time.Duration) time.Duration {
42+
if val := os.Getenv(envVar); val != "" {
43+
duration, err := time.ParseDuration(val)
44+
if err != nil {
45+
log.Warnf("Invalid duration format '%s' for environment variable '%s'. Using default value: %s", val, envVar, defaultValue)
46+
return defaultValue
47+
}
48+
return duration
49+
}
50+
return defaultValue
51+
}
52+
3853
// Helper function to parse a number from an environment variable. Returns a
3954
// default if env is not set, is not parseable to a number, exceeds max (if
4055
// max is greater than 0) or is less than min.

registry-scanner/pkg/env/env_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package env
33
import (
44
"os"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
)
@@ -37,6 +38,29 @@ func Test_GetStringVal(t *testing.T) {
3738
})
3839
}
3940

41+
func Test_GetDurationVal(t *testing.T) {
42+
t.Run("Get duration value from existing env var", func(t *testing.T) {
43+
_ = os.Setenv("TEST_DURATION_VAL", "1m")
44+
defer os.Setenv("TEST_DURATION_VAL", "")
45+
assert.Equal(t, time.Minute, GetDurationVal("TEST_DURATION_VAL", 2*time.Minute))
46+
})
47+
t.Run("Get default value from non-existing env var", func(t *testing.T) {
48+
_ = os.Setenv("TEST_DURATION_VAL", "")
49+
defer os.Setenv("TEST_DURATION_VAL", "")
50+
assert.Equal(t, 2*time.Minute, GetDurationVal("TEST_DURATION_VAL", 2*time.Minute))
51+
})
52+
t.Run("Get default value for bad format env var", func(t *testing.T) {
53+
_ = os.Setenv("TEST_DURATION_VAL", "bad format")
54+
defer os.Setenv("TEST_DURATION_VAL", "")
55+
assert.Equal(t, 2*time.Minute, GetDurationVal("TEST_DURATION_VAL", 2*time.Minute))
56+
})
57+
t.Run("Get 0 duration value for 0 env var", func(t *testing.T) {
58+
_ = os.Setenv("TEST_DURATION_VAL", "0")
59+
defer os.Setenv("TEST_DURATION_VAL", "")
60+
assert.Equal(t, 0*time.Minute, GetDurationVal("TEST_DURATION_VAL", 2*time.Minute))
61+
})
62+
}
63+
4064
func Test_ParseNumFromEnv(t *testing.T) {
4165
t.Run("Get number from existing env var within range", func(t *testing.T) {
4266
_ = os.Setenv("TEST_NUM_VAL", "5")

0 commit comments

Comments
 (0)