-
Notifications
You must be signed in to change notification settings - Fork 71
/
var_set.go
98 lines (76 loc) · 2.11 KB
/
var_set.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
package runner
import (
"fmt"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/ui"
)
func askForMissingVars(missingVars []varSet) []varSet {
ui.DefaultUI.Warning("Some variables are required by one or more tests")
ui.DefaultUI.Info("Fill the values for each variable:")
filledVariables := make([]varSet, 0, len(missingVars))
for _, missingVar := range missingVars {
answer := missingVar
answer.UserValue = ui.DefaultUI.TextInput(missingVar.Name, missingVar.DefaultValue)
filledVariables = append(filledVariables, answer)
}
return filledVariables
}
type varSet struct {
Name string
DefaultValue string
UserValue string
}
func (ev varSet) value() string {
if ev.UserValue != "" {
return ev.UserValue
}
return ev.DefaultValue
}
type varSets []varSet
func (evs varSets) toOpenapi() []openapi.VariableSetValue {
vars := make([]openapi.VariableSetValue, len(evs))
for i, ev := range evs {
vars[i] = openapi.VariableSetValue{
Key: openapi.PtrString(ev.Name),
Value: openapi.PtrString(ev.value()),
}
}
return vars
}
func (evs varSets) unique() varSets {
seen := make(map[string]bool)
vars := make(varSets, 0, len(evs))
for _, ev := range evs {
if seen[ev.Name] {
continue
}
seen[ev.Name] = true
vars = append(vars, ev)
}
return vars
}
type missingVarsError varSets
func (e missingVarsError) Error() string {
return fmt.Sprintf("missing variables: %v", []varSet(e))
}
func (e missingVarsError) Is(target error) bool {
_, ok := target.(missingVarsError)
return ok
}
func buildMissingVarsError(body []byte) error {
var missingVarsErrResp openapi.MissingVariablesError
err := jsonFormat.Unmarshal(body, &missingVarsErrResp)
if err != nil {
return fmt.Errorf("could not unmarshal response body: %w", err)
}
missingVars := varSets{}
for _, missingVarErr := range missingVarsErrResp.MissingVariables {
for _, missingVar := range missingVarErr.Variables {
missingVars = append(missingVars, varSet{
Name: missingVar.GetKey(),
DefaultValue: missingVar.GetDefaultValue(),
})
}
}
return missingVarsError(missingVars.unique())
}