-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
52 lines (47 loc) · 1.24 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
package collection
import (
"encoding/json"
"io"
)
//Environment represents environment variables
type Environment struct {
ID json.RawMessage `json:"id"`
Name string `json:"name"`
Values []Field `json:"values"`
PostmanVariableScope json.RawMessage `json:"_postman_variable_scope"`
PostmanExportedAt json.RawMessage `json:"_postman_exported_at"`
PostmanExportedUsing json.RawMessage `json:"_postman_exported_using"`
}
// Open open an environment file
func (e *Environment) Open(rdr io.Reader) error {
dcr := json.NewDecoder(rdr)
if err := dcr.Decode(&e); err != nil {
return err
}
for i := len(e.Values) - 1; i >= 0; i-- {
if !e.Values[i].Enabled {
e.Values = append(e.Values[:i], e.Values[i+1:]...)
}
}
return nil
}
func (e *Environment) upsertVariable(f Field) {
var update bool
for i, v := range e.Values {
if v.Key == f.Key {
e.Values[i] = f
update = true
}
}
if !update {
e.Values = append(e.Values, f)
}
}
// SetCollectionVariables set collection variables to the env // if env provided then the values will be replaced
func (e *Environment) SetCollectionVariables(vv []Field) {
for _, v := range vv {
if !v.Disabled {
e.upsertVariable(v)
}
}
}