forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_errand.go
102 lines (82 loc) · 2.23 KB
/
run_errand.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"fmt"
boshdir "github.com/cloudfoundry/bosh-cli/director"
biui "github.com/cloudfoundry/bosh-cli/ui"
boshtbl "github.com/cloudfoundry/bosh-cli/ui/table"
)
type RunErrandCmd struct {
deployment boshdir.Deployment
downloader Downloader
ui biui.UI
}
func NewRunErrandCmd(
deployment boshdir.Deployment,
downloader Downloader,
ui biui.UI,
) RunErrandCmd {
return RunErrandCmd{deployment: deployment, downloader: downloader, ui: ui}
}
func (c RunErrandCmd) Run(opts RunErrandOpts) error {
results, err := c.deployment.RunErrand(opts.Args.Name, opts.KeepAlive, opts.WhenChanged)
if err != nil {
return err
}
errandErr := c.summarize(opts.Args.Name, results)
for _, result := range results {
if opts.DownloadLogs && len(result.LogsBlobstoreID) > 0 {
err := c.downloader.Download(
result.LogsBlobstoreID,
result.LogsSHA1,
opts.Args.Name,
opts.LogsDirectory.Path,
)
if err != nil {
return bosherr.WrapError(err, "Downloading errand logs")
}
}
}
return errandErr
}
func (c RunErrandCmd) summarize(errandName string, results []boshdir.ErrandResult) error {
table := boshtbl.Table{
Content: "errand(s)",
Header: []boshtbl.Header{
boshtbl.NewHeader("Exit Code"),
boshtbl.NewHeader("Stdout"),
boshtbl.NewHeader("Stderr"),
},
SortBy: []boshtbl.ColumnSort{
{Column: 0, Asc: true},
},
Notes: []string{},
FillFirstColumn: true,
Transpose: true,
}
var errandErr error
for _, result := range results {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueInt(result.ExitCode),
boshtbl.NewValueString(result.Stdout),
boshtbl.NewValueString(result.Stderr),
})
prefix := fmt.Sprintf("Errand '%s'", errandName)
suffix := fmt.Sprintf("(exit code %d)", result.ExitCode)
switch {
case result.ExitCode == 0:
case result.ExitCode > 128:
errandErr = bosherr.Errorf("%s was canceled %s", prefix, suffix)
default:
errandErr = bosherr.Errorf("%s completed with error %s", prefix, suffix)
}
}
c.ui.PrintTable(table)
return errandErr
}
func (c RunErrandCmd) printOutput(title, output string) {
if len(output) > 0 {
c.ui.PrintLinef("%s", title)
c.ui.PrintLinef("%s", output)
}
}