-
Notifications
You must be signed in to change notification settings - Fork 10
/
ls_inputs.go
104 lines (82 loc) · 2.33 KB
/
ls_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
package command
import (
"fmt"
"os"
"sort"
"github.com/spf13/cobra"
"github.com/simplesurance/baur"
"github.com/simplesurance/baur/format"
"github.com/simplesurance/baur/format/csv"
"github.com/simplesurance/baur/format/table"
"github.com/simplesurance/baur/log"
)
type lsInputsConf struct {
quiet bool
showDigest bool
csv bool
}
var lsInputsCmd = &cobra.Command{
Use: "inputs [<APP-NAME>|<PATH>]",
Short: "list resolved build inputs of an application",
Run: lsInputs,
Args: cobra.ExactArgs(1),
}
var lsInputsConfig lsInputsConf
func init() {
lsInputsCmd.Flags().BoolVar(&lsInputsConfig.csv, "csv", false,
"Show output in RFC4180 CSV format")
lsInputsCmd.Flags().BoolVarP(&lsInputsConfig.quiet, "quiet", "q", false,
"Only show filepaths")
lsInputsCmd.Flags().BoolVar(&lsInputsConfig.showDigest, "digests", false,
"show digests")
lsCmd.AddCommand(lsInputsCmd)
}
func lsInputs(cmd *cobra.Command, args []string) {
var formatter format.Formatter
var headers []string
rep := MustFindRepository()
app := mustArgToApp(rep, args[0])
writeHeaders := !lsInputsConfig.quiet && !lsInputsConfig.csv
if !app.HasBuildInputs() {
log.Fatalf("No build inputs are configured in %s of %s", baur.AppCfgFile, app.Name)
}
if writeHeaders {
headers = []string{"Path"}
if lsInputsConfig.showDigest {
headers = append(headers, "Digest")
}
}
if lsInputsConfig.csv {
formatter = csv.New(headers, os.Stdout)
} else {
formatter = table.New(headers, os.Stdout)
}
inputs, err := app.BuildInputs()
if err != nil {
log.Fatalln(err)
}
sort.Slice(inputs, func(i, j int) bool {
return inputs[i].RepoRelPath() < inputs[j].RepoRelPath()
})
for _, input := range inputs {
if !lsInputsConfig.showDigest || lsInputsConfig.quiet {
mustWriteRow(formatter, []interface{}{input})
continue
}
digest, err := input.Digest()
if err != nil {
log.Fatalln("calculating digest failed:", err)
}
mustWriteRow(formatter, []interface{}{input, digest.String()})
}
if err := formatter.Flush(); err != nil {
log.Fatalln(err)
}
if lsInputsConfig.showDigest && !lsInputsConfig.quiet && !lsInputsConfig.csv {
totalDigest, err := app.TotalInputDigest()
if err != nil {
log.Fatalln("calculating total input digest failed:", err)
}
fmt.Printf("\nTotal Build Input Digest: %s\n", highlight(totalDigest.String()))
}
}