Skip to content

Commit 647101b

Browse files
authored
feat: add --shared-with-me flag to coder list command (coder#19948)
Closes [coder/internal#1013](coder/internal#1013)
1 parent a78790c commit 647101b

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

cli/list.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (r *RootCmd) list() *serpent.Command {
9595
),
9696
cliui.JSONFormat(),
9797
)
98+
sharedWithMe bool
9899
)
99100
cmd := &serpent.Command{
100101
Annotations: workspaceCommand,
@@ -104,13 +105,36 @@ func (r *RootCmd) list() *serpent.Command {
104105
Middleware: serpent.Chain(
105106
serpent.RequireNArgs(0),
106107
),
108+
Options: serpent.OptionSet{
109+
{
110+
Name: "shared-with-me",
111+
Description: "Show workspaces shared with you.",
112+
Flag: "shared-with-me",
113+
Value: serpent.BoolOf(&sharedWithMe),
114+
Hidden: true,
115+
},
116+
},
107117
Handler: func(inv *serpent.Invocation) error {
108118
client, err := r.InitClient(inv)
109119
if err != nil {
110120
return err
111121
}
112122

113-
res, err := QueryConvertWorkspaces(inv.Context(), client, filter.Filter(), WorkspaceListRowFromWorkspace)
123+
workspaceFilter := filter.Filter()
124+
if sharedWithMe {
125+
user, err := client.User(inv.Context(), codersdk.Me)
126+
if err != nil {
127+
return xerrors.Errorf("fetch current user: %w", err)
128+
}
129+
workspaceFilter.SharedWithUser = user.ID.String()
130+
131+
// Unset the default query that conflicts with the --shared-with-me flag
132+
if workspaceFilter.FilterQuery == "owner:me" {
133+
workspaceFilter.FilterQuery = ""
134+
}
135+
}
136+
137+
res, err := QueryConvertWorkspaces(inv.Context(), client, workspaceFilter, WorkspaceListRowFromWorkspace)
114138
if err != nil {
115139
return err
116140
}

cli/list_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/coder/coder/v2/coderd/coderdtest"
1414
"github.com/coder/coder/v2/coderd/database"
1515
"github.com/coder/coder/v2/coderd/database/dbfake"
16+
"github.com/coder/coder/v2/coderd/rbac"
1617
"github.com/coder/coder/v2/codersdk"
1718
"github.com/coder/coder/v2/pty/ptytest"
1819
"github.com/coder/coder/v2/testutil"
@@ -100,4 +101,49 @@ func TestList(t *testing.T) {
100101

101102
require.Len(t, stderr.Bytes(), 0)
102103
})
104+
105+
t.Run("SharedWorkspaces", func(t *testing.T) {
106+
t.Parallel()
107+
108+
var (
109+
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{
110+
DeploymentValues: coderdtest.DeploymentValues(t, func(dv *codersdk.DeploymentValues) {
111+
dv.Experiments = []string{string(codersdk.ExperimentWorkspaceSharing)}
112+
}),
113+
})
114+
orgOwner = coderdtest.CreateFirstUser(t, client)
115+
memberClient, member = coderdtest.CreateAnotherUser(t, client, orgOwner.OrganizationID, rbac.ScopedRoleOrgAuditor(orgOwner.OrganizationID))
116+
sharedWorkspace = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
117+
Name: "wibble",
118+
OwnerID: orgOwner.UserID,
119+
OrganizationID: orgOwner.OrganizationID,
120+
}).Do().Workspace
121+
_ = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
122+
Name: "wobble",
123+
OwnerID: orgOwner.UserID,
124+
OrganizationID: orgOwner.OrganizationID,
125+
}).Do().Workspace
126+
)
127+
128+
ctx := testutil.Context(t, testutil.WaitMedium)
129+
130+
client.UpdateWorkspaceACL(ctx, sharedWorkspace.ID, codersdk.UpdateWorkspaceACL{
131+
UserRoles: map[string]codersdk.WorkspaceRole{
132+
member.ID.String(): codersdk.WorkspaceRoleUse,
133+
},
134+
})
135+
136+
inv, root := clitest.New(t, "list", "--shared-with-me", "--output=json")
137+
clitest.SetupConfig(t, memberClient, root)
138+
139+
stdout := new(bytes.Buffer)
140+
inv.Stdout = stdout
141+
err := inv.WithContext(ctx).Run()
142+
require.NoError(t, err)
143+
144+
var workspaces []codersdk.Workspace
145+
require.NoError(t, json.Unmarshal(stdout.Bytes(), &workspaces))
146+
require.Len(t, workspaces, 1)
147+
require.Equal(t, sharedWorkspace.ID, workspaces[0].ID)
148+
})
103149
}

0 commit comments

Comments
 (0)