8000 refactor: workspace autostop_schedule -> ttl by johnstcn · Pull Request #1578 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

refactor: workspace autostop_schedule -> ttl #1578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: workspace: autostop_schedule -> ttl
  • Loading branch information
johnstcn committed May 19, 2022
commit 3980007b48d2067cd01ba52652f63c93905689b4
167 changes: 0 additions & 167 deletions cli/autostop.go

This file was deleted.

8 changes: 3 additions & 5 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func list() *cobra.Command {
}

tableWriter := cliui.Table()
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "autostop"}
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "ttl"}
tableWriter.AppendHeader(header)
tableWriter.SortBy([]table.SortBy{{
Name: "workspace",
Expand Down Expand Up @@ -116,10 +116,8 @@ func list() *cobra.Command {
}

autostopDisplay := "-"
if workspace.AutostopSchedule != "" {
if sched, err := schedule.Weekly(workspace.AutostopSchedule); err == nil {
autostopDisplay = sched.Cron()
}
if workspace.TTL != nil {
autostopDisplay = workspace.TTL.String()
}

user := usersByID[workspace.OwnerID]
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func Root() *cobra.Command {

cmd.AddCommand(
autostart(),
autostop(),
configSSH(),
create(),
delete(),
Expand All @@ -78,6 +77,7 @@ func Root() *cobra.Command {
stop(),
ssh(),
templates(),
ttl(),
update(),
users(),
portForward(),
Expand Down
10 changes: 2 additions & 8 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/autobuild/notify"
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/cryptorand"
)
Expand Down Expand Up @@ -270,16 +269,11 @@ func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID u
return time.Time{}, nil
}

if ws.AutostopSchedule == "" {
if ws.TTL == nil {
return time.Time{}, nil
}

sched, err := schedule.Weekly(ws.AutostopSchedule)
if err != nil {
return time.Time{}, nil
}

deadline = sched.Next(now)
deadline = now.Add(*ws.TTL)
callback = func() {
ttl := deadline.Sub(now)
var title, body string
Expand Down
131 changes: 131 additions & 0 deletions cli/ttl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cli

import (
"fmt"
"time"

"github.com/spf13/cobra"

"github.com/coder/coder/codersdk"
)

const ttlDescriptionLong = `To have your workspace stop automatically after a configurable interval has passed.`

func ttl() *cobra.Command {
ttlCmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "ttl enable <workspace>",
Short: "schedule a workspace to automatically stop after a configurable interval",
Long: ttlDescriptionLong,
Example: "coder ttl enable my-workspace 8h30m",
}

ttlCmd.AddCommand(ttlShow())
ttlCmd.AddCommand(ttlEnable())
ttlCmd.AddCommand(ttlDisable())

return ttlCmd
}

func ttlShow() *cobra.Command {
cmd := &cobra.Command{
Use: "show <workspace_name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}

if workspace.TTL == nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
return nil
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", workspace.TTL)

return nil
},
}
return cmd
}

func ttlEnable() *cobra.Command {
cmd := &cobra.Command{
Use: "enable <workspace_name> <ttl>",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}

ttl, err := time.ParseDuration(args[1])
if err != nil {
return err
}

err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
TTL: &ttl,
})
if err != nil {
return err
}

return nil
},
}

return cmd
}

func ttlDisable() *cobra.Command {
return &cobra.Command{
Use: "disable <workspace_name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}

err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
TTL: nil,
})
if err != nil {
return err
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\nThe %s workspace will no longer automatically stop.\n\n", workspace.Name)

return nil
},
}
}
Loading
0