-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetenv.go
39 lines (32 loc) · 816 Bytes
/
getenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package getenv
import (
"fmt"
"log"
"os"
)
// DefaultString -
type DefaultString *string
// EnvOrDefault - main function to fetch for environment variable or return default
func EnvOrDefault(key string, d DefaultString) string {
value := os.Getenv(key)
if len(value) == 0 && d != nil {
log.Print(fmt.Sprintf("%s is not found. fallback to default value.", key))
return Value(d)
} else if len(value) == 0 && d == nil {
log.Panicf("%s is a required environment variable", key)
}
return value
}
// Nil - custom fn to return nil
func Nil() *string {
return nil
}
// String - custom fn to return the pointer of a string
func String(s string) *string {
newS := s
return &newS
}
// Value - returns the default value as a string
func Value(defaultValue DefaultString) string {
return *defaultValue
}