fix: cli: prettify schedule when printing output (#1440)

* Adds methods to schedule.Schedule to show the raw cron string and timezone
* Uses these methods to clean up output of auto(start|stop) show or ls
* Defaults CRON_TZ=UTC if not provided
This commit is contained in:
Cian Johnston
2022-05-16 17:02:44 +01:00
committed by GitHub
parent 2a278b8698
commit b7049032a0
7 changed files with 77 additions and 16 deletions
+9 -1
View File
@@ -63,7 +63,15 @@ func autostartShow() *cobra.Command {
return nil
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostartSchedule, validSchedule.Next(time.Now()))
next := validSchedule.Next(time.Now())
loc, _ := time.LoadLocation(validSchedule.Timezone())
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
"schedule: %s\ntimezone: %s\nnext: %s\n",
validSchedule.Cron(),
validSchedule.Timezone(),
next.In(loc),
)
return nil
},
+2 -1
View File
@@ -45,7 +45,8 @@ func TestAutostart(t *testing.T) {
err = cmd.Execute()
require.NoError(t, err, "unexpected error")
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
// CRON_TZ gets stripped
require.Contains(t, stdoutBuf.String(), "schedule: 30 17 * * 1-5")
})
t.Run("EnableDisableOK", func(t *testing.T) {
+9 -1
View File
@@ -63,7 +63,15 @@ func autostopShow() *cobra.Command {
return nil
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostopSchedule, validSchedule.Next(time.Now()))
next := validSchedule.Next(time.Now())
loc, _ := time.LoadLocation(validSchedule.Timezone())
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
"schedule: %s\ntimezone: %s\nnext: %s\n",
validSchedule.Cron(),
validSchedule.Timezone(),
next.In(loc),
)
return nil
},
+2 -1
View File
@@ -45,7 +45,8 @@ func TestAutostop(t *testing.T) {
err = cmd.Execute()
require.NoError(t, err, "unexpected error")
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
// CRON_TZ gets stripped
require.Contains(t, stdoutBuf.String(), "schedule: 30 17 * * 1-5")
})
t.Run("EnableDisableOK", func(t *testing.T) {
+9 -4
View File
@@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"
)
@@ -108,14 +109,18 @@ func list() *cobra.Command {
durationDisplay = durationDisplay[:len(durationDisplay)-2]
}
autostartDisplay := "not enabled"
autostartDisplay := "-"
if workspace.AutostartSchedule != "" {
autostartDisplay = workspace.AutostartSchedule
if sched, err := schedule.Weekly(workspace.AutostartSchedule); err == nil {
autostartDisplay = sched.Cron()
}
}
autostopDisplay := "not enabled"
autostopDisplay := "-"
if workspace.AutostopSchedule != "" {
autostopDisplay = workspace.AutostopSchedule
if sched, err := schedule.Weekly(workspace.AutostopSchedule); err == nil {
autostopDisplay = sched.Cron()
}
}
user := usersByID[workspace.OwnerID]