-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargs.go
205 lines (170 loc) · 3.84 KB
/
args.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package kargo
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
)
type Args struct {
underlying []interface{}
}
func NewArgs(vs ...interface{}) *Args {
var args *Args
args = args.Append(vs...)
return args
}
func (a *Args) Len() int {
if a == nil {
return 0
}
return len(a.underlying)
}
func (a *Args) CopyFrom(other *Args) *Args {
if a == nil {
a = &Args{}
}
if other != nil {
a.underlying = append(a.underlying, other.underlying...)
}
return a
}
func (a *Args) AppendStrings(s ...string) *Args {
if a == nil {
a = &Args{}
}
for _, v := range s {
a.underlying = append(a.underlying, v)
}
return a
}
func (a *Args) Append(vs ...interface{}) *Args {
if a == nil {
a = &Args{}
}
for _, v := range vs {
v := v
switch v := v.(type) {
case *Args:
a.underlying = append(a.underlying, v.underlying...)
default:
a.underlying = append(a.underlying, v)
}
}
return a
}
func (a *Args) AppendValueFromOutput(ref string) *Args {
if a == nil {
a = &Args{}
}
a.underlying = append(a.underlying, DynArg{FromOutput: ref})
return a
}
func (a *Args) AppendValueIfOutput(v, ref string) *Args {
if a == nil {
a = &Args{}
}
a.underlying = append(a.underlying, DynArg{FromOutput: ref, Value: v})
return a
}
func (a *Args) AppendValueFromOutputWithPrefix(prefix, ref string) *Args {
if a == nil {
a = &Args{}
}
a.underlying = append(a.underlying, DynArg{Prefix: prefix, FromOutput: ref})
return a
}
func (a *Args) Visit(str func(string), out func(DynArg), flag func(KargoValueProvider)) {
if a == nil {
return
}
for _, x := range a.underlying {
switch a := x.(type) {
case string:
str(a)
case DynArg:
out(a)
case KargoValueProvider:
flag(a)
case *Args:
a.Visit(str, out, flag)
case []string:
for _, s := range a {
str(s)
}
default:
panic(fmt.Sprintf("unexpected type(%T) of item: %q", a, a))
}
}
}
func (a *Args) Collect(get func(string) (string, error)) ([]string, error) {
if a == nil {
return nil, nil
}
var (
prev string
args []string
errors []error
)
a.Visit(func(s string) {
args = append(args, s)
prev = s
}, func(a DynArg) {
v, err := get(a.FromOutput)
if err != nil {
errors = append(errors, fmt.Errorf("after %s: %w", prev, err))
return
}
if a.Value != "" {
args = append(args, a.Value)
} else {
args = append(args, a.Prefix+v)
}
prev = a.FromOutput
}, func(fvp KargoValueProvider) {
v, err := fvp.KargoValue(get)
if err != nil {
errors = append(errors, fmt.Errorf("after %s: %w", prev, err))
return
}
args = append(args, v)
prev = v
})
if len(errors) == 1 {
return nil, errors[0]
}
if len(errors) > 1 {
return nil, multierror.Append(nil, errors...)
}
return args, nil
}
func (a *Args) MustCollect(get func(string) (string, error)) []string {
got, err := a.Collect(get)
if err != nil {
panic(err)
}
return got
}
func (a *Args) String() string {
var args []string
a.Visit(func(s string) {
args = append(args, s)
}, func(a DynArg) {
args = append(args, fmt.Sprintf("$(get %s with prefix %s)", a.FromOutput, a.Prefix))
}, func(fvp KargoValueProvider) {
args = append(args, fmt.Sprintf("%s", fvp))
})
return strings.Join(args, " ")
}
// DynArg is a dynamic argument that is resolved at runtime.
// It is used to compose a command-line argument like --foo=$bar,
// where $bar is a value of another kargo command.
type DynArg struct {
// Prefix is a prefix to be prepended to the value of FromOutput.
// For example, Prefix=foo= and FromOutput=bar will result in foo=$bar.
// This is handy when you need to compose a command-line argument like --foo=$bar,
// instead of --foo bar.
Prefix string
// FromOutput is a reference to an output of another kargo command.
FromOutput string
// Value is the value used when the FromOutput is provided and the output is non-empty.
Value string
}