Skip to content

Commit c8adca1

Browse files
committed
wip: Add rename command
1 parent 95e95ed commit c8adca1

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

cli/rename.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cli
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"golang.org/x/xerrors"
6+
7+
"github.com/coder/coder/codersdk"
8+
)
9+
10+
func rename() *cobra.Command {
11+
return &cobra.Command{
12+
Annotations: workspaceCommand,
13+
Use: "rename <workspace> <new name>",
14+
Short: "Rename a workspace",
15+
Args: cobra.ExactArgs(2),
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
client, err := createClient(cmd)
18+
if err != nil {
19+
return err
20+
}
21+
workspace, err := namedWorkspace(cmd, client, args[0])
22+
if err != nil {
23+
return xerrors.Errorf("get workspace: %w", err)
24+
}
25+
err = client.UpdateWorkspace(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceRequest{
26+
Name: args[1],
27+
})
28+
if err != nil {
29+
return xerrors.Errorf("rename workspace: %w", err)
30+
}
31+
return nil
32+
},
33+
}
34+
}

cli/rename_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cli_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/coder/coder/cli/clitest"
10+
"github.com/coder/coder/coderd/coderdtest"
11+
"github.com/coder/coder/pty/ptytest"
12+
"github.com/coder/coder/testutil"
13+
)
14+
15+
func TestRename(t *testing.T) {
16+
t.Parallel()
17+
18+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
19+
user := coderdtest.CreateFirstUser(t, client)
20+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
21+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
22+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
23+
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
24+
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
25+
26+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
27+
defer cancel()
28+
29+
want := workspace.Name + "-test"
30+
cmd, root := clitest.New(t, "rename", workspace.Name, want)
31+
clitest.SetupConfig(t, client, root)
32+
pty := ptytest.New(t)
33+
cmd.SetIn(pty.Input())
34+
cmd.SetOut(pty.Output())
35+
36+
err := cmd.ExecuteContext(ctx)
37+
assert.NoError(t, err)
38+
39+
ws, err := client.Workspace(ctx, workspace.ID)
40+
assert.NoError(t, err)
41+
42+
got := ws.Name
43+
assert.Equal(t, want, got, "workspace name did not change")
44+
}

0 commit comments

Comments
 (0)