Skip to content

Commit f970829

Browse files
authored
feat: add autostart/autostop show, show autostart/autostop schedule in ls output (#1436)
* feat: add autostart/autostop show, show autostart/autostop schedule in ls output
1 parent 9410237 commit f970829

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

cli/autostart.go

+40
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,52 @@ func autostart() *cobra.Command {
2525
Example: "coder autostart enable my-workspace --minute 30 --hour 9 --days 1-5 --tz Europe/Dublin",
2626
}
2727

28+
autostartCmd.AddCommand(autostartShow())
2829
autostartCmd.AddCommand(autostartEnable())
2930
autostartCmd.AddCommand(autostartDisable())
3031

3132
return autostartCmd
3233
}
3334

35+
func autostartShow() *cobra.Command {
36+
cmd := &cobra.Command{
37+
Use: "show <workspace_name>",
38+
Args: cobra.ExactArgs(1),
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
client, err := createClient(cmd)
41+
if err != nil {
42+
return err
43+
}
44+
organization, err := currentOrganization(cmd, client)
45+
if err != nil {
46+
return err
47+
}
48+
49+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
50+
if err != nil {
51+
return err
52+
}
53+
54+
if workspace.AutostartSchedule == "" {
55+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
56+
return nil
57+
}
58+
59+
validSchedule, err := schedule.Weekly(workspace.AutostartSchedule)
60+
if err != nil {
61+
// This should never happen.
62+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "invalid autostart schedule %q for workspace %s: %s\n", workspace.AutostartSchedule, workspace.Name, err.Error())
63+
return nil
64+
}
65+
66+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostartSchedule, validSchedule.Next(time.Now()))
67+
68+
return nil
69+
},
70+
}
71+
return cmd
72+
}
73+
3474
func autostartEnable() *cobra.Command {
3575
// yes some of these are technically numbers but the cron library will do that work
3676
var autostartMinute string

cli/autostart_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,43 @@ import (
1111

1212
"github.com/coder/coder/cli/clitest"
1313
"github.com/coder/coder/coderd/coderdtest"
14+
"github.com/coder/coder/codersdk"
1415
)
1516

1617
func TestAutostart(t *testing.T) {
1718
t.Parallel()
1819

20+
t.Run("ShowOK", func(t *testing.T) {
21+
t.Parallel()
22+
23+
var (
24+
ctx = context.Background()
25+
client = coderdtest.New(t, nil)
26+
_ = coderdtest.NewProvisionerDaemon(t, client)
27+
user = coderdtest.CreateFirstUser(t, client)
28+
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
29+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
30+
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
31+
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
32+
cmdArgs = []string{"autostart", "show", workspace.Name}
33+
sched = "CRON_TZ=Europe/Dublin 30 17 * * 1-5"
34+
stdoutBuf = &bytes.Buffer{}
35+
)
36+
37+
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
38+
Schedule: sched,
39+
})
40+
require.NoError(t, err)
41+
42+
cmd, root := clitest.New(t, cmdArgs...)
43+
clitest.SetupConfig(t, client, root)
44+
cmd.SetOut(stdoutBuf)
45+
46+
err = cmd.Execute()
47+
require.NoError(t, err, "unexpected error")
48+
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
49+
})
50+
1951
t.Run("EnableDisableOK", func(t *testing.T) {
2052
t.Parallel()
2153

cli/autostop.go

+45-4
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,59 @@ The default autostop schedule is at 18:00 in your local timezone (TZ env, UTC by
1818

1919
func autostop() *cobra.Command {
2020
autostopCmd := &cobra.Command{
21-
Use: "autostop enable <workspace>",
22-
Short: "schedule a workspace to automatically stop at a regular time",
23-
Long: autostopDescriptionLong,
24-
Example: "coder autostop enable my-workspace --minute 0 --hour 18 --days 1-5 -tz Europe/Dublin",
21+
Annotations: workspaceCommand,
22+
Use: "autostop enable <workspace>",
23+
Short: "schedule a workspace to automatically stop at a regular time",
24+
Long: autostopDescriptionLong,
25+
Example: "coder autostop enable my-workspace --minute 0 --hour 18 --days 1-5 -tz Europe/Dublin",
2526
}
2627

28+
autostopCmd.AddCommand(autostopShow())
2729
autostopCmd.AddCommand(autostopEnable())
2830
autostopCmd.AddCommand(autostopDisable())
2931

3032
return autostopCmd
3133
}
3234

35+
func autostopShow() *cobra.Command {
36+
cmd := &cobra.Command{
37+
Use: "show <workspace_name>",
38+
Args: cobra.ExactArgs(1),
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
client, err := createClient(cmd)
41+
if err != nil {
42+
return err
43+
}
44+
organization, err := currentOrganization(cmd, client)
45+
if err != nil {
46+
return err
47+
}
48+
49+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
50+
if err != nil {
51+
return err
52+
}
53+
54+
if workspace.AutostopSchedule == "" {
55+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
56+
return nil
57+
}
58+
59+
validSchedule, err := schedule.Weekly(workspace.AutostopSchedule)
60+
if err != nil {
61+
// This should never happen.
62+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "invalid autostop schedule %q for workspace %s: %s\n", workspace.AutostopSchedule, workspace.Name, err.Error())
63+
return nil
64+
}
65+
66+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostopSchedule, validSchedule.Next(time.Now()))
67+
68+
return nil
69+
},
70+
}
71+
return cmd
72+
}
73+
3374
func autostopEnable() *cobra.Command {
3475
// yes some of these are technically numbers but the cron library will do that work
3576
var autostopMinute string

cli/autostop_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,43 @@ import (
1111

1212
"github.com/coder/coder/cli/clitest"
1313
"github.com/coder/coder/coderd/coderdtest"
14+
"github.com/coder/coder/codersdk"
1415
)
1516

1617
func TestAutostop(t *testing.T) {
1718
t.Parallel()
1819

20+
t.Run("ShowOK", func(t *testing.T) {
21+
t.Parallel()
22+
23+
var (
24+
ctx = context.Background()
25+
client = coderdtest.New(t, nil)
26+
_ = coderdtest.NewProvisionerDaemon(t, client)
27+
user = coderdtest.CreateFirstUser(t, client)
28+
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
29+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
30+
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
31+
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
32+
cmdArgs = []string{"autostop", "show", workspace.Name}
33+
sched = "CRON_TZ=Europe/Dublin 30 17 * * 1-5"
34+
stdoutBuf = &bytes.Buffer{}
35+
)
36+
37+
err := client.UpdateWorkspaceAutostop(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostopRequest{
38+
Schedule: sched,
39+
})
40+
require.NoError(t, err)
41+
42+
cmd, root := clitest.New(t, cmdArgs...)
43+
clitest.SetupConfig(t, client, root)
44+
cmd.SetOut(stdoutBuf)
45+
46+
err = cmd.Execute()
47+
require.NoError(t, err, "unexpected error")
48+
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
49+
})
50+
1951
t.Run("EnableDisableOK", func(t *testing.T) {
2052
t.Parallel()
2153

cli/list.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func list() *cobra.Command {
4949
}
5050

5151
tableWriter := cliui.Table()
52-
header := table.Row{"workspace", "template", "status", "last built", "outdated"}
52+
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "autostop"}
5353
tableWriter.AppendHeader(header)
5454
tableWriter.SortBy([]table.SortBy{{
5555
Name: "workspace",
@@ -108,13 +108,25 @@ func list() *cobra.Command {
108108
durationDisplay = durationDisplay[:len(durationDisplay)-2]
109109
}
110110

111+
autostartDisplay := "not enabled"
112+
if workspace.AutostartSchedule != "" {
113+
autostartDisplay = workspace.AutostartSchedule
114+
}
115+
116+
autostopDisplay := "not enabled"
117+
if workspace.AutostopSchedule != "" {
118+
autostopDisplay = workspace.AutostopSchedule
119+
}
120+
111121
user := usersByID[workspace.OwnerID]
112122
tableWriter.AppendRow(table.Row{
113123
user.Username + "/" + workspace.Name,
114124
workspace.TemplateName,
115125
status,
116126
durationDisplay,
117127
workspace.Outdated,
128+
autostartDisplay,
129+
autostopDisplay,
118130
})
119131
}
120132
_, err = fmt.Fprintln(cmd.OutOrStdout(), tableWriter.Render())

0 commit comments

Comments
 (0)