Skip to content

Commit 181a899

Browse files
committed
fix: add new version
1 parent b11debb commit 181a899

File tree

1 file changed

+66
-18
lines changed

1 file changed

+66
-18
lines changed

env/env.go

+66-18
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
11
package env
22

33
import (
4+
"fmt"
45
"os"
56
"strconv"
67

78
_ "github.com/joho/godotenv/autoload"
8-
"github.com/ochom/gutils/logs"
99
)
1010

1111
// Get returns env variable or the provided default value when variable not found
12-
func Get(props ...string) string {
13-
if len(props) == 0 {
14-
logs.Error("Get Env: props cannot be empty")
15-
return ""
16-
}
17-
18-
key := props[0]
12+
func Get(name string, defaults ...string) string {
1913
defaultValue := ""
20-
if len(props) > 1 {
21-
defaultValue = props[1]
14+
if len(defaults) > 1 {
15+
defaultValue = defaults[0]
2216
}
2317

24-
value, ok := os.LookupEnv(key)
18+
value, ok := os.LookupEnv(name)
2519
if !ok {
26-
logs.Warn("Get Env: %s not found", key)
2720
return defaultValue
2821
}
2922

3023
return value
3124
}
3225

3326
// Int returns an integer from env variable or the provided default value when variable not found
34-
func Int(key string, defaultValue int) int {
35-
value, ok := os.LookupEnv(key)
27+
func Int(name string, defaultValue int) int {
28+
value, ok := os.LookupEnv(name)
3629
if !ok {
3730
return defaultValue
3831
}
@@ -46,8 +39,8 @@ func Int(key string, defaultValue int) int {
4639
}
4740

4841
// Bool returns a boolean from env variable or the provided default value when variable not found
49-
func Bool(key string, defaultValue bool) bool {
50-
value, ok := os.LookupEnv(key)
42+
func Bool(name string, defaultValue bool) bool {
43+
value, ok := os.LookupEnv(name)
5144
if !ok {
5245
return defaultValue
5346
}
@@ -61,8 +54,8 @@ func Bool(key string, defaultValue bool) bool {
6154
}
6255

6356
// Float returns a float from env variable or the provided default value when variable not found
64-
func Float(key string, defaultValue float64) float64 {
65-
value, ok := os.LookupEnv(key)
57+
func Float(name string, defaultValue float64) float64 {
58+
value, ok := os.LookupEnv(name)
6659
if !ok {
6760
return defaultValue
6861
}
@@ -74,3 +67,58 @@ func Float(key string, defaultValue float64) float64 {
7467

7568
return val
7669
}
70+
71+
// MustGet returns env variable or panics when variable not found
72+
func MustGet(name string) string {
73+
value, ok := os.LookupEnv(name)
74+
if !ok {
75+
panic(fmt.Errorf("MustGet Env: %s not found", name))
76+
}
77+
78+
return value
79+
}
80+
81+
// MustInt returns an integer from env variable or panics when variable not found
82+
func MustInt(name string) int {
83+
value, ok := os.LookupEnv(name)
84+
if !ok {
85+
panic(fmt.Errorf("MustInt Env: %s not found", name))
86+
}
87+
88+
val, err := strconv.Atoi(value)
89+
if err != nil {
90+
panic(fmt.Errorf("MustInt Env: %s not an integer", name))
91+
}
92+
93+
return val
94+
}
95+
96+
// MustBool returns a boolean from env variable or panics when variable not found
97+
func MustBool(name string) bool {
98+
value, ok := os.LookupEnv(name)
99+
if !ok {
100+
panic(fmt.Errorf("MustBool Env: %s not found", name))
101+
}
102+
103+
val, err := strconv.ParseBool(value)
104+
if err != nil {
105+
panic(fmt.Errorf("MustBool Env: %s not a boolean", name))
106+
}
107+
108+
return val
109+
}
110+
111+
// MustFloat returns a float from env variable or panics when variable not found
112+
func MustFloat(name string) float64 {
113+
value, ok := os.LookupEnv(name)
114+
if !ok {
115+
panic(fmt.Errorf("MustFloat Env: %s not found", name))
116+
}
117+
118+
val, err := strconv.ParseFloat(value, 64)
119+
if err != nil {
120+
panic(fmt.Errorf("MustFloat Env: %s not a float", name))
121+
}
122+
123+
return val
124+
}

0 commit comments

Comments
 (0)