11package envutil
22
33import (
4- "os"
5- "strconv"
6- "strings"
4+ "os"
5+ "strconv"
6+ "strings"
77)
88
99// GetEnvString returns the value of the environment variable specified by envKey.
1010// If the environment variable is not set or is empty, it returns the defaultValue.
1111func GetEnvString (envKey , defaultValue string ) string {
12- if value := os .Getenv (envKey ); value != "" {
13- return value
14- }
15- return defaultValue
12+ if value := os .Getenv (envKey ); value != "" {
13+ return value
14+ }
15+ return defaultValue
1616}
1717
1818// GetEnvInt returns the integer value of the environment variable specified by envKey.
1919// If the environment variable is not set, is empty, cannot be parsed as an integer,
2020// or is not positive (> 0), it returns the defaultValue.
2121// This function validates that the value is a positive integer.
2222func GetEnvInt (envKey string , defaultValue int ) int {
23- if envValue := os .Getenv (envKey ); envValue != "" {
24- if value , err := strconv .Atoi (envValue ); err == nil && value > 0 {
25- return value
26- }
27- }
28- return defaultValue
23+ if envValue := os .Getenv (envKey ); envValue != "" {
24+ if value , err := strconv .Atoi (envValue ); err == nil && value > 0 {
25+ return value
26+ }
27+ }
28+ return defaultValue
2929}
3030
3131// GetEnvBool returns the boolean value of the environment variable specified by envKey.
@@ -34,13 +34,13 @@ return defaultValue
3434// Falsy values (case-insensitive): "0", "false", "no", "off"
3535// Any other value returns the defaultValue.
3636func GetEnvBool (envKey string , defaultValue bool ) bool {
37- if envValue := os .Getenv (envKey ); envValue != "" {
38- switch strings .ToLower (envValue ) {
39- case "1" , "true" , "yes" , "on" :
40- return true
41- case "0" , "false" , "no" , "off" :
42- return false
43- }
44- }
45- return defaultValue
37+ if envValue := os .Getenv (envKey ); envValue != "" {
38+ switch strings .ToLower (envValue ) {
39+ case "1" , "true" , "yes" , "on" :
40+ return true
41+ case "0" , "false" , "no" , "off" :
42+ return false
43+ }
44+ }
45+ return defaultValue
4646}
0 commit comments