-
Notifications
You must be signed in to change notification settings - Fork 10
/
ls_outputs.go
109 lines (85 loc) · 2.02 KB
/
ls_outputs.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
package command
import (
"os"
"strconv"
"github.com/spf13/cobra"
"github.com/simplesurance/baur/format"
"github.com/simplesurance/baur/format/csv"
"github.com/simplesurance/baur/format/table"
"github.com/simplesurance/baur/log"
)
var lsOutputsCmd = &cobra.Command{
Use: "outputs <BUILD-ID>",
Short: "list outputs for a build",
Run: lsOutputs,
Args: cobra.ExactArgs(1),
}
type lsOutputsConfig struct {
quiet bool
csv bool
}
var lsOutputsConf lsOutputsConfig
func init() {
lsOutputsCmd.Flags().BoolVar(&lsOutputsConf.csv, "csv", false,
"Show output in RFC4180 CSV format")
lsOutputsCmd.Flags().BoolVarP(&lsOutputsConf.quiet, "quiet", "q", false,
"Only show URIs")
lsCmd.AddCommand(lsOutputsCmd)
}
func lsOutputs(cmd *cobra.Command, args []string) {
repo := MustFindRepository()
pgClient := MustGetPostgresClt(repo)
buildID, err := strconv.Atoi(args[0])
if err != nil {
log.Fatalf("'%s' is not a numeric build ID", args[0])
}
exist, err := pgClient.BuildExist(buildID)
if err != nil {
log.Fatalln(err)
}
if !exist {
log.Fatalf("build with ID %d does not exist", buildID)
}
outputs, err := pgClient.GetBuildOutputs(buildID)
if err != nil {
log.Fatalln(err)
}
formatter := getLsOutputsFormatter(lsOutputsConf.quiet, lsOutputsConf.csv)
for _, o := range outputs {
var row []interface{}
if lsOutputsConf.quiet {
row = []interface{}{o.Upload.URI}
} else {
row = []interface{}{
o.Upload.URI,
o.Digest,
bytesToMib(int(o.SizeBytes)),
durationToStrSeconds(o.Upload.UploadDuration),
o.Type,
o.Upload.Method,
}
}
mustWriteRow(formatter, row)
}
if err = formatter.Flush(); err != nil {
log.Fatalln(err)
}
}
func getLsOutputsFormatter(isQuiet, isCsv bool) format.Formatter {
var headers []string
if isCsv {
return csv.New(headers, os.Stdout)
}
if isQuiet {
return table.New(headers, os.Stdout)
}
headers = []string{
"URI",
"Digest",
"Size (MiB)",
"Upload Duration (s)",
"Output Type",
"Method",
}
return table.New(headers, os.Stdout)
}