8000 feat: support fully-qualified workspace names in CLI by dwahler · Pull Request #2036 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: support fully-qualified workspace names in CLI #2036

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 4 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to 10000 load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: support qualified workspace names in CLI commands
  • Loading branch information
dwahler committed Jun 3, 2022
commit d846be68b4d215d491e5da5bc8da8a3e349afb46
6 changes: 3 additions & 3 deletions cli/autostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func autostartShow() *cobra.Command {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func autostartEnable() *cobra.Command {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func autostartDisable() *cobra.Command {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func bump() *cobra.Command {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func delete() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down
54 changes: 54 additions & 0 deletions cli/delete_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cli_test

import (
"context"
"io"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
)

Expand Down Expand Up @@ -38,4 +41,55 @@ func TestDelete(t *testing.T) {
pty.ExpectMatch("Cleaning Up")
<-doneChan
})

t.Run("DifferentUser", func(t *testing.T) {
t.Parallel()
adminClient := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
adminUser := coderdtest.CreateFirstUser(t, adminClient)
orgID := adminUser.OrganizationID
client := coderdtest.CreateAnotherUser(t, adminClient, orgID)
user, err := client.User(context.Background(), codersdk.Me)
require.NoError(t, err)

version := coderdtest.CreateTemplateVersion(t, adminClient, orgID, nil)
coderdtest.AwaitTemplateVersionJob(t, adminClient, version.ID)
template := coderdtest.CreateTemplate(t, adminClient, orgID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, orgID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

cmd, root := clitest.New(t, "delete", user.Username+"/"+workspace.Name, "-y")
clitest.SetupConfig(t, adminClient, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := cmd.Execute()
// When running with the race detector on, we sometimes get an EOF.
if err != nil {
assert.ErrorIs(t, err, io.EOF)
}
}()

pty.ExpectMatch("Cleaning Up")
<-doneChan

workspace, err = client.Workspace(context.Background(), workspace.ID)
require.ErrorContains(t, err, "was deleted")
})

t.Run("InvalidWorkspaceIdentifier", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
cmd, root := clitest.New(t, "delete", "a/b/c", "-y")
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
err := cmd.Execute()
assert.ErrorContains(t, err, "invalid workspace name")
}()
<-doneChan
})
}
22 changes: 22 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"golang.org/x/xerrors"

"github.com/google/uuid"
"github.com/kirsle/configdir"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -176,6 +177,27 @@ func currentOrganization(cmd *cobra.Command, client *codersdk.Client) (codersdk.
return orgs[0], nil
}

// namedWorkspace fetches and returns a workspace by an identifier, which may be either
// a bare name (for a workspace owned by the current user) or a "user/workspace" combination,
// where user is either a username or UUID.
func namedWorkspace(cmd *cobra.Command, client *codersdk.Client, orgID uuid.UUID, identifier string) (codersdk.Workspace, error) {
parts := strings.Split(identifier, "/")

var owner, name string
switch len(parts) {
case 1:
owner = codersdk.Me
name = parts[0]
case 2:
owner = parts[0]
name = parts[1]
default:
return codersdk.Workspace{}, xerrors.New("invalid workspace name")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be helpful to include this!

Suggested change
return codersdk.Workspace{}, xerrors.New("invalid workspace name")
return codersdk.Workspace{}, xerrors.New("invalid workspace name: %q", identifier)

}

return client.WorkspaceByOwnerAndName(cmd.Context(), orgID, owner, name)
}

// createConfig consumes the global configuration flag to produce a config root.
func createConfig(cmd *cobra.Command) config.Root {
globalRoot, err := cmd.Flags().GetString(varGlobalConfig)
Expand Down
3 changes: 1 addition & 2 deletions cli/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"golang.org/x/xerrors"

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

func show() *cobra.Command {
Expand All @@ -23,7 +22,7 @@ func show() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func start() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func statePull() *cobra.Command {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func statePush() *cobra.Command {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func stop() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cli/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ttlShow() *cobra.Command {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func ttlset() *cobra.Command {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func ttlunset() *cobra.Command {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
0