@@ -2,6 +2,7 @@ package cli
22
33import (
44 "fmt"
5+ "os"
56 "time"
67
78 "github.com/spf13/cobra"
@@ -11,20 +12,16 @@ import (
1112)
1213
1314const autostartDescriptionLong = `To have your workspace build automatically at a regular time you can enable autostart.
14- When enabling autostart, provide a schedule. This schedule is in cron format except only
15- the following fields are allowed:
16- - minute
17- - hour
18- - day of week
19-
20- For example, to start your workspace every weekday at 9.30 am, provide the schedule '30 9 1-5'.`
15+ When enabling autostart, provide the minute, hour, and day(s) of week.
16+ The default schedule is at 09:00 in your local timezone (TZ env, UTC by default).
17+ `
2118
2219func workspaceAutostart () * cobra.Command {
2320 autostartCmd := & cobra.Command {
24- Use : "autostart enable <workspace> <schedule> " ,
21+ Use : "autostart enable <workspace>" ,
2522 Short : "schedule a workspace to automatically start at a regular time" ,
2623 Long : autostartDescriptionLong ,
27- Example : "coder workspaces autostart enable my-workspace ' 30 9 1-5' " ,
24+ Example : "coder workspaces autostart enable my-workspace --minute 30 --hour 9 --days 1-5 --tz Europe/Dublin " ,
2825 Hidden : true , // TODO(cian): un-hide when autostart scheduling implemented
2926 }
3027
@@ -35,22 +32,28 @@ func workspaceAutostart() *cobra.Command {
3532}
3633
3734func workspaceAutostartEnable () * cobra.Command {
38- return & cobra.Command {
35+ // yes some of these are technically numbers but the cron library will do that work
36+ var autostartMinute string
37+ var autostartHour string
38+ var autostartDayOfWeek string
39+ var autostartTimezone string
40+ cmd := & cobra.Command {
3941 Use : "enable <workspace_name> <schedule>" ,
4042 ValidArgsFunction : validArgsWorkspaceName ,
41- Args : cobra .ExactArgs (2 ),
43+ Args : cobra .ExactArgs (1 ),
4244 RunE : func (cmd * cobra.Command , args []string ) error {
4345 client , err := createClient (cmd )
4446 if err != nil {
4547 return err
4648 }
4749
48- workspace , err := client .WorkspaceByName (cmd .Context (), codersdk .Me , args [0 ])
50+ spec := fmt .Sprintf ("CRON_TZ=%s %s %s * * %s" , autostartTimezone , autostartMinute , autostartHour , autostartDayOfWeek )
51+ validSchedule , err := schedule .Weekly (spec )
4952 if err != nil {
5053 return err
5154 }
5255
53- validSchedule , err := schedule . Weekly ( args [1 ])
56+ workspace , err := client . WorkspaceByName ( cmd . Context (), codersdk . Me , args [0 ])
5457 if err != nil {
5558 return err
5659 }
@@ -67,6 +70,16 @@ func workspaceAutostartEnable() *cobra.Command {
6770 return nil
6871 },
6972 }
73+
74+ cmd .Flags ().StringVar (& autostartMinute , "minute" , "0" , "autostart minute" )
75+ cmd .Flags ().StringVar (& autostartHour , "hour" , "9" , "autostart hour" )
76+ cmd .Flags ().StringVar (& autostartDayOfWeek , "days" , "1-5" , "autostart day(s) of week" )
77+ tzEnv := os .Getenv ("TZ" )
78+ if tzEnv == "" {
79+ tzEnv = "UTC"
80+ }
81+ cmd .Flags ().StringVar (& autostartTimezone , "tz" , tzEnv , "autostart timezone" )
82+ return cmd
7083}
7184
7285func workspaceAutostartDisable () * cobra.Command {
0 commit comments