10000 refactor: workspace: autostop_schedule -> ttl · coder/coder@22cd000 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22cd000

Browse files
committed
refactor: workspace: autostop_schedule -> ttl
1 parent 8814cb0 commit 22cd000

23 files changed

+311
-417
lines changed

cli/autostop.go

Lines changed: 0 additions & 167 deletions
This file was deleted.

cli/list.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func list() *cobra.Command {
5050
}
5151

5252
tableWriter := cliui.Table()
53-
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "autostop"}
53+
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "< 10000 span class="x x-first x-last">ttl"}
5454
tableWriter.AppendHeader(header)
5555
tableWriter.SortBy([]table.SortBy{{
5656
Name: "workspace",
@@ -117,10 +117,8 @@ func list() *cobra.Command {
117117
}
118118

119119
autostopDisplay := "-"
120-
if workspace.AutostopSchedule != "" {
121-
if sched, err := schedule.Weekly(workspace.AutostopSchedule); err == nil {
122-
autostopDisplay = sched.Cron()
123-
}
120+
if workspace.TTL != nil {
121+
autostopDisplay = workspace.TTL.String()
124122
}
125123

126124
user := usersByID[workspace.OwnerID]

cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func Root() *cobra.Command {
6262

6363
cmd.AddCommand(
6464
autostart(),
65-
autostop(),
6665
configSSH(),
6766
create(),
6867
delete(),
@@ -78,6 +77,7 @@ func Root() *cobra.Command {
7877
stop(),
7978
ssh(),
8079
templates(),
80+
ttl(),
8181
update(),
8282
users(),
8383
portForward(),

cli/ssh.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/coder/coder/cli/cliflag"
2222
"github.com/coder/coder/cli/cliui"
2323
"github.com/coder/coder/coderd/autobuild/notify"
24-
"github.com/coder/coder/coderd/autobuild/schedule"
2524
"github.com/coder/coder/coderd/database"
2625
"github.com/coder/coder/codersdk"
2726
"github.com/coder/coder/cryptorand"
@@ -271,16 +270,11 @@ func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID u
271270
return time.Time{}, nil
272271
}
273272

274-
if ws.AutostopSchedule == "" {
273+
if ws.TTL == nil {
275274
return time.Time{}, nil
276275
}
277276

278-
sched, err := schedule.Weekly(ws.AutostopSchedule)
279-
if err != nil {
280-
return time.Time{}, nil
281-
}
282-
283-
deadline = sched.Next(now)
277+
deadline = now.Add(*ws.TTL)
284278
callback = func() {
285279
ttl := deadline.Sub(now)
286280
var title, body string

cli/ttl.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/coder/coder/codersdk"
10+
)
11+
12+
const ttlDescriptionLong = `To have your workspace stop automatically after a configurable interval has passed.`
13+
14+
func ttl() *cobra.Command {
15+
ttlCmd := &cobra.Command{
16+
Annotations: workspaceCommand,
17+
Use: "ttl enable <workspace>",
18+
Short: "schedule a workspace to automatically stop after a configurable interval",
19+
Long: ttlDescriptionLong,
20+
Example: "coder ttl enable my-workspace 8h30m",
21+
}
22+
23+
ttlCmd.AddCommand(ttlShow())
24+
ttlCmd.AddCommand(ttlEnable())
25+
ttlCmd.AddCommand(ttlDisable())
26+
27+
return ttlCmd
28+
}
29+
30+
func ttlShow() *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: "show <workspace_name>",
33+
Args: cobra.ExactArgs(1),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
client, err := createClient(cmd)
36+
if err != nil {
37+
return err
38+
}
39+
organization, err := currentOrganization(cmd, client)
40+
if err != nil {
41+
return err
42+
}
43+
44+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
45+
if err != nil {
46+
return err
47+
}
48+
49+
if workspace.TTL == nil {
50+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
51+
return nil
52+
}
53+
54+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", workspace.TTL)
55+
56+
return nil
57+
},
58+
}
59+
return cmd
60+
}
61+
62+
func ttl 10000 Enable() *cobra.Command {
63+
cmd := &cobra.Command{
64+
Use: "enable <workspace_name> <ttl>",
65+
Args: cobra.ExactArgs(2),
66+
RunE: func(cmd *cobra.Command, args []string) error {
67+
client, err := createClient(cmd)
68+
if err != nil {
69+
return err
70+
}
71+
organization, err := currentOrganization(cmd, client)
72+
if err != nil {
73+
return err
74+
}
75+
76+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
77+
if err != nil {
78+
return err
79+
}
80+
81+
ttl, err := time.ParseDuration(args[1])
82+
if err != nil {
83+
return err
84+
}
85+
86+
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
87+
TTL: &ttl,
88+
})
89+
if err != nil {
90+
return err
91+
}
92+
93+
return nil
94+
},
95+
}
96+
97+
return cmd
98+
}
99+
100+
func ttlDisable() *cobra.Command {
101+
return &cobra.Command{
102+
Use: "disable <workspace_name>",
103+
Args: cobra.ExactArgs(1),
104+
RunE: func(cmd *cobra.Command, args []string) error {
105+
client, err := createClient(cmd)
106+
if err != nil {
107+
return err
108+
}
109+
organization, err := currentOrganization(cmd, client)
110+
if err != nil {
111+
return err
112+
}
113+
114+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
115+
if err != nil {
116+
return err
117+
}
118+
119+
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
120+
TTL: nil,
121+
})
122+
if err != nil {
123+
return err
124+
}
125+
126+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\nThe %s workspace will no longer automatically stop.\n\n", workspace.Name)
127+
128+
return nil
129+
},
130+
}
131+
}

0 commit comments

Comments
 (0)
0