-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonv.go
64 lines (55 loc) · 1.44 KB
/
gonv.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
package gonv
import(
"github.com/jenazads/goutils";
)
// Gonv object
type Gonv struct {
enVar map[string]interface{}
}
// Create New Gonv Object
func NewGonv() (*Gonv){
mv := make(map[string]interface{})
return &Gonv{
enVar: mv}
}
// Set new environmental variable
func (gnv *Gonv) SetEnv(tag string, value interface{}) error {
if val, err := gnv.GetEnv(tag); val == nil || err != nil {
gnv.enVar[tag] = value
return nil
}else{
return GonvErrAlreadyExist
}
}
// Get environmental variable
func (gnv *Gonv) GetEnv(tag string) (interface{}, error) {
if val := gnv.enVar[tag]; val == nil{
return nil, GonvErrNotFound
}else{
return val, nil
}
}
// Update environmental variable
func (gnv *Gonv) UpdateEnv(tag string, value interface{}) error {
if val, err := gnv.GetEnv(tag); val == nil || err != nil {
return GonvErrNotFound
}else{
gnv.enVar[tag] = value
return nil
}
}
// Get environmental variable and transforms to string
func (gnv *Gonv) GetStringEnv(tag string) (string, error){
value, err := gnv.GetEnv(tag)
return goutils.ToString(value), err
}
// Get environmental variable and transforms to int
func (gnv *Gonv) GetIntEnv(tag string) (int, error){
value, err := gnv.GetEnv(tag)
return goutils.ToInt(value), err
}
// Get environmental variable and transforms to bool
func (gnv *Gonv) GetBoolEnv(tag string) (bool, error){
value, err := gnv.GetEnv(tag)
return goutils.ToBool(value), err
}