-
Notifications
You must be signed in to change notification settings - Fork 11
/
ls_apps.go
218 lines (176 loc) · 4.97 KB
/
ls_apps.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
package command
import (
"fmt"
"os"
"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/internal/command/flag"
"github.com/simplesurance/baur/log"
"github.com/simplesurance/baur/storage"
)
const (
lsAppNameHeader = "Name"
lsAppNameParam = "name"
lsAppPathHeader = "Path"
lsAppPathParam = "path"
lsAppBuildStatusHeader = "Build Status"
lsAppBuildStatusParam = "build-status"
lsAppBuildIDHeader = "Build ID"
lsAppBuildIDParam = "build-id"
lsAppGitCommitHeader = "Git Commit"
lsAppGitCommitParam = "git-commit"
)
type lsAppsConf struct {
csv bool
quiet bool
absPaths bool
buildStatus flag.BuildStatus
fields *flag.Fields
}
var lsAppsCmd = &cobra.Command{
Use: "apps [<APP-NAME>|<PATH>]...",
Short: "list applications and their status",
Run: ls,
Args: cobra.ArbitraryArgs,
}
var lsAppsConfig lsAppsConf
func init() {
lsAppsCmd.Flags().BoolVar(&lsAppsConfig.csv, "csv", false,
"List applications in RFC4180 CSV format")
lsAppsCmd.Flags().BoolVarP(&lsAppsConfig.quiet, "quiet", "q", false,
"Suppress printing a header and progress dots")
lsAppsCmd.Flags().BoolVar(&lsAppsConfig.absPaths, "abs-path", false,
"Show absolute instead of relative paths")
lsAppsCmd.Flags().VarP(&lsAppsConfig.buildStatus, "build-status", "s",
lsAppsConfig.buildStatus.Usage(highlight))
lsAppsConfig.fields = flag.NewFields([]string{
lsAppNameParam,
lsAppPathParam,
lsAppBuildIDParam,
lsAppBuildStatusParam,
lsAppGitCommitParam,
})
lsAppsCmd.Flags().VarP(lsAppsConfig.fields, "fields", "f",
lsAppsConfig.fields.Usage(highlight))
lsCmd.AddCommand(lsAppsCmd)
}
func createHeader() []string {
var headers []string
for _, f := range lsAppsConfig.fields.Fields {
switch f {
case lsAppNameParam:
headers = append(headers, lsAppNameHeader)
case lsAppPathParam:
headers = append(headers, lsAppPathHeader)
case lsAppBuildStatusParam:
headers = append(headers, lsAppBuildStatusHeader)
case lsAppBuildIDParam:
headers = append(headers, lsAppBuildIDHeader)
case lsAppGitCommitParam:
headers = append(headers, lsAppGitCommitHeader)
default:
panic(fmt.Sprintf("unsupported value '%v' in fields parameter", f))
}
}
return headers
}
func ls(cmd *cobra.Command, args []string) {
var headers []string
var formatter format.Formatter
var storageClt storage.Storer
repo := MustFindRepository()
apps := mustArgToApps(repo, args)
writeHeaders := !lsAppsConfig.quiet && !lsAppsConfig.csv
storageQueryNeeded := storageQueryIsNeeded()
if storageQueryNeeded {
storageClt = MustGetPostgresClt(repo)
}
if writeHeaders {
headers = createHeader()
}
if lsAppsConfig.csv {
formatter = csv.New(headers, os.Stdout)
} else {
formatter = table.New(headers, os.Stdout)
}
showProgress := len(apps) >= 5 && !lsAppsConfig.quiet && !lsAppsConfig.csv
baur.SortAppsByName(apps)
for i, app := range apps {
var row []interface{}
var build *storage.BuildWithDuration
var buildStatus baur.BuildStatus
if storageQueryNeeded {
var err error
buildStatus, build, err = baur.GetBuildStatus(storageClt, app)
if err != nil {
log.Fatalf("gathering informations for %s failed: %s", app, err)
}
// querying the build status for all applications can
// take some time, output progress dots to let the user
// know that something is happening
if showProgress {
fmt.Printf(".")
if i+1 == len(apps) {
fmt.Printf("\n\n")
}
}
}
if lsAppsConfig.buildStatus.IsSet() && buildStatus != lsAppsConfig.buildStatus.Status {
continue
}
row = assembleRow(app, build, buildStatus)
if err := formatter.WriteRow(row); err != nil {
log.Fatalln(err)
}
}
if err := formatter.Flush(); err != nil {
log.Fatalln(err)
}
}
func storageQueryIsNeeded() bool {
for _, f := range lsAppsConfig.fields.Fields {
switch f {
case lsAppBuildStatusParam:
return true
case lsAppBuildIDParam:
return true
case lsAppGitCommitParam:
return true
}
}
return false
}
func assembleRow(app *baur.App, build *storage.BuildWithDuration, buildStatus baur.BuildStatus) []interface{} {
var row []interface{}
for _, f := range lsAppsConfig.fields.Fields {
switch f {
case lsAppNameParam:
row = append(row, app.Name)
case lsAppPathParam:
if lsAppsConfig.absPaths {
row = append(row, app.Path)
} else {
row = append(row, app.RelPath)
}
case lsAppBuildStatusParam:
row = append(row, buildStatus)
case lsAppBuildIDParam:
if buildStatus == baur.BuildStatusExist {
row = append(row, fmt.Sprint(build.ID))
} else {
// no build exist, we don't have a build id
row = append(row, "")
}
case lsAppGitCommitParam:
if buildStatus == baur.BuildStatusExist {
row = append(row, fmt.Sprint(build.VCSState.CommitID))
} else {
row = append(row, "")
}
}
}
return row
}