forked from eyecuelab/kit
/
env.go
77 lines (68 loc) · 1.87 KB
/
env.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Package env contains helper functions for setting and getting environment variables
package env
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/spf13/viper"
)
//Key is the key string of an environment variable.
type Key string
//Value is an alias for os.GetEnv(string(key))
func (key Key) Value() string {
return os.Getenv(string(key))
}
//Lookup is an alias for os.LookupEnv(string(key))
func (key Key) Lookup() (string, bool) {
return os.LookupEnv(string(key))
}
//SetString sets the string address pointed to by sp to key.Value().
func (key Key) SetString(sp *string) error {
val, ok := key.Lookup()
if !ok {
return fmt.Errorf("missing environment variable %s", key)
}
*sp = val
return nil
}
//SetInt sets the int address pointed to by ip to key.Value().
func (key Key) SetInt(ip *int) error {
val, ok := key.Lookup()
if !ok {
return fmt.Errorf("env.Key.SetInt: missing environment variable %s", key)
}
asInt, err := strconv.Atoi(val)
if err != nil {
return fmt.Errorf("env.Key.SetInt: strconv.Atoi: %v", err)
}
*ip = asInt
return nil
}
//SetBool sets the boolean address pointed to by bp to key.Value()
func (key Key) SetBool(bp *bool) error {
str := string(key)
val, ok := os.LookupEnv(str)
if !ok {
return fmt.Errorf("env.Key.SetBool: missing environment variable %s", key)
}
asBool, err := strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("env.Key.SetInt: strconv.ParseBool: %v", err)
}
*bp = asBool
return nil
}
//Check checks the local environment to see that all of the environment variables specified in config are set.
func Check() {
var missing []string
for _, key := range viper.GetStringSlice("env_var_names") {
if _, ok := os.LookupEnv(key); !ok {
missing = append(missing, key)
}
}
if len(missing) > 0 {
log.Fatalf("CheckEnv(): missing the following environmental variables: \n\t%s", strings.Join(missing, "\n\t"))
}
}