|
| 1 | +package envvar |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | +) |
| 10 | + |
| 11 | +func String(n string, args ...string) (string, bool) { |
| 12 | + defaultValue := "" |
| 13 | + if len(args) > 0 { |
| 14 | + defaultValue = args[0] |
| 15 | + } |
| 16 | + |
| 17 | + str, ok := os.LookupEnv(n) |
| 18 | + if !ok { |
| 19 | + return defaultValue, false |
| 20 | + } |
| 21 | + |
| 22 | + return str, true |
| 23 | +} |
| 24 | + |
| 25 | +func Duration(n string, args ...time.Duration) (time.Duration, bool) { |
| 26 | + defaultValue := time.Duration(0) |
| 27 | + if len(args) > 0 { |
| 28 | + defaultValue = args[0] |
| 29 | + } |
| 30 | + |
| 31 | + str, ok := os.LookupEnv(n) |
| 32 | + if !ok { |
| 33 | + return defaultValue, false |
| 34 | + } |
| 35 | + |
| 36 | + du, err := time.ParseDuration(str) |
| 37 | + if err != nil { |
| 38 | + logrus.WithError(err).Errorf("can not parse env var %q as time.Duration, incorrect format", str) |
| 39 | + return defaultValue, false |
| 40 | + } |
| 41 | + |
| 42 | + return du, true |
| 43 | +} |
| 44 | + |
| 45 | +// Int64 returns the int64 value of the environment variable named n. |
| 46 | +func Int64(n string, args ...int64) (int64, bool) { |
| 47 | + defaultValue := int64(0) |
| 48 | + if len(args) > 0 { |
| 49 | + defaultValue = args[0] |
| 50 | + } |
| 51 | + |
| 52 | + str, ok := os.LookupEnv(n) |
| 53 | + if !ok { |
| 54 | + return defaultValue, false |
| 55 | + } |
| 56 | + |
| 57 | + num, err := strconv.ParseInt(str, 10, 64) |
| 58 | + if err != nil { |
| 59 | + logrus.WithError(err).Errorf("can not parse env var %q as int, incorrect format", str) |
| 60 | + return defaultValue, false |
| 61 | + } |
| 62 | + |
| 63 | + return num, true |
| 64 | +} |
| 65 | + |
| 66 | +func Int(n string, args ...int) (int, bool) { |
| 67 | + defaultValue := 0 |
| 68 | + if len(args) > 0 { |
| 69 | + defaultValue = args[0] |
| 70 | + } |
| 71 | + |
| 72 | + str, ok := os.LookupEnv(n) |
| 73 | + if !ok { |
| 74 | + return defaultValue, false |
| 75 | + } |
| 76 | + |
| 77 | + num, err := strconv.Atoi(str) |
| 78 | + if err != nil { |
| 79 | + logrus.WithError(err).Errorf("can not parse env var %q as int, incorrect format", str) |
| 80 | + return defaultValue, false |
| 81 | + } |
| 82 | + |
| 83 | + return num, true |
| 84 | +} |
| 85 | + |
| 86 | +func SetBool(n string, v *bool) bool { |
| 87 | + b, ok := Bool(n) |
| 88 | + if ok { |
| 89 | + *v = b |
| 90 | + } |
| 91 | + |
| 92 | + return ok |
| 93 | +} |
| 94 | + |
| 95 | +func Bool(n string, args ...bool) (bool, bool) { |
| 96 | + defaultValue := false |
| 97 | + if len(args) > 0 { |
| 98 | + defaultValue = args[0] |
| 99 | + } |
| 100 | + |
| 101 | + str, ok := os.LookupEnv(n) |
| 102 | + if !ok { |
| 103 | + return defaultValue, false |
| 104 | + } |
| 105 | + |
| 106 | + num, err := strconv.ParseBool(str) |
| 107 | + if err != nil { |
| 108 | + logrus.WithError(err).Errorf("can not parse env var %q as bool, incorrect format", str) |
| 109 | + return defaultValue, false |
| 110 | + } |
| 111 | + |
| 112 | + return num, true |
| 113 | +} |
0 commit comments