Skip to content

Commit a4e8c14

Browse files
committed
chore: use namedWorkspace function
1 parent 3eb707c commit a4e8c14

File tree

1 file changed

+2
-26
lines changed

1 file changed

+2
-26
lines changed

cli/sharing.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
2828
var (
2929
client = new(codersdk.Client)
3030
userAndGroupRegex = regexp.MustCompile(`([A-Za-z0-9]+)(?::([A-Za-z0-9]+))?`)
31-
workspaceRegex = regexp.MustCompile(`([A-Za-z0-9\-]+)(?:\/([A-Za-z0-9\-]+))?`)
3231
users []string
3332
groups []string
3433
)
@@ -55,32 +54,9 @@ func (r *RootCmd) shareWorkspace(orgContext *OrganizationContext) *serpent.Comma
5554
serpent.RequireRangeArgs(1, -1),
5655
),
5756
Handler: func(inv *serpent.Invocation) error {
58-
workspaceAndOwner := workspaceRegex.FindStringSubmatch(inv.Args[0])
59-
60-
// Assume the workspace is being given in the format <owner>/<workspace>
61-
// If the owner is an empty string assume just a workspace name was given
62-
ownerUsername := workspaceAndOwner[1]
63-
workspaceName := workspaceAndOwner[2]
64-
if workspaceName == "" {
65-
workspaceName = workspaceAndOwner[1]
66-
ownerUsername = ""
67-
}
68-
69-
ownerId := codersdk.Me
70-
if ownerUsername != "" {
71-
owner, err := client.User(inv.Context(), ownerUsername)
72-
if err != nil {
73-
return xerrors.Errorf("could not fetch the workspace owner with the username %s.", ownerUsername)
74-
}
75-
76-
ownerId = owner.ID.String()
77-
}
78-
79-
workspace, err := client.WorkspaceByOwnerAndName(inv.Context(), ownerId, workspaceName, codersdk.WorkspaceOptions{
80-
IncludeDeleted: false,
81-
})
57+
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
8258
if err != nil {
83-
return xerrors.Errorf("could not fetch the workspace %s.", workspaceName)
59+
return xerrors.Errorf("could not fetch the workspace %s.", inv.Args[0])
8460
}
8561

8662
userRoles := make(map[string]codersdk.WorkspaceRole, len(users))

0 commit comments

Comments
 (0)