-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
book.go
124 lines (112 loc) · 2.9 KB
/
book.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
package runn
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"time"
"github.com/goccy/go-yaml"
"github.com/k1LoW/duration"
"github.com/k1LoW/expand"
)
const noDesc = "[No Description]"
type book struct {
Desc string `yaml:"desc,omitempty"`
Runners map[string]string `yaml:"runners,omitempty"`
Vars map[string]interface{} `yaml:"vars,omitempty"`
Steps []map[string]interface{} `yaml:"steps,omitempty"`
Debug bool `yaml:"debug,omitempty"`
Interval string `yaml:"interval,omitempty"`
stepKeys []string
path string
httpRunners map[string]*httpRunner
dbRunners map[string]*dbRunner
interval time.Duration
t *testing.T
}
func newBook() *book {
return &book{
Runners: map[string]string{},
Vars: map[string]interface{}{},
Steps: []map[string]interface{}{},
httpRunners: map[string]*httpRunner{},
dbRunners: map[string]*dbRunner{},
interval: 0 * time.Second,
}
}
func loadBook(in io.Reader) (*book, error) {
bk := newBook()
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, in); err != nil {
return nil, err
}
if err := yaml.NewDecoder(bytes.NewBuffer(expand.ExpandenvYAMLBytes(buf.Bytes()))).Decode(bk); err == nil {
if bk.Runners == nil {
bk.Runners = map[string]string{}
}
if bk.Vars == nil {
bk.Vars = map[string]interface{}{}
}
if bk.Desc == "" {
bk.Desc = noDesc
}
return bk, nil
}
// orderedmap
m := struct {
Desc string `yaml:"desc,omitempty"`
Runners map[string]string `yaml:"runners,omitempty"`
Vars map[string]interface{} `yaml:"vars,omitempty"`
Steps yaml.MapSlice `yaml:"steps,omitempty"`
Debug bool `yaml:"debug,omitempty"`
Interval string `yaml:"interval,omitempty"`
}{
Runners: map[string]string{},
Vars: map[string]interface{}{},
Steps: yaml.MapSlice{},
}
if err := yaml.NewDecoder(bytes.NewBuffer(expand.ExpandenvYAMLBytes(buf.Bytes()))).Decode(&m); err != nil {
return nil, err
}
bk.Desc = m.Desc
bk.Runners = m.Runners
bk.Vars = m.Vars
bk.Debug = m.Debug
bk.Interval = m.Interval
if bk.Interval != "" {
d, err := duration.Parse(bk.Interval)
if err != nil {
return nil, err
}
bk.interval = d
}
for _, s := range m.Steps {
bk.Steps = append(bk.Steps, s.Value.(map[string]interface{}))
switch v := s.Key.(type) {
case string:
bk.stepKeys = append(bk.stepKeys, v)
case uint64:
bk.stepKeys = append(bk.stepKeys, fmt.Sprintf("%d", v))
default:
bk.stepKeys = append(bk.stepKeys, fmt.Sprintf("%v", v))
}
}
return bk, nil
}
func LoadBook(path string) (*book, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
bk, err := loadBook(f)
if err != nil {
_ = f.Close()
return nil, err
}
bk.path = path
if err := f.Close(); err != nil {
return nil, err
}
return bk, nil
}