diff --git a/cli/main.ts b/cli/main.ts index c13c31fc52..fa98bd454d 100644 --- a/cli/main.ts +++ b/cli/main.ts @@ -66,9 +66,8 @@ try { } catch (e) { if (e.name === "ApiError") { console.log("Server failed. " + e.statusText + ": " + e.body); - } else { - throw e; } + throw e; } export default command; diff --git a/cli/workspace.ts b/cli/workspace.ts index 0e2e7e9bbf..f4f26aaa42 100644 --- a/cli/workspace.ts +++ b/cli/workspace.ts @@ -2,7 +2,16 @@ import { GlobalOptions } from "./types.ts"; import { getRootStore } from "./store.ts"; import { loginInteractive, tryGetLoginInfo } from "./login.ts"; -import { colors, Command, DelimiterStream, Input, Table } from "./deps.ts"; +import { + colors, + Command, + DelimiterStream, + Input, + setClient, + Table, + WorkspaceService, +} from "./deps.ts"; +import { requireLogin } from "./context.ts"; export type Workspace = { remote: string; @@ -132,7 +141,11 @@ async function switchC(opts: GlobalOptions, workspaceName: string) { } export async function add( - opts: GlobalOptions, + opts: GlobalOptions & { + create: boolean; + createWorkspaceName: string | undefined; + createUsername: string; + }, workspaceName: string | undefined, workspaceId: string | undefined, remote: string | undefined, @@ -178,6 +191,25 @@ export async function add( token = await loginInteractive(remote); } + if (opts.create) { + setClient(token, remote.substring(0, remote.length - 1)); + + if ( + !await WorkspaceService.existsWorkspace({ + requestBody: { id: workspaceId }, + }) + ) { + console.log(colors.yellow("Workspace does not exist. Creating...")); + await WorkspaceService.createWorkspace({ + requestBody: { + id: workspaceId, + name: opts.createWorkspaceName ?? workspaceId, + username: opts.createUsername, + }, + }); + } + } + await addWorkspace({ name: workspaceName, remote: remote, @@ -224,6 +256,18 @@ const command = new Command() .command("add") .description("Add a workspace") .arguments("[workspace_name:string] [workspace_id:string] [remote:string]") + .option("-c --create", "Create the workspace if it does not exist") + .option( + "--create-workspace-name ", + "Specify the workspace name. Ignored if --create is not specified or the workspace already exists. Will default to the workspace id.", + ) + .option( + "--create-username ", + "Specify your own username in the newly created workspace. Ignored if --create is not specified or the workspace already exists.", + { + default: "admin", + }, + ) .action(add as any) .command("remove") .description("Remove a workspace")