diff --git a/pkg/env/integersetting.go b/pkg/env/integersetting.go new file mode 100644 index 000000000..2ea0f1bdf --- /dev/null +++ b/pkg/env/integersetting.go @@ -0,0 +1,33 @@ +package env + +import ( + "strconv" +) + +// IntegerSetting represents an environment variable which should be parsed into an integer. +type IntegerSetting interface { + Setting + Int() int +} + +type integerSetting struct { + Setting + defaultValue int +} + +// Int returns the int object represented by the environment variable. +func (s *integerSetting) Int() int { + v, err := strconv.Atoi(s.Value()) + if err != nil { + return s.defaultValue + } + return v +} + +// RegisterIntegerSetting globally registers and returns a new integer setting. +func RegisterIntegerSetting(envVar string, defaultValue int, opts ...SettingOption) IntegerSetting { + return &integerSetting{ + Setting: RegisterSetting(envVar, append(opts, WithDefault(strconv.Itoa(defaultValue)))...), + defaultValue: defaultValue, + } +}