-
Notifications
You must be signed in to change notification settings - Fork 5
/
genv.go
150 lines (117 loc) · 2.84 KB
/
genv.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Package genv is a library for Go (golang) that makes it easy to read and use
// environment variables in your projects. It also allows environment variables
// to be loaded from the .env file.
package genv
import (
"os"
"strconv"
)
// EnvVariable contains information about the environment variable, such as key,
// value, and default value.
type EnvVariable struct {
Key string
Val string
DefaultValue interface{}
IsDefined bool
}
// EnvVariables is where environment variables are stored.
var EnvVariables = make(map[string]*EnvVariable)
// Key is used to determine the path of the environment variable to be accessed.
//
// genv.Key("env-key").String()
//
func Key(key string) *EnvVariable {
envVar, ok := EnvVariables[key]
if !ok {
val, ok := os.LookupEnv(key)
EnvVariables[key] = &EnvVariable{Key: key, Val: val, IsDefined: ok}
return EnvVariables[key]
}
return envVar
}
// Default is used to specify the default value for the environment
// variable to be accessed.
//
// genv.Key("env-key").Default("defaultValue").String()
//
func (e *EnvVariable) Default(defaultValue interface{}) *EnvVariable {
e.DefaultValue = defaultValue
return e
}
// Update is used to update the value of the corresponding environment variable.
//
// genv.Key("env-key").Update("updatedValue")
//
func (e *EnvVariable) Update(value interface{}) {
switch value.(type) {
case bool:
e.Val = strconv.FormatBool(value.(bool))
case float64:
e.Val = strconv.FormatFloat(value.(float64), 'f', -1, 64)
case int:
e.Val = strconv.FormatInt(int64(value.(int)), 10)
case string:
e.Val = value.(string)
}
e.IsDefined = true
os.Setenv(e.Key, e.Val)
}
// Bool method is used for environment variables of type bool.
//
// genv.Key("env-key").Bool()
//
func (e *EnvVariable) Bool() bool {
var dv bool
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(bool)
}
return dv
}
val, _ := strconv.ParseBool(e.Val)
return val
}
// Float method is used for environment variables of type float.
//
// genv.Key("env-key").Float()
//
func (e *EnvVariable) Float() float64 {
var dv float64
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(float64)
}
return dv
}
val, _ := strconv.ParseFloat(e.Val, 64)
return val
}
// Int method is used for environment variables of type int.
//
// genv.Key("env-key").Int()
//
func (e *EnvVariable) Int() int {
var dv int
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(int)
}
return dv
}
val, _ := strconv.ParseInt(e.Val, 10, 32)
return int(val)
}
// String method is used for environment variables of type string.
//
// genv.Key("env-key").String()
//
func (e *EnvVariable) String() string {
var dv string
if !e.IsDefined {
if e.DefaultValue != nil {
dv = e.DefaultValue.(string)
}
return dv
}
return e.Val
}