Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dwahler merged 4 commits intomainfromdavid/1424-cli-workspace-namespaces
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
6 changes: 3 additions & 3 deletionscli/autostart.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletioncli/bump.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletioncli/delete.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscli/delete_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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: \"a/b/c\"")
}()
<-doneChan
})
}
22 changes: 22 additions & 0 deletionscli/root.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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.Errorf("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 deletionscli/show.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletioncli/ssh.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -207,7 +207,7 @@ func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, orgID uui
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
}
} else {
workspace, err =client.WorkspaceByOwnerAndName(cmd.Context(), orgID, userID, workspaceParts[0])
workspace, err =namedWorkspace(cmd, client, orgID, workspaceParts[0])
if err != nil {
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
}
Expand Down
2 changes: 1 addition & 1 deletioncli/start.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscli/state.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletioncli/stop.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscli/ttl.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
2 changes: 1 addition & 1 deletioncli/update.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,7 +23,7 @@ func update() *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

[8]ページ先頭

©2009-2025 Movatter.jp