-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
247 lines (202 loc) · 5.04 KB
/
runtime.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package gostman
import (
"errors"
"flag"
"io"
"os"
"sync"
"testing"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func init() {
flag.StringVar(&runtime.flagEnv, "env", "", "Select environment define in .gostman.env.yml")
flag.StringVar(&runtime.flagSetenv, "setenv", "", "Select environment define in .gostman.env.yml and set it for the future request")
flag.BoolVar(&runtime.flagReset, "reset", false, "Reset .gostman.runtime.yml")
flag.BoolVar(&runtime.flagDebug, "debug", false, "Run gostman in debug mode")
}
const (
defaultEnv = "no_env"
envFilename = ".gostman.env.yml"
runtimeFilename = ".gostman.runtime.yml"
)
type gmRuntime struct {
once sync.Once
m *testing.M
flagEnv string
flagSetenv string
flagReset bool
flagDebug bool
cfgRuntimeFile *os.File
cfgEnv map[string]map[string]string
cfgRuntime struct {
Env string `yaml:"env"`
Initial map[string]map[string]string `yaml:"initial"`
Current map[string]map[string]string `yaml:"current"`
}
env string
mu sync.RWMutex
initial map[string]string // initial variable for the selected environment
current map[string]string // current variable for the selected environment
}
var runtime = new(gmRuntime)
// Run run gostman runtime and the test. It returns an exit code to pass to os.Exit.
// The runtime should be run in the TestMain before using Gostman.
//
// func TestMain(m *testing.M) {
// os.Exit(gostman.Run(m))
// }
func Run(m *testing.M) (code int) {
runtime.once.Do(func() {
if !flag.Parsed() {
flag.Parse()
}
if err := runtime.init(m); err != nil {
log.Fatal(err)
}
})
defer runtime.close()
return runtime.m.Run()
}
func (gmr *gmRuntime) init(m *testing.M) error {
// register *testing.M
gmr.m = m
// check debug mode
if gmr.flagDebug {
log.SetLevel(log.DebugLevel)
}
// load config env and runtime
if err := gmr.loadCfgEnv(); err != nil {
return err
}
if err := gmr.loadCfgRuntime(); err != nil {
return err
}
// check reset
if gmr.flagReset {
gmr.reset()
gmr.populate()
}
// check env
gmr.env = gmr.cfgRuntime.Env
if gmr.flagSetenv != "" {
gmr.cfgRuntime.Env = gmr.flagSetenv
gmr.env = gmr.flagSetenv
}
if gmr.flagEnv != "" {
gmr.env = gmr.flagEnv
}
log.Infof("using env %q", gmr.env)
// set variable for the selected environment
gmr.initial = gmr.cfgRuntime.Initial[gmr.env]
gmr.current = gmr.cfgRuntime.Current[gmr.env]
return nil
}
func (gmr *gmRuntime) loadCfgEnv() error {
// init first
gmr.cfgEnv = make(map[string]map[string]string)
f, err := os.Open(envFilename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Debug(err)
return nil
}
return err
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(&gmr.cfgEnv); err != nil {
if errors.Is(err, io.EOF) {
log.Debug(err)
return nil
}
return err
}
return nil
}
func (gmr *gmRuntime) loadCfgRuntime() error {
// init first
gmr.reset()
f, err := os.OpenFile(runtimeFilename, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
gmr.cfgRuntimeFile = f
if err := yaml.NewDecoder(f).Decode(&gmr.cfgRuntime); err != nil {
// format error, debug it and continue populate the runtime
log.Debug(err)
}
gmr.populate()
return nil
}
func (gmr *gmRuntime) reset() {
gmr.cfgRuntime.Env = defaultEnv
gmr.cfgRuntime.Initial = make(map[string]map[string]string)
gmr.cfgRuntime.Current = make(map[string]map[string]string)
}
func (gmr *gmRuntime) populate() {
for env, values := range gmr.cfgEnv {
if _, ok := gmr.cfgRuntime.Initial[env]; !ok {
gmr.cfgRuntime.Initial[env] = make(map[string]string)
gmr.cfgRuntime.Current[env] = make(map[string]string)
}
for k, v := range values {
if rv, ok := gmr.cfgRuntime.Initial[env][k]; !ok || rv != v {
gmr.cfgRuntime.Initial[env][k] = v
gmr.cfgRuntime.Current[env][k] = v
}
}
}
for env, values := range gmr.cfgRuntime.Initial {
if _, ok := gmr.cfgRuntime.Current[env]; !ok {
gmr.cfgRuntime.Current[env] = make(map[string]string)
}
for k, v := range values {
if _, ok := gmr.cfgRuntime.Current[env][k]; !ok {
gmr.cfgRuntime.Current[env][k] = v
}
}
}
for env, values := range gmr.cfgRuntime.Current {
if _, ok := gmr.cfgRuntime.Initial[env]; !ok {
delete(gmr.cfgRuntime.Current, env)
}
for k := range values {
if _, ok := gmr.cfgRuntime.Initial[env][k]; !ok {
delete(gmr.cfgRuntime.Current[env], k)
}
}
}
}
func (gmr *gmRuntime) close() {
f := gmr.cfgRuntimeFile
if f == nil {
return
}
defer f.Close()
f.Truncate(0)
f.Seek(0, 0)
enc := yaml.NewEncoder(f)
defer enc.Close()
if err := enc.Encode(gmr.cfgRuntime); err != nil {
log.Fatal(err)
}
}
func (gmr *gmRuntime) setEnvVar(name, val string) {
if runtime.initial == nil {
return
}
runtime.mu.Lock()
defer runtime.mu.Unlock()
if _, ok := runtime.initial[name]; !ok {
runtime.initial[name] = ""
}
runtime.current[name] = val
}
func (gmr *gmRuntime) envVar(name string) string {
if runtime.initial == nil {
return ""
}
runtime.mu.RLock()
defer runtime.mu.RUnlock()
return runtime.current[name]
}