-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
72 lines (64 loc) · 1.54 KB
/
config.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
package main
import (
"encoding/json"
"io"
"log"
"os"
"strings"
"gioman/service"
"gioman/state"
)
type (
config struct {
Requests []requestConfig `json:"requests,omitempty"`
}
requestConfig struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Headers []state.Header `json:"headers"`
}
)
func configFromFilepath(path string) (cfg config, err error) {
// TODO change the filename path, look for the config folder of the system?
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&cfg)
return
}
func (cfg *config) save(w io.Writer) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("" /*prefix*/, " " /*indent*/)
return encoder.Encode(cfg)
}
func (cfg *config) setRequests(requests []state.Request) {
cfg.Requests = cfg.Requests[:0]
for _, r := range requests {
cfg.Requests = append(cfg.Requests, requestConfig{
Name: r.Name,
URL: r.URL,
Method: r.Method.String(),
Headers: r.Headers,
})
}
}
func (cfg *config) requests() (requests []state.Request) {
for _, r := range cfg.Requests {
method, ok := service.Methods[strings.ToUpper(r.Method)]
if !ok {
method = service.GET
log.Printf("requests: stored request %q: unknown method %q, assuming %q", r.Name, r.Method, method)
}
requests = append(requests, state.Request{
Name: r.Name,
URL: r.URL,
Method: method,
Headers: r.Headers,
})
}
return
}