feat: add ability to make workspace for other user from cli (#8481)

* feat: add ability to make workspace for other user from cli
* Add example to show functionality
This commit is contained in:
Steven Masley
2023-07-14 09:48:02 -04:00
committed by GitHub
parent 5fd77ad7cf
commit 4c4d966c7b
5 changed files with 102 additions and 17 deletions
+19 -13
View File
@@ -626,24 +626,30 @@ func CurrentOrganization(inv *clibase.Invocation, client *codersdk.Client) (code
return orgs[0], nil
}
func splitNamedWorkspace(identifier string) (owner string, workspaceName string, err error) {
parts := strings.Split(identifier, "/")
switch len(parts) {
case 1:
owner = codersdk.Me
workspaceName = parts[0]
case 2:
owner = parts[0]
workspaceName = parts[1]
default:
return "", "", xerrors.Errorf("invalid workspace name: %q", identifier)
}
return owner, workspaceName, 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(ctx context.Context, client *codersdk.Client, 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)
owner, name, err := splitNamedWorkspace(identifier)
if err != nil {
return codersdk.Workspace{}, err
}
return client.WorkspaceByOwnerAndName(ctx, owner, name, codersdk.WorkspaceOptions{})
}