/
diff_inputs.go
373 lines (302 loc) · 8.95 KB
/
diff_inputs.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package command
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/simplesurance/baur/v3/internal/format/csv"
"github.com/simplesurance/baur/v3/internal/format/table"
"github.com/simplesurance/baur/v3/internal/vcs/git"
"github.com/simplesurance/baur/v3/pkg/baur"
"github.com/simplesurance/baur/v3/pkg/storage"
)
type diffInputArgDetails struct {
arg string
appName string
taskName string
runID string
task *baur.Task
}
func init() {
diffCmd.AddCommand(&newDiffInputsCmd().Command)
}
type diffInputsCmd struct {
cobra.Command
csv bool
quiet bool
inputStr []string
gitRepo *git.Repository
}
const diffInputslongHelp = `
List the difference of inputs between tasks or task-runs.
An argument can either reference a task or a task-run.
If a task is specified the current inputs of the task in the filesystem are
compared. A task is specified in the format APP_NAME.TASK_NAME.
A past task-run that was recorded in the database can be specified by:
- its run-id,
- or by the git-like syntax APP_NAME.TASK_NAME^, the number of ^ characters
specify the run-id, ^ refers the last recorded run, '^^' the run before the
last, and so on
States:
D - digests do not match,
+ - the input is missing in the first task(-run)
- - the input is missing in the second task(-run)
Exit Codes:
0 - Inputs are the same
1 - Internal error
2 - Inputs differ
`
const diffInputsExample = `
baur diff inputs calc.build calc.check - Compare current inputs of the
build and check tasks of the calc app
baur diff inputs calc.build 312 - Compare current inputs of the build
task of the calc app with the recorded
run with ID 312.
baur diff inputs calc.build calc.build^ - Compare current inputs and the one
of the last recorded run of the calc
task of the build app.
`
func newDiffInputsCmd() *diffInputsCmd {
cmd := diffInputsCmd{
Command: cobra.Command{
Use: "inputs <APP_NAME.TASK_NAME[^]|RUN_ID> <APP_NAME.TASK_NAME[^]|RUN_ID>",
Short: "list inputs that differ between two task-runs",
Long: strings.TrimSpace(diffInputslongHelp),
Example: strings.TrimSpace(diffInputsExample),
Args: diffArgs(),
ValidArgsFunction: newCompleteTargetFunc(completeTargetFuncOpts{
withoutWildcards: true,
withoutAppNames: true,
withoutPaths: true,
}),
},
}
cmd.Run = cmd.run
cmd.Flags().BoolVar(&cmd.csv, "csv", false,
"show output in RFC4180 CSV format")
cmd.Flags().BoolVarP(&cmd.quiet, "quiet", "q", false,
"do not list the inputs that differ")
cmd.Flags().StringArrayVar(&cmd.inputStr, "input-str", nil,
"include a string as input of the task or run specified by the first argument,\n"+
"can be specified multiple times")
return &cmd
}
// diffArgs returns an error in the following scenarios:
// - there is less than or greater than 2 args specified
// - either arg is not in the format APP-NAME.TASK-AME> or a numeric value
func diffArgs() cobra.PositionalArgs {
return func(_ *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("accepts 2 args, received %d", len(args))
}
validArgRE := regexp.MustCompile(`^[\w-]+\.[\w-]+\^*$|^[0-9]+\d*$`)
for _, arg := range args {
if !validArgRE.MatchString(arg) {
return fmt.Errorf("invalid argument: %q", arg)
}
}
return nil
}
}
func (c *diffInputsCmd) run(_ *cobra.Command, args []string) {
if len(c.inputStr) == 0 && args[0] == args[1] {
exitOnErr(fmt.Errorf("%s and %s refer to the same task-run", args[0], args[1]))
}
repo := mustFindRepository()
c.gitRepo = mustGetRepoState(repo.Path)
argDetails := c.getDiffInputArgDetails(repo, args)
inputs1, run1 := c.getTaskRunInputs(repo, argDetails[0])
inputs1.Add(baur.AsInputStrings(c.inputStr...))
inputs2, run2 := c.getTaskRunInputs(repo, argDetails[1])
if len(c.inputStr) == 0 && run1 != nil && run2 != nil && run1.ID == run2.ID {
exitOnErr(fmt.Errorf("%s and %s refer to the same task-run", args[0], args[1]))
}
diffs, err := baur.DiffInputs(inputs1, inputs2)
exitOnErr(err)
c.printOutput(diffs)
if len(diffs) > 0 {
exitFunc(2)
}
exitFunc(0)
}
func (c *diffInputsCmd) getDiffInputArgDetails(repo *baur.Repository, args []string) []*diffInputArgDetails {
results := make([]*diffInputArgDetails, 0, len(args))
for _, arg := range args {
app, task, runID := parseDiffSpec(arg)
results = append(results, &diffInputArgDetails{arg: arg, appName: app, taskName: task, runID: runID})
}
var mustHaveTasks []string
for _, argDetails := range results {
if argDetails.runID == "" {
mustHaveTasks = append(mustHaveTasks, argDetails.arg)
}
}
if len(mustHaveTasks) > 0 {
tasks := mustArgToTasks(repo, c.gitRepo, mustHaveTasks)
for _, task := range tasks {
for _, argDetails := range results {
if argDetails.runID == "" && task.AppName == argDetails.appName && task.Name == argDetails.taskName {
argDetails.task = task
}
}
}
for _, argDetails := range results {
if argDetails.runID == "" && argDetails.task == nil {
exitOnErr(fmt.Errorf("%s: task not found", argDetails.arg))
}
}
}
return results
}
// parseDiffSpec splits an argument into the app, task and runID components.
// It relies on the fact that the arguments have already been validated
// in the cobra.Command
func parseDiffSpec(s string) (app, task, runID string) {
if strings.Contains(s, ".") {
spl := strings.Split(s, ".")
app = spl[0]
task = spl[1]
if strings.Contains(task, "^") {
caretIndex := strings.Index(task, "^")
runID = subStr(task, caretIndex, len(task))
task = subStr(task, 0, caretIndex)
}
return app, task, runID
}
return "", "", s
}
func (c *diffInputsCmd) getTaskRunInputs(repo *baur.Repository, argDetails *diffInputArgDetails) (*baur.Inputs, *storage.TaskRunWithID) {
if argDetails.task != nil {
inputResolver := baur.NewInputResolver(c.gitRepo, repo.Path, true)
inputFiles, err := inputResolver.Resolve(ctx, argDetails.task)
if err != nil {
stderr.TaskPrintf(argDetails.task, err.Error())
os.Exit(1)
}
return baur.NewInputs(inputFiles), nil
}
taskRun := getTaskRun(repo, argDetails)
psql := mustNewCompatibleStorage(repo)
defer psql.Close()
storageInputs, err := psql.Inputs(ctx, taskRun.ID)
exitOnErr(err)
// Convert the inputs from the DB into baur.Input interface implementation
baurInputs := toBaurInputs(storageInputs)
return baur.NewInputs(baurInputs), taskRun
}
func getTaskRun(repo *baur.Repository, argDetails *diffInputArgDetails) *storage.TaskRunWithID {
psql := mustNewCompatibleStorage(repo)
defer psql.Close()
if strings.Contains(argDetails.runID, "^") {
return getPreviousTaskRun(psql, argDetails)
}
id, err := strconv.Atoi(argDetails.runID)
exitOnErr(err)
return getTaskRunByID(psql, id)
}
func getPreviousTaskRun(psql storage.Storer, argDetails *diffInputArgDetails) *storage.TaskRunWithID {
filters := []*storage.Filter{
{
Field: storage.FieldApplicationName,
Operator: storage.OpEQ,
Value: argDetails.appName,
},
{
Field: storage.FieldTaskName,
Operator: storage.OpEQ,
Value: argDetails.taskName,
},
}
sorters := []*storage.Sorter{
{
Field: storage.FieldID,
Order: storage.OrderDesc,
},
}
var taskRun *storage.TaskRunWithID
found := errors.New("found_task_run")
runPosition := len(argDetails.runID)
retrieved := 0
err := psql.TaskRuns(
ctx,
filters,
sorters,
storage.NoLimit,
func(record *storage.TaskRunWithID) error {
retrieved++
if retrieved == runPosition {
taskRun = record
return found
}
return nil
},
)
if err != nil && errors.Unwrap(err) != found { //nolint:errorlint
exitOnErr(err)
}
if runPosition > retrieved {
exitOnErr(fmt.Errorf("run %s does not exist, only %d task-run(s) exist(s)", argDetails.arg, retrieved))
}
return taskRun
}
func getTaskRunByID(psql storage.Storer, id int) *storage.TaskRunWithID {
filters := []*storage.Filter{
{
Field: storage.FieldID,
Operator: storage.OpEQ,
Value: id,
},
}
sorters := []*storage.Sorter{
{
Field: storage.FieldID,
Order: storage.OrderDesc,
},
}
var taskRun *storage.TaskRunWithID
err := psql.TaskRuns(
ctx,
filters,
sorters,
storage.NoLimit,
func(run *storage.TaskRunWithID) error {
taskRun = run
return nil
},
)
if errors.Is(err, storage.ErrNotExist) {
err = fmt.Errorf("task-run %d does not exist", id)
}
exitOnErr(err)
return taskRun
}
func (c *diffInputsCmd) printOutput(diffs []*baur.InputDiff) {
if !c.quiet {
var formatter Formatter
if c.csv {
formatter = csv.New(nil, stdout)
} else {
headers := []string{"State", "Path", "Digest1", "Digest2"}
formatter = table.New(headers, stdout)
}
for _, diff := range diffs {
mustWriteRow(formatter, diff.State, diff.Path, diff.Digest1, diff.Digest2)
}
err := formatter.Flush()
exitOnErr(err)
}
if c.csv {
return
}
if !c.quiet {
stdout.Println()
}
if len(diffs) > 0 {
stdout.Printf("the inputs differ\n")
} else {
stdout.Printf("the inputs are the same\n")
}
}