Skip to content

fix: Unnest workspaces command to the top-level #1241

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 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 10 additions & 12 deletions cli/workspaceautostart.go → cli/autostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,30 @@ When enabling autostart, provide the minute, hour, and day(s) of week.
The default schedule is at 09:00 in your local timezone (TZ env, UTC by default).
`

func workspaceAutostart() *cobra.Command {
func autostart() *cobra.Command {
autostartCmd := &cobra.Command{
Use: "autostart enable <workspace>",
Short: "schedule a workspace to automatically start at a regular time",
Long: autostartDescriptionLong,
Example: "coder workspaces autostart enable my-workspace --minute 30 --hour 9 --days 1-5 --tz Europe/Dublin",
Example: "coder autostart enable my-workspace --minute 30 --hour 9 --days 1-5 --tz Europe/Dublin",
Hidden: true,
}

autostartCmd.AddCommand(workspaceAutostartEnable())
autostartCmd.AddCommand(workspaceAutostartDisable())
autostartCmd.AddCommand(autostartEnable())
autostartCmd.AddCommand(autostartDisable())

return autostartCmd
}

func workspaceAutostartEnable() *cobra.Command {
func autostartEnable() *cobra.Command {
// yes some of these are technically numbers but the cron library will do that work
var autostartMinute string
var autostartHour string
var autostartDayOfWeek string
var autostartTimezone string
cmd := &cobra.Command{
Use: "enable <workspace_name> <schedule>",
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "enable <workspace_name> <schedule>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down Expand Up @@ -86,11 +85,10 @@ func workspaceAutostartEnable() *cobra.Command {
return cmd
}

func workspaceAutostartDisable() *cobra.Command {
func autostartDisable() *cobra.Command {
return &cobra.Command{
Use: "disable <workspace_name>",
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "disable <workspace_name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions cli/workspaceautostart_test.go → cli/autostart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/coder/coder/coderd/coderdtest"
)

func TestWorkspaceAutostart(t *testing.T) {
func TestAutostart(t *testing.T) {
t.Parallel()

t.Run("EnableDisableOK", func(t *testing.T) {
Expand All @@ -29,7 +29,7 @@ func TestWorkspaceAutostart(t *testing.T) {
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
tz = "Europe/Dublin"
cmdArgs = []string{"workspaces", "autostart", "enable", workspace.Name, "--minute", "30", "--hour", "9", "--days", "1-5", "--tz", tz}
cmdArgs = []string{"autostart", "enable", workspace.Name, "--minute", "30", "--hour", "9", "--days", "1-5", "--tz", tz}
sched = "CRON_TZ=Europe/Dublin 30 9 * * 1-5"
stdoutBuf = &bytes.Buffer{}
)
Expand All @@ -48,7 +48,7 @@ func TestWorkspaceAutostart(t *testing.T) {
require.Equal(t, sched, updated.AutostartSchedule, "expected autostart schedule to be set")

// Disable schedule
cmd, root = clitest.New(t, "workspaces", "autostart", "disable", workspace.Name)
cmd, root = clitest.New(t, "autostart", "disable", workspace.Name)
clitest.SetupConfig(t, client, root)
cmd.SetOut(stdoutBuf)

Expand All @@ -73,7 +73,7 @@ func TestWorkspaceAutostart(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
)

cmd, root := clitest.New(t, "workspaces", "autostart", "enable", "doesnotexist")
cmd, root := clitest.New(t, "autostart", "enable", "doesnotexist")
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
Expand All @@ -91,7 +91,7 @@ func TestWorkspaceAutostart(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
)

cmd, root := clitest.New(t, "workspaces", "autostart", "disable", "doesnotexist")
cmd, root := clitest.New(t, "autostart", "disable", "doesnotexist")
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
Expand All @@ -118,7 +118,7 @@ func TestWorkspaceAutostart(t *testing.T) {
currTz = "UTC"
}
expectedSchedule := fmt.Sprintf("CRON_TZ=%s 0 9 * * 1-5", currTz)
cmd, root := clitest.New(t, "workspaces", "autostart", "enable", workspace.Name)
cmd, root := clitest.New(t, "autostart", "enable", workspace.Name)
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
Expand Down
22 changes: 10 additions & 12 deletions cli/workspaceautostop.go → cli/autostop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,30 @@ When enabling autostop, provide the minute, hour, and day(s) of week.
The default autostop schedule is at 18:00 in your local timezone (TZ env, UTC by default).
`

func workspaceAutostop() *cobra.Command {
func autostop() *cobra.Command {
autostopCmd := &cobra.Command{
Use: "autostop enable <workspace>",
Short: "schedule a workspace to automatically stop at a regular time",
Long: autostopDescriptionLong,
Example: "coder workspaces autostop enable my-workspace --minute 0 --hour 18 --days 1-5 -tz Europe/Dublin",
Example: "coder autostop enable my-workspace --minute 0 --hour 18 --days 1-5 -tz Europe/Dublin",
Hidden: true,
}

autostopCmd.AddCommand(workspaceAutostopEnable())
autostopCmd.AddCommand(workspaceAutostopDisable())
autostopCmd.AddCommand(autostopEnable())
autostopCmd.AddCommand(autostopDisable())

return autostopCmd
}

func workspaceAutostopEnable() *cobra.Command {
func autostopEnable() *cobra.Command {
// yes some of these are technically numbers but the cron library will do that work
var autostopMinute string
var autostopHour string
var autostopDayOfWeek string
var autostopTimezone string
cmd := &cobra.Command{
Use: "enable <workspace_name> <schedule>",
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "enable <workspace_name> <schedule>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down Expand Up @@ -86,11 +85,10 @@ func workspaceAutostopEnable() *cobra.Command {
return cmd
}

func workspaceAutostopDisable() *cobra.Command {
func autostopDisable() *cobra.Command {
return &cobra.Command{
Use: "disable <workspace_name>",
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "disable <workspace_name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions cli/workspaceautostop_test.go → cli/autostop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/coder/coder/coderd/coderdtest"
)

func TestWorkspaceAutostop(t *testing.T) {
func TestAutostop(t *testing.T) {
t.Parallel()

t.Run("EnableDisableOK", func(t *testing.T) {
Expand All @@ -28,7 +28,7 @@ func TestWorkspaceAutostop(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
cmdArgs = []string{"workspaces", "autostop", "enable", workspace.Name, "--minute", "30", "--hour", "17", "--days", "1-5", "--tz", "Europe/Dublin"}
cmdArgs = []string{"autostop", "enable", workspace.Name, "--minute", "30", "--hour", "17", "--days", "1-5", "--tz", "Europe/Dublin"}
sched = "CRON_TZ=Europe/Dublin 30 17 * * 1-5"
stdoutBuf = &bytes.Buffer{}
)
Expand All @@ -47,7 +47,7 @@ func TestWorkspaceAutostop(t *testing.T) {
require.Equal(t, sched, updated.AutostopSchedule, "expected autostop schedule to be set")

// Disable schedule
cmd, root = clitest.New(t, "workspaces", "autostop", "disable", workspace.Name)
cmd, root = clitest.New(t, "autostop", "disable", workspace.Name)
clitest.SetupConfig(t, client, root)
cmd.SetOut(stdoutBuf)

Expand All @@ -72,7 +72,7 @@ func TestWorkspaceAutostop(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
)

cmd, root := clitest.New(t, "workspaces", "autostop", "enable", "doesnotexist")
cmd, root := clitest.New(t, "autostop", "enable", "doesnotexist")
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
Expand All @@ -90,7 +90,7 @@ func TestWorkspaceAutostop(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
)

cmd, root := clitest.New(t, "workspaces", "autostop", "disable", "doesnotexist")
cmd, root := clitest.New(t, "autostop", "disable", "doesnotexist")
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestWorkspaceAutostop(t *testing.T) {
}
expectedSchedule := fmt.Sprintf("CRON_TZ=%s 0 18 * * 1-5", currTz)

cmd, root := clitest.New(t, "workspaces", "autostop", "enable", workspace.Name)
cmd, root := clitest.New(t, "autostop", "enable", workspace.Name)
clitest.SetupConfig(t, client, root)

err := cmd.Execute()
Expand Down
2 changes: 1 addition & 1 deletion cli/cliui/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
defer resourceMutex.Unlock()
message := "Don't panic, your workspace is booting up!"
if agent.Status == codersdk.WorkspaceAgentDisconnected {
message = "The workspace agent lost connection! Wait for it to reconnect or run: " + Styles.Code.Render("coder workspaces rebuild "+opts.WorkspaceName)
message = "The workspace agent lost connection! Wait for it to reconnect or run: " + Styles.Code.Render("coder rebuild "+opts.WorkspaceName)
}
// This saves the cursor position, then defers clearing from the cursor
// position to the end of the screen.
Expand Down
2 changes: 1 addition & 1 deletion cli/workspacecreate.go → cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/coder/coder/codersdk"
)

func workspaceCreate() *cobra.Command {
func create() *cobra.Command {
var (
workspaceName string
templateName string
Expand Down
10 changes: 5 additions & 5 deletions cli/workspacecreate_test.go → cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/coder/coder/pty/ptytest"
)

func TestWorkspaceCreate(t *testing.T) {
func TestCreate(t *testing.T) {
t.Parallel()
t.Run("Create", func(t *testing.T) {
t.Parallel()
Expand All @@ -22,7 +22,7 @@ func TestWorkspaceCreate(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
cmd, root := clitest.New(t, "workspaces", "create", "my-workspace", "--template", template.Name)
cmd, root := clitest.New(t, "create", "my-workspace", "--template", template.Name)
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestWorkspaceCreate(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
cmd, root := clitest.New(t, "workspaces", "create", "my-workspace")
cmd, root := clitest.New(t, "create", "my-workspace")
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestWorkspaceCreate(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
cmd, root := clitest.New(t, "workspaces", "create", "")
cmd, root := clitest.New(t, "create", "")
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestWorkspaceCreate(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
cmd, root := clitest.New(t, "workspaces", "create", "")
cmd, root := clitest.New(t, "create", "")
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
Expand Down
10 changes: 5 additions & 5 deletions cli/workspacedelete.go → cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/coder/coder/codersdk"
)

func workspaceDelete() *cobra.Command {
// nolint
func delete() *cobra.Command {
return &cobra.Command{
Use: "delete <workspace>",
Aliases: []string{"rm"},
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "delete <workspace>",
Aliases: []string{"rm"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cli/workspacelist.go → cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/coder/coder/codersdk"
)

func workspaceList() *cobra.Command {
func list() *cobra.Command {
return &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Expand All @@ -31,7 +31,7 @@ func workspaceList() *cobra.Command {
if len(workspaces) == 0 {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"No workspaces found! Create one:")
_, _ = fmt.Fprintln(cmd.OutOrStdout())
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+cliui.Styles.Code.Render("coder workspaces create <name>"))
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+cliui.Styles.Code.Render("coder create <name>"))
_, _ = fmt.Fprintln(cmd.OutOrStdout())
return nil
}
Expand Down
20 changes: 14 additions & 6 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,25 @@ func Root() *cobra.Command {
cmd.SetVersionTemplate(versionTemplate())

cmd.AddCommand(
autostart(),
autostop(),
configSSH(),
server(),
create(),
delete(),
gitssh(),
list(),
login(),
parameters(),
publickey(),
server(),
show(),
start(),
stop(),
ssh(),
templates(),
update(),
users(),
workspaces(),
ssh(),
workspaceTunnel(),
gitssh(),
publickey(),
tunnel(),
workspaceAgent(),
)

Expand Down
2 changes: 1 addition & 1 deletion cli/workspaceshow.go → cli/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/coder/coder/codersdk"
)

func workspaceShow() *cobra.Command {
func show() *cobra.Command {
return &cobra.Command{
Use: "show",
Args: cobra.ExactArgs(1),
Expand Down
7 changes: 3 additions & 4 deletions cli/workspacestart.go → cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"github.com/coder/coder/codersdk"
)

func workspaceStart() *cobra.Command {
func start() *cobra.Command {
return &cobra.Command{
Use: "start <workspace>",
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "start <workspace>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cli/workspacestop.go → cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"github.com/coder/coder/codersdk"
)

func workspaceStop() *cobra.Command {
func stop() *cobra.Command {
return &cobra.Command{
Use: "stop <workspace>",
ValidArgsFunction: validArgsWorkspaceName,
Args: cobra.ExactArgs(1),
Use: "stop <workspace>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func templateCreate() *cobra.Command {
"The "+cliui.Styles.Keyword.Render(templateName)+" template has been created! "+
"Developers can provision a workspace with this template using:")+"\n")

_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+cliui.Styles.Code.Render(fmt.Sprintf("coder workspaces create --template=%q [workspace name]", templateName)))
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+cliui.Styles.Code.Render(fmt.Sprintf("coder create --template=%q [workspace name]", templateName)))
_, _ = fmt.Fprintln(cmd.OutOrStdout())

return nil
Expand Down
Loading