Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 037b348

Browse files
committed
feat: cli: prettify autostart/autostop output
1 parent 5f7cf87 commit 037b348

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

cli/autostart.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ func autostartShow() *cobra.Command {
6363
return nil
6464
}
6565

66-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostartSchedule, validSchedule.Next(time.Now()))
66+
next := validSchedule.Next(time.Now())
67+
loc, _ := time.LoadLocation(validSchedule.Timezone())
68+
69+
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
70+
"schedule: %s\ntimezone: %s\nnext: %s\n",
71+
validSchedule.Cron(),
72+
validSchedule.Timezone(),
73+
next.In(loc),
74+
)
6775

6876
return nil
6977
},

cli/autostart_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func TestAutostart(t *testing.T) {
4545

4646
err = cmd.Execute()
4747
require.NoError(t, err, "unexpected error")
48-
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
48+
// CRON_TZ gets stripped
49+
require.Contains(t, stdoutBuf.String(), "schedule: 30 17 * * 1-5")
4950
})
5051

5152
t.Run("EnableDisableOK", func(t *testing.T) {

cli/autostop.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ func autostopShow() *cobra.Command {
6363
return nil
6464
}
6565

66-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostopSchedule, validSchedule.Next(time.Now()))
66+
next := validSchedule.Next(time.Now())
67+
loc, _ := time.LoadLocation(validSchedule.Timezone())
68+
69+
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
70+
"schedule: %s\ntimezone: %s\nnext: %s\n",
71+
validSchedule.Cron(),
72+
validSchedule.Timezone(),
73+
next.In(loc),
74+
)
6775

6876
return nil
6977
},

cli/autostop_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func TestAutostop(t *testing.T) {
4545

4646
err = cmd.Execute()
4747
require.NoError(t, err, "unexpected error")
48-
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
48+
// CRON_TZ gets stripped
49+
require.Contains(t, stdoutBuf.String(), "schedule: 30 17 * * 1-5")
4950
})
5051

5152
t.Run("EnableDisableOK", func(t *testing.T) {

cli/list.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/spf13/cobra"
1111

1212
"github.com/coder/coder/cli/cliui"
13+
"github.com/coder/coder/coderd/autobuild/schedule"
1314
"github.com/coder/coder/coderd/database"
1415
"github.com/coder/coder/codersdk"
1516
)
@@ -108,14 +109,18 @@ func list() *cobra.Command {
108109
durationDisplay = durationDisplay[:len(durationDisplay)-2]
109110
}
110111

111-
autostartDisplay := "not enabled"
112+
autostartDisplay := "-"
112113
if workspace.AutostartSchedule != "" {
113-
autostartDisplay = workspace.AutostartSchedule
114+
if sched, err := schedule.Weekly(workspace.AutostartSchedule); err == nil {
115+
autostartDisplay = sched.Cron()
116+
}
114117
}
115118

116-
autostopDisplay := "not enabled"
119+
autostopDisplay := "-"
117120
if workspace.AutostopSchedule != "" {
118-
autostopDisplay = workspace.AutostopSchedule
121+
if sched, err := schedule.Weekly(workspace.AutostopSchedule); err == nil {
122+
autostopDisplay = sched.Cron()
123+
}
119124
}
120125

121126
user := usersByID[workspace.OwnerID]

0 commit comments

Comments
 (0)