Skip to content

Commit 352058b

Browse files
committed
Add auto start to template edit
1 parent 18c7093 commit 352058b

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

cli/templateedit.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ import (
1717

1818
func (r *RootCmd) templateEdit() *clibase.Cmd {
1919
var (
20-
name string
21-
displayName string
22-
description string
23-
icon string
24-
defaultTTL time.Duration
25-
maxTTL time.Duration
26-
autostopRequirementDaysOfWeek []string
27-
autostopRequirementWeeks int64
28-
failureTTL time.Duration
29-
inactivityTTL time.Duration
30-
allowUserCancelWorkspaceJobs bool
31-
allowUserAutostart bool
32-
allowUserAutostop bool
20+
name string
21+
displayName string
22+
description string
23+
icon string
24+
defaultTTL time.Duration
25+
maxTTL time.Duration
26+
autostopRequirementDaysOfWeek []string
27+
autostopRequirementWeeks int64
28+
autostartRequirementDaysOfWeek []string
29+
failureTTL time.Duration
30+
inactivityTTL time.Duration
31+
allowUserCancelWorkspaceJobs bool
32+
allowUserAutostart bool
33+
allowUserAutostop bool
3334
)
3435
client := new(codersdk.Client)
3536

@@ -48,7 +49,9 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
4849
!allowUserAutostop ||
4950
maxTTL != 0 ||
5051
failureTTL != 0 ||
51-
inactivityTTL != 0
52+
inactivityTTL != 0 ||
53+
len(autostartRequirementDaysOfWeek) > 0
54+
5255
if requiresEntitlement {
5356
entitlements, err := client.Entitlements(inv.Context())
5457
var sdkErr *codersdk.Error
@@ -77,6 +80,12 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
7780
if len(autostopRequirementDaysOfWeek) == 0 {
7881
autostopRequirementDaysOfWeek = template.AutostopRequirement.DaysOfWeek
7982
}
83+
if len(autostartRequirementDaysOfWeek) == 1 && autostartRequirementDaysOfWeek[0] == "all" {
84+
// Set it to every day of the week
85+
autostartRequirementDaysOfWeek = []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
86+
} else if len(autostartRequirementDaysOfWeek) == 0 {
87+
autostartRequirementDaysOfWeek = template.AutostartRequirement.DaysOfWeek
88+
}
8089
if unsetAutostopRequirementDaysOfWeek {
8190
autostopRequirementDaysOfWeek = []string{}
8291
}
@@ -93,6 +102,9 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
93102
DaysOfWeek: autostopRequirementDaysOfWeek,
94103
Weeks: autostopRequirementWeeks,
95104
},
105+
AutostartRequirement: &codersdk.TemplateAutostartRequirement{
106+
DaysOfWeek: autostartRequirementDaysOfWeek,
107+
},
96108
FailureTTLMillis: failureTTL.Milliseconds(),
97109
TimeTilDormantMillis: inactivityTTL.Milliseconds(),
98110
AllowUserCancelWorkspaceJobs: allowUserCancelWorkspaceJobs,
@@ -140,6 +152,22 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
140152
Description: "Edit the template maximum time before shutdown - workspaces created from this template must shutdown within the given duration after starting, regardless of user activity. This is an enterprise-only feature. Maps to \"Max lifetime\" in the UI.",
141153
Value: clibase.DurationOf(&maxTTL),
142154
},
155+
{
156+
Flag: "autostart-requirement-weekdays",
157+
// workspaces created from this template must be restarted on the given weekdays. To unset this value for the template (and disable the autostop requirement for the template), pass 'none'.
158+
Description: "Edit the template autostart requirement weekdays - workspaces created from this template can only autostart on the given weekdays. To unset this value for the template (and allow autostart on all days), pass 'all'.",
159+
Value: clibase.Validate(clibase.StringArrayOf(&autostartRequirementDaysOfWeek), func(value *clibase.StringArray) error {
160+
v := value.GetSlice()
161+
if len(v) == 1 && v[0] == "all" {
162+
return nil
163+
}
164+
_, err := codersdk.WeekdaysToBitmap(v)
165+
if err != nil {
166+
return xerrors.Errorf("invalid autostart requirement days of week %q: %w", strings.Join(v, ","), err)
167+
}
168+
return nil
169+
}),
170+
},
143171
{
144172
Flag: "autostop-requirement-weekdays",
145173
Description: "Edit the template autostop requirement weekdays - workspaces created from this template must be restarted on the given weekdays. To unset this value for the template (and disable the autostop requirement for the template), pass 'none'.",

0 commit comments

Comments
 (0)