11package env
22
33import (
4+ "errors"
45 "os"
56 "strconv"
7+ )
8+
9+ type Environment string
610
7- "github.com/joho/godotenv"
11+ const (
12+ EnvironmentProd Environment = "prod"
13+ EnvironmentStaging Environment = "staging"
14+ EnvironmentDev Environment = "dev"
15+ EnvironmentLocal Environment = ""
816)
917
10- func init () {
11- _ = godotenv .Load (".env" )
18+ func (e Environment ) String () string {
19+ if e == EnvironmentLocal {
20+ return "local"
21+ } else {
22+ return string (e )
23+ }
1224}
1325
14- // IsDebug returns true if the DEBUG environment variable is set to true
15- func IsDebug () bool {
16- return GetWithFallback ("DEBUG" , false )
26+ var env Environment
27+ var envSet bool
28+
29+ func SetEnvironment (e Environment ) {
30+ env = e
31+ envSet = true
32+ }
33+
34+ func GetEnvironment () Environment {
35+ if envSet {
36+ return env
37+ }
38+ e , _ := GetWithFallback ("ENVIRONMENT" , string (EnvironmentLocal ))
39+ return Environment (e )
1740}
1841
19- // GetOrFail returns the value of the environment variable or panics if it is not set
20- func GetOrFail [
42+ // GetOrFail returns the value of the environment variable or returns an error if it is not set
43+ func GetOrFail [ //NOSONAR
2144 T string |
2245 bool |
2346 uintptr |
2447 int | int64 | int32 | int16 | int8 |
2548 uint | uint64 | uint32 | uint16 | uint8 |
2649 float64 | float32 |
27- complex128 | complex64 ](env string ) T {
50+ complex128 | complex64 ](env string ) ( T , error ) {
2851 str := os .Getenv (env )
2952 if str == "" {
30- panic ("missing env variable: " + env )
53+ return * new ( T ), errors . New ("missing env variable: " + env )
3154 }
3255
3356 var val any
@@ -38,104 +61,104 @@ func GetOrFail[
3861 case bool :
3962 b , err := strconv .ParseBool (str )
4063 if err != nil {
41- panic ("invalid bool: " + env )
64+ return * new ( T ), errors . New ("invalid bool: " + env )
4265 }
4366 val = b
4467 case uintptr :
4568 u64 , err := strconv .ParseUint (str , 10 , 0 )
4669 if err != nil {
47- panic ("invalid uintptr: " + env )
70+ return * new ( T ), errors . New ("invalid uintptr: " + env )
4871 }
4972 val = uintptr (u64 )
5073 case int :
5174 i64 , err := strconv .ParseInt (str , 10 , 0 )
5275 if err != nil {
53- panic ("invalid int: " + env )
76+ return * new ( T ), errors . New ("invalid int: " + env )
5477 }
5578 val = int (i64 )
5679 case int64 :
5780 i64 , err := strconv .ParseInt (str , 10 , 64 )
5881 if err != nil {
59- panic ("invalid int64: " + env )
82+ return * new ( T ), errors . New ("invalid int64: " + env )
6083 }
6184 val = i64
6285 case int32 :
6386 i64 , err := strconv .ParseInt (str , 10 , 32 )
6487 if err != nil {
65- panic ("invalid int32: " + env )
88+ return * new ( T ), errors . New ("invalid int32: " + env )
6689 }
6790 val = int32 (i64 )
6891 case int16 :
6992 i64 , err := strconv .ParseInt (str , 10 , 16 )
7093 if err != nil {
71- panic ("invalid int16: " + env )
94+ return * new ( T ), errors . New ("invalid int16: " + env )
7295 }
7396 val = int16 (i64 )
7497 case int8 :
7598 i64 , err := strconv .ParseInt (str , 10 , 8 )
7699 if err != nil {
77- panic ("invalid int8: " + env )
100+ return * new ( T ), errors . New ("invalid int8: " + env )
78101 }
79102 val = int8 (i64 )
80103 case uint :
81104 i64 , err := strconv .ParseUint (str , 10 , 0 )
82105 if err != nil {
83- panic ("invalid uint: " + env )
106+ return * new ( T ), errors . New ("invalid uint: " + env )
84107 }
85108 val = uint (i64 )
86109 case uint64 :
87110 i64 , err := strconv .ParseUint (str , 10 , 64 )
88111 if err != nil {
89- panic ("invalid uint64: " + env )
112+ return * new ( T ), errors . New ("invalid uint64: " + env )
90113 }
91114 val = i64
92115 case uint32 :
93116 i64 , err := strconv .ParseUint (str , 10 , 32 )
94117 if err != nil {
95- panic ("invalid uint32: " + env )
118+ return * new ( T ), errors . New ("invalid uint32: " + env )
96119 }
97120 val = uint32 (i64 )
98121 case uint16 :
99122 i64 , err := strconv .ParseUint (str , 10 , 16 )
100123 if err != nil {
101- panic ("invalid uint16: " + env )
124+ return * new ( T ), errors . New ("invalid uint16: " + env )
102125 }
103126 val = uint16 (i64 )
104127 case uint8 :
105128 i64 , err := strconv .ParseUint (str , 10 , 8 )
106129 if err != nil {
107- panic ("invalid uint8: " + env )
130+ return * new ( T ), errors . New ("invalid uint8: " + env )
108131 }
109132 val = uint8 (i64 )
110133 case float64 :
111134 f64 , err := strconv .ParseFloat (str , 64 )
112135 if err != nil {
113- panic ("invalid float64: " + env )
136+ return * new ( T ), errors . New ("invalid float64: " + env )
114137 }
115138 val = f64
116139 case float32 :
117140 f64 , err := strconv .ParseFloat (str , 32 )
118141 if err != nil {
119- panic ("invalid float32: " + env )
142+ return * new ( T ), errors . New ("invalid float32: " + env )
120143 }
121144 val = float32 (f64 )
122145 case complex128 :
123146 c128 , err := strconv .ParseComplex (str , 128 )
124147 if err != nil {
125- panic ("invalid complex128: " + env )
148+ return * new ( T ), errors . New ("invalid complex128: " + env )
126149 }
127150 val = c128
128151 case complex64 :
129152 c128 , err := strconv .ParseComplex (str , 64 )
130153 if err != nil {
131- panic ("invalid complex64: " + env )
154+ return * new ( T ), errors . New ("invalid complex64: " + env )
132155 }
133156 val = complex64 (c128 )
134157 default :
135- panic ("unsupported type" )
158+ return * new ( T ), errors . New ("unsupported type" )
136159 }
137160
138- return val .(T )
161+ return val .(T ), nil
139162}
140163
141164// GetWithFallback returns the value of the environment variable or the fallback value if it is not set
@@ -146,10 +169,10 @@ func GetWithFallback[
146169 int | int64 | int32 | int16 | int8 |
147170 uint | uint64 | uint32 | uint16 | uint8 |
148171 float64 | float32 |
149- complex128 | complex64 ](env string , fallback T ) T {
172+ complex128 | complex64 ](env string , fallback T ) ( T , error ) {
150173 str := os .Getenv (env )
151174 if str == "" {
152- return fallback
175+ return fallback , nil
153176 }
154177
155178 return GetOrFail [T ](env )
0 commit comments