Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9d9dd32

Browse files
johnstcnkylecarbs
authored andcommitted
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 parentd38be6d commit9d9dd32

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

‎cli/autostart.go

Lines changed: 40 additions & 0 deletions
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
returnautostartCmd
3233
}
3334

35+
funcautostartShow()*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+
iferr!=nil {
42+
returnerr
43+
}
44+
organization,err:=currentOrganization(cmd,client)
45+
iferr!=nil {
46+
returnerr
47+
}
48+
49+
workspace,err:=client.WorkspaceByOwnerAndName(cmd.Context(),organization.ID,codersdk.Me,args[0])
50+
iferr!=nil {
51+
returnerr
52+
}
53+
54+
ifworkspace.AutostartSchedule=="" {
55+
_,_=fmt.Fprintf(cmd.OutOrStdout(),"not enabled\n")
56+
returnnil
57+
}
58+
59+
validSchedule,err:=schedule.Weekly(workspace.AutostartSchedule)
60+
iferr!=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+
returnnil
64+
}
65+
66+
_,_=fmt.Fprintf(cmd.OutOrStdout(),"schedule: %s\nnext: %s\n",workspace.AutostartSchedule,validSchedule.Next(time.Now()))
67+
68+
returnnil
69+
},
70+
}
71+
returncmd
72+
}
73+
3474
funcautostartEnable()*cobra.Command {
3575
// yes some of these are technically numbers but the cron library will do that work
3676
varautostartMinutestring

‎cli/autostart_test.go

Lines changed: 32 additions & 0 deletions
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
funcTestAutostart(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

Lines changed: 45 additions & 4 deletions
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
funcautostop()*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
returnautostopCmd
3133
}
3234

35+
funcautostopShow()*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+
iferr!=nil {
42+
returnerr
43+
}
44+
organization,err:=currentOrganization(cmd,client)
45+
iferr!=nil {
46+
returnerr
47+
}
48+
49+
workspace,err:=client.WorkspaceByOwnerAndName(cmd.Context(),organization.ID,codersdk.Me,args[0])
50+
iferr!=nil {
51+
returnerr
52+
}
53+
54+
ifworkspace.AutostopSchedule=="" {
55+
_,_=fmt.Fprintf(cmd.OutOrStdout(),"not enabled\n")
56+
returnnil
57+
}
58+
59+
validSchedule,err:=schedule.Weekly(workspace.AutostopSchedule)
60+
iferr!=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+
returnnil
64+
}
65+
66+
_,_=fmt.Fprintf(cmd.OutOrStdout(),"schedule: %s\nnext: %s\n",workspace.AutostopSchedule,validSchedule.Next(time.Now()))
67+
68+
returnnil
69+
},
70+
}
71+
returncmd
72+
}
73+
3374
funcautostopEnable()*cobra.Command {
3475
// yes some of these are technically numbers but the cron library will do that work
3576
varautostopMinutestring

‎cli/autostop_test.go

Lines changed: 32 additions & 0 deletions
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
funcTestAutostop(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

Lines changed: 13 additions & 1 deletion
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+
ifworkspace.AutostartSchedule!="" {
113+
autostartDisplay=workspace.AutostartSchedule
114+
}
115+
116+
autostopDisplay:="not enabled"
117+
ifworkspace.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)

[8]ページ先頭

©2009-2025 Movatter.jp