forked from rockintest/rocktest-go
/
step.go
88 lines (66 loc) · 1.36 KB
/
step.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
package scenario
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
)
type Step struct {
Origin map[string]interface{}
Type string
Desc string
Value string
Params map[string]interface{}
Steps []interface{}
M Module
scenario *Scenario
}
// Construct a new Step from the YAML node
func NewStep(n map[string]interface{}, s *Scenario) *Step {
ret := new(Step)
ret.Origin = n
ret.scenario = s
var val interface{}
var as string
for k, v := range n {
log.Tracef("%s - %s", k, v)
switch k {
case "desc":
ret.Desc = fmt.Sprintf("%v", v)
case "params":
ret.Params = nodeToMap(v)
case "steps":
ret.Steps = nodeToList(v)
case "as":
as = fmt.Sprint(v)
default:
ret.Type = strings.Title(strings.ReplaceAll(k, ".", "_"))
val = v
if v != nil {
ret.Value = fmt.Sprintf("%v", v)
} else {
ret.Value = ""
}
}
}
if ret.Params == nil {
ret.Params = make(map[string]interface{})
}
if val != nil {
ret.Params["value"] = val
}
if as != "" {
ret.Params["as"] = as
}
return ret
}
func (s Step) ToString() string {
ret := fmt.Sprintf("Type: %s, Value: %s, Desc: %s, Params: %v", s.Type, s.Value, s.Desc, s.Params)
return ret
}
func (s *Step) Exec() error {
log.Tracef("STEP: %s", s.ToString())
if s.Desc != "" {
fmt.Printf(">> %s\n", s.Desc)
}
return s.scenario.Exec(s.Type, s.Params)
}