Skip to content

Commit bb76827

Browse files
authored
Simplify command-line tool output format (#655)
* Simplify command-line tool output format * Print identifier before creating resources * Polish `task read` * fixup! Polish `task read` * Add verbose flag and default read status * Move read events to debug logs * Stop parsing arguments after first non-option Uses SetInterspersed(false) as in `docker run` See spf13/pflag#180 * Log identifier in the beginning * Use `BoolVar` for `--verbose` * fixup! Use `BoolVar` for `--verbose`
1 parent 64d3d0d commit bb76827

File tree

10 files changed

+65
-34
lines changed

10 files changed

+65
-34
lines changed

cmd/create/create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func New(cloud *common.Cloud) *cobra.Command {
5555
cmd.Flags().StringToStringVar(&o.Tags, "tags", map[string]string{}, "resource tags")
5656
cmd.Flags().IntVar(&o.Timeout, "timeout", 24*60*60, "timeout")
5757
cmd.Flags().StringVar(&o.Workdir, "workdir", ".", "working directory to upload")
58+
cmd.Flags().SetInterspersed(false)
5859

5960
return cmd
6061
}
@@ -115,6 +116,9 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
115116
return err
116117
}
117118

119+
logrus.Infof("Using identifier %s", id.Long())
120+
defer fmt.Println(id.Long())
121+
118122
if err := tsk.Create(ctx); err != nil {
119123
logrus.Errorf("Failed to create a new task: %v", err)
120124
logrus.Warn("Attempting to delete residual resources...")
@@ -125,6 +129,5 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
125129
return err
126130
}
127131

128-
fmt.Println(id.Long())
129132
return nil
130133
}

cmd/list/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package list
22

33
import (
44
"context"
5+
"fmt"
56

6-
"github.com/sirupsen/logrus"
77
"github.com/spf13/cobra"
88

99
"terraform-provider-iterative/task"
@@ -38,7 +38,7 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
3838
}
3939

4040
for _, id := range lst {
41-
logrus.Info(id.Long())
41+
fmt.Println(id.Long())
4242
}
4343

4444
return nil

cmd/read/read.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ import (
1313
)
1414

1515
type Options struct {
16-
Parallelism int
16+
Parallelism int
17+
Timestamps bool
18+
Status bool
19+
Events bool
20+
Logs bool
1721
}
1822

1923
func New(cloud *common.Cloud) *cobra.Command {
2024
o := Options{}
2125

2226
cmd := &cobra.Command{
2327
Use: "read <name>",
24-
Short: "Read the status of a task",
28+
Short: "Read information from an existing task",
2529
Long: ``,
2630
Args: cobra.ExactArgs(1),
2731
RunE: func(cmd *cobra.Command, args []string) error {
@@ -30,6 +34,10 @@ func New(cloud *common.Cloud) *cobra.Command {
3034
}
3135

3236
cmd.Flags().IntVar(&o.Parallelism, "parallelism", 1, "parallelism")
37+
cmd.Flags().BoolVar(&o.Timestamps, "timestamps", false, "display timestamps")
38+
cmd.Flags().BoolVar(&o.Status, "status", true, "read status")
39+
cmd.Flags().BoolVar(&o.Logs, "logs", false, "read logs")
40+
cmd.MarkFlagsMutuallyExclusive("status", "logs")
3341

3442
return cmd
3543
}
@@ -58,45 +66,61 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
5866
return err
5967
}
6068

61-
var events []string
62-
for _, event := range tsk.Events(ctx) {
63-
events = append(events, fmt.Sprintf(
64-
"%s: %s\n%s",
65-
event.Time.Format("2006-01-02 15:04:05"),
66-
event.Code,
67-
strings.Join(event.Description, "\n"),
68-
))
69+
switch {
70+
case o.Logs:
71+
return o.printLogs(ctx, tsk)
72+
case o.Status:
73+
return o.printStatus(ctx, tsk)
6974
}
70-
logrus.Info(events)
7175

76+
return nil
77+
}
78+
79+
func (o *Options) printLogs(ctx context.Context, tsk task.Task) error {
7280
logs, err := tsk.Logs(ctx)
7381
if err != nil {
7482
return err
7583
}
7684

77-
for index, log := range logs {
85+
for _, log := range logs {
7886
for _, line := range strings.Split(strings.Trim(log, "\n"), "\n") {
79-
logrus.Infof("\x1b[%dmLOG %d >> %s", 35, index, line)
87+
if !o.Timestamps {
88+
_, line, _ = strings.Cut(line, " ")
89+
}
90+
fmt.Println(line)
91+
}
92+
}
93+
94+
return nil
95+
}
96+
97+
func (o *Options) printStatus(ctx context.Context, tsk task.Task) error {
98+
for _, event := range tsk.Events(ctx) {
99+
line := fmt.Sprintf("%s: %s", event.Code, strings.Join(event.Description, " "))
100+
if o.Timestamps {
101+
line = fmt.Sprintf("%s %s", event.Time.Format("2006-01-02T15:04:05Z"), line)
80102
}
103+
104+
logrus.Debug(line)
81105
}
82106

83107
status, err := tsk.Status(ctx)
84108
if err != nil {
85109
return err
86110
}
87111

88-
message := fmt.Sprintf("\x1b[%dmStatus: queued \x1b[1m•\x1b[0m", 34)
112+
message := "queued"
89113

90114
if status["succeeded"] >= o.Parallelism {
91-
message = fmt.Sprintf("\x1b[%dmStatus: completed successfully \x1b[1m•\x1b[0m", 32)
115+
message = "succeeded"
92116
}
93117
if status["failed"] > 0 {
94-
message = fmt.Sprintf("\x1b[%dmStatus: completed with errors \x1b[1m•\x1b[0m", 31)
118+
message = "failed"
95119
}
96120
if status["running"] >= o.Parallelism {
97-
message = fmt.Sprintf("\x1b[%dmStatus: running \x1b[1m•\x1b[0m", 33)
121+
message = "running"
98122
}
99123

100-
logrus.Info(message)
124+
fmt.Println(message)
101125
return nil
102126
}

cmd/root.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
type Options struct {
2424
Region string
2525
Provider string
26-
Log string
26+
Verbose bool
2727
common.Cloud
2828
}
2929

@@ -61,18 +61,21 @@ func New() *cobra.Command {
6161
cmd.AddCommand(stop.New(&o.Cloud))
6262

6363
cmd.PersistentFlags().StringVar(&o.Provider, "cloud", "", "cloud provider")
64-
cmd.PersistentFlags().StringVar(&o.Log, "log", "info", "log level")
64+
cmd.PersistentFlags().BoolVar(&o.Verbose, "verbose", false, "verbose output")
6565
cmd.PersistentFlags().StringVar(&o.Region, "region", "us-east", "cloud region")
6666
cobra.CheckErr(cmd.MarkPersistentFlagRequired("cloud"))
6767

6868
cobra.OnInitialize(func() {
69-
switch o.Log {
70-
case "info":
71-
logrus.SetLevel(logrus.InfoLevel)
72-
case "debug":
69+
logrus.SetLevel(logrus.InfoLevel)
70+
if o.Verbose {
7371
logrus.SetLevel(logrus.DebugLevel)
7472
}
7573

74+
logrus.SetFormatter(&logrus.TextFormatter{
75+
ForceColors: true,
76+
DisableTimestamp: true,
77+
})
78+
7679
o.Cloud.Provider = common.Provider(o.Provider)
7780
o.Cloud.Region = common.Region(o.Region)
7881
})

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require (
3535
github.com/sebdah/goldie/v2 v2.5.3
3636
github.com/shirou/gopsutil v3.21.11+incompatible
3737
github.com/sirupsen/logrus v1.8.1
38-
github.com/spf13/cobra v1.4.0
38+
github.com/spf13/cobra v1.5.0
3939
github.com/spf13/pflag v1.0.5
4040
github.com/spf13/viper v1.11.0
4141
github.com/stretchr/testify v1.7.1

go.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
303303
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
304304
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
305305
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
306+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
306307
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
307308
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
308309
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -1007,8 +1008,8 @@ github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
10071008
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
10081009
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
10091010
github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk=
1010-
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
1011-
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
1011+
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
1012+
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
10121013
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
10131014
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
10141015
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=

task/common/machine/machine-script.sh.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sudo systemctl disable --now apt-daily.timer
110110

111111
while sleep 5; do
112112
test -n "$TPI_MACHINE_LOGS" && journalctl > "$TPI_LOG_DIRECTORY/machine-$TPI_MACHINE_IDENTITY"
113-
journalctl --all --no-hostname --output=short-iso --quiet --unit=tpi-task --utc | sed 's/^\([0-9-]*\)T\([0-9:]*\)+0000 \S*: \(.*\)/\1 \2 \3/g' > "$TPI_LOG_DIRECTORY/task-$TPI_MACHINE_IDENTITY"
113+
journalctl --all --no-hostname --output=short-iso --quiet --unit=tpi-task --utc | sed 's/^\([0-9-]*\)T\([0-9:]*\)+0000 \S*: \(.*\)/\1T\2Z \3/g' > "$TPI_LOG_DIRECTORY/task-$TPI_MACHINE_IDENTITY"
114114
NEW_TPI_LOG_DIRECTORY_HASH="$(md5sum "$TPI_LOG_DIRECTORY"/*)"
115115
if test "$NEW_TPI_LOG_DIRECTORY_HASH" != "$TPI_LOG_DIRECTORY_HASH"; then
116116
TPI_LOG_DIRECTORY_HASH="$NEW_TPI_LOG_DIRECTORY_HASH"

task/common/machine/testdata/machine_script_full.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sudo systemctl disable --now apt-daily.timer
110110

111111
while sleep 5; do
112112
test -n "$TPI_MACHINE_LOGS" && journalctl > "$TPI_LOG_DIRECTORY/machine-$TPI_MACHINE_IDENTITY"
113-
journalctl --all --no-hostname --output=short-iso --quiet --unit=tpi-task --utc | sed 's/^\([0-9-]*\)T\([0-9:]*\)+0000 \S*: \(.*\)/\1 \2 \3/g' > "$TPI_LOG_DIRECTORY/task-$TPI_MACHINE_IDENTITY"
113+
journalctl --all --no-hostname --output=short-iso --quiet --unit=tpi-task --utc | sed 's/^\([0-9-]*\)T\([0-9:]*\)+0000 \S*: \(.*\)/\1T\2Z \3/g' > "$TPI_LOG_DIRECTORY/task-$TPI_MACHINE_IDENTITY"
114114
NEW_TPI_LOG_DIRECTORY_HASH="$(md5sum "$TPI_LOG_DIRECTORY"/*)"
115115
if test "$NEW_TPI_LOG_DIRECTORY_HASH" != "$TPI_LOG_DIRECTORY_HASH"; then
116116
TPI_LOG_DIRECTORY_HASH="$NEW_TPI_LOG_DIRECTORY_HASH"

task/common/machine/testdata/machine_script_minimal.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sudo systemctl disable --now apt-daily.timer
110110

111111
while sleep 5; do
112112
test -n "$TPI_MACHINE_LOGS" && journalctl > "$TPI_LOG_DIRECTORY/machine-$TPI_MACHINE_IDENTITY"
113-
journalctl --all --no-hostname --output=short-iso --quiet --unit=tpi-task --utc | sed 's/^\([0-9-]*\)T\([0-9:]*\)+0000 \S*: \(.*\)/\1 \2 \3/g' > "$TPI_LOG_DIRECTORY/task-$TPI_MACHINE_IDENTITY"
113+
journalctl --all --no-hostname --output=short-iso --quiet --unit=tpi-task --utc | sed 's/^\([0-9-]*\)T\([0-9:]*\)+0000 \S*: \(.*\)/\1T\2Z \3/g' > "$TPI_LOG_DIRECTORY/task-$TPI_MACHINE_IDENTITY"
114114
NEW_TPI_LOG_DIRECTORY_HASH="$(md5sum "$TPI_LOG_DIRECTORY"/*)"
115115
if test "$NEW_TPI_LOG_DIRECTORY_HASH" != "$TPI_LOG_DIRECTORY_HASH"; then
116116
TPI_LOG_DIRECTORY_HASH="$NEW_TPI_LOG_DIRECTORY_HASH"

task/common/steps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Step struct {
1616
func RunSteps(ctx context.Context, steps []Step) error {
1717
total := len(steps)
1818
for i, step := range steps {
19-
logrus.Infof("[%d/%d] %s...", i+1, total, step.Description)
19+
logrus.Infof("[%d/%d] %s", i+1, total, step.Description)
2020
if err := step.Action(ctx); err != nil {
2121
return err
2222
}

0 commit comments

Comments
 (0)