Skip to content

Commit 0f3c3bb

Browse files
committed
ft: get default
1 parent 455c63e commit 0f3c3bb

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

helpers/env.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ package helpers
33
import (
44
"os"
55
"strconv"
6+
7+
"github.com/ochom/gutils/logs"
68
)
79

8-
// GetEnv ...
9-
func GetEnv(key string, defaultValue string) string {
10+
// GetEnv returns env variable or the provided default value when variable not found
11+
func GetEnv(key string) string {
12+
value, ok := os.LookupEnv(key)
13+
if !ok {
14+
logs.Error("Environment variable %s not found", key)
15+
}
16+
17+
return value
18+
}
19+
20+
// GetEnv returns env variable or the provided default value when variable not found
21+
func GetEnvDefault(key string, defaultValue string) string {
1022
value, ok := os.LookupEnv(key)
1123
if !ok {
1224
return defaultValue
@@ -15,7 +27,7 @@ func GetEnv(key string, defaultValue string) string {
1527
return value
1628
}
1729

18-
// GetEnvInt ...
30+
// GetEnvInt returns an integer from env variable or the provided default value when variable not found
1931
func GetEnvInt(key string, defaultValue int) int {
2032
value, ok := os.LookupEnv(key)
2133
if !ok {
@@ -30,7 +42,7 @@ func GetEnvInt(key string, defaultValue int) int {
3042
return val
3143
}
3244

33-
// GetEnvBool ...
45+
// GetEnvBool returns a boolean from env variable or the provided default value when variable not found
3446
func GetEnvBool(key string, defaultValue bool) bool {
3547
value, ok := os.LookupEnv(key)
3648
if !ok {
@@ -45,7 +57,7 @@ func GetEnvBool(key string, defaultValue bool) bool {
4557
return val
4658
}
4759

48-
// GetEnvFloat ...
60+
// GetEnvFloat returns a float from env variable or the provided default value when variable not found
4961
func GetEnvFloat(key string, defaultValue float64) float64 {
5062
value, ok := os.LookupEnv(key)
5163
if !ok {

helpers/env_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestGetEnv(t *testing.T) {
3737
}
3838
for _, tt := range tests {
3939
t.Run(tt.name, func(t *testing.T) {
40-
if got := helpers.GetEnv(tt.args.key, tt.args.defaultValue); got != tt.want {
40+
if got := helpers.GetEnvDefault(tt.args.key, tt.args.defaultValue); got != tt.want {
4141
t.Errorf("GetEnv() = %v, want %v", got, tt.want)
4242
}
4343
})

0 commit comments

Comments
 (0)