-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
64 lines (53 loc) · 1.49 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
package tranquility
import (
"encoding/json"
"io/ioutil"
"os"
)
type environment map[string]string
//Env is the global enviroment to be shared across actions
var Env = environment{}
// Set modifies/adds the new key - value in the global environment
func (env environment) Set(key, value string) {
env[key] = value
}
// Get returns the value associated with key in case it exists, otherwise ""
func (env environment) Get(key string) string {
return env[key]
}
// Del deletes the key and the value associated from the map
func (env environment) Del(key string) {
delete(env, key)
}
// Init initializes the environment with the given map
func (env environment) Init(initMap map[string]string) {
for k, v := range initMap {
env.Set(k, v)
}
}
// Representation of a Postman Environment json file
type postmanEnv struct {
Values []postmanEnvVar `json:"values"`
}
// postmanEnvVar is the representation of a Postman environment value from a json
type postmanEnvVar struct {
Key string `json:"key"`
Value string `json:"value"`
}
// ReadPostmanEnv initializes the environment with the given json format postman environment file
func (env environment) ReadPostmanEnv(file string) {
envFile, err := os.Open(file)
if err != nil {
panic(err)
}
defer envFile.Close()
jsonBytes, err := ioutil.ReadAll(envFile)
if err != nil {
panic(err)
}
var loadedEnv postmanEnv
json.Unmarshal(jsonBytes, &loadedEnv)
for i := range loadedEnv.Values {
env.Set(loadedEnv.Values[i].Key, loadedEnv.Values[i].Value)
}
}