-
Notifications
You must be signed in to change notification settings - Fork 207
/
flags.go
148 lines (127 loc) · 3.08 KB
/
flags.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
package builder
import (
"fmt"
"sort"
)
// Flag represents a flag passed to a mixin command.
type Flag struct {
Name string
Values []string
}
type Dashes struct {
Long string
Short string
}
// NewFlag creates an instance of a Flag.
func NewFlag(name string, values ...string) Flag {
f := Flag{
Name: name,
}
if len(values) > 0 {
f.Values = make([]string, len(values))
copy(f.Values, values)
}
return f
}
// ToSlice converts to a string array suitable of command arguments suitable for passing to exec.Command
func (flag Flag) ToSlice(dashes Dashes) []string {
var flagName string
dash := dashes.Long
if len(flag.Name) == 1 {
dash = dashes.Short
}
flagName = fmt.Sprintf("%s%s", dash, flag.Name)
result := make([]string, 0, len(flag.Values)+1)
if len(flag.Values) == 0 {
result = append(result, flagName)
} else {
for _, value := range flag.Values {
result = append(result, flagName)
result = append(result, value)
}
}
return result
}
type Flags []Flag
// ToSlice converts to a string array suitable of command arguments suitable for passing to exec.Command
func (flags *Flags) ToSlice(dashes Dashes) []string {
result := make([]string, 0, 2*len(*flags))
sort.Sort(flags)
for _, flag := range *flags {
result = append(result, flag.ToSlice(dashes)...)
}
return result
}
// UnmarshalYAML takes input like this
// flags:
// flag1: value
// flag2: value
// flag3:
// - value1
// - value2
//
// and turns it into this:
//
// []Flags{ {"flag1", []string{"value"}}, {"flag2", []string{"value"}}, {"flag3", []string{"value1", "value2"} }
func (flags *Flags) UnmarshalYAML(unmarshal func(interface{}) error) error {
flagMap := map[string]interface{}{}
err := unmarshal(&flagMap)
if err != nil {
return fmt.Errorf("could not unmarshal yaml into Step.Flags: %w", err)
}
*flags = make(Flags, 0, len(flagMap))
for k, v := range flagMap {
f := Flag{}
f.Name = k
switch t := v.(type) {
case []interface{}:
f.Values = make([]string, len(t))
for i := range t {
iv, ok := t[i].(string)
if !ok {
return fmt.Errorf("invalid yaml type for flag %s: %T", f.Name, t[i])
}
f.Values[i] = iv
}
case nil:
// do nothing
default:
f.Values = make([]string, 1)
f.Values[0] = fmt.Sprintf("%v", v)
}
*flags = append(*flags, f)
}
return nil
}
// MarshalYAML writes out flags back into the proper format for mixin flags.
// Input:
// []Flags{ {"flag1", []string{"value"}}, {"flag2", []string{"value"}}, {"flag3", []string{"value1", "value2"} }
//
// Is turned into
//
// flags:
// flag1: value
// flag2: value
// flag3:
// - value1
// - value2
func (flags Flags) MarshalYAML() (interface{}, error) {
result := make(map[string]interface{}, len(flags))
for _, flag := range flags {
if len(flag.Values) == 1 {
result[flag.Name] = flag.Values[0]
} else {
result[flag.Name] = flag.Values
}
}
return result, nil
}
func (flags Flags) Len() int {
return len(flags)
}
func (flags Flags) Swap(i, j int) {
flags[i], flags[j] = flags[j], flags[i]
}
func (flags Flags) Less(i, j int) bool {
return flags[i].Name < flags[j].Name
}