From 7bd7cbb712bcb7d20cc02f0684381deea3862279 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 16 Mar 2023 03:10:45 +0100 Subject: [PATCH] update cli error message for folders --- cli/folder.ts | 71 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/cli/folder.ts b/cli/folder.ts index 548f950906..cfd49957d9 100644 --- a/cli/folder.ts +++ b/cli/folder.ts @@ -20,11 +20,17 @@ import { export class FolderFile implements Resource, PushDiffs { @property(array(() => String)) owners: Array | undefined; - @property(map(() => String, () => Boolean, { shape: MapShape.Object })) + @property( + map( + () => String, + () => Boolean, + { shape: MapShape.Object } + ) + ) extra_perms: Map | undefined; @property(() => String) - display_name: string| undefined; - + display_name: string | undefined; + async push(workspace: string, remotePath: string): Promise { if (remotePath.startsWith("/")) { remotePath = remotePath.substring(1); @@ -36,14 +42,14 @@ export class FolderFile implements Resource, PushDiffs { await this.pushDiffs( workspace, remotePath, - microdiff({}, this, { cyclesFix: false }), + microdiff({}, this, { cyclesFix: false }) ); } async pushDiffs( workspace: string, remotePath: string, - diffs: Difference[], + diffs: Difference[] ): Promise { if (remotePath.startsWith("/")) { remotePath = remotePath.substring(1); @@ -55,15 +61,18 @@ export class FolderFile implements Resource, PushDiffs { // TODO: Support this in backend let exists: boolean; try { - exists = !!await FolderService.getFolder({ workspace, name: remotePath }); + exists = !!(await FolderService.getFolder({ + workspace, + name: remotePath, + })); } catch { exists = false; } if (exists) { console.log( colors.bold.yellow( - `Applying ${diffs.length} diffs to existing folder... ${remotePath}`, - ), + `Applying ${diffs.length} diffs to existing folder... ${remotePath}` + ) ); const changeset: { @@ -74,12 +83,12 @@ export class FolderFile implements Resource, PushDiffs { for (const diff of diffs) { if ( diff.type !== "REMOVE" && - ( - diff.path.length !== 1 || - !["owners", "extra_perms", "display_name"].includes(diff.path[0] as string) - ) + (diff.path.length !== 1 || + !["owners", "extra_perms", "display_name"].includes( + diff.path[0] as string + )) ) { - console.log(diff.path) + console.log(diff.path); throw new Error("Invalid folder diff with path " + diff.path); } if (diff.type === "CREATE" || diff.type === "CHANGE") { @@ -89,17 +98,27 @@ export class FolderFile implements Resource, PushDiffs { } } - const hasChanges = Object.values(changeset).some((v) => - v !== null && typeof v !== "undefined" + const hasChanges = Object.values(changeset).some( + (v) => v !== null && typeof v !== "undefined" ); if (!hasChanges) { return; } - await FolderService.updateFolder({ - workspace: workspace, - name: remotePath, - requestBody: {...changeset, extra_perms: changeset.extra_perms ? Object.fromEntries(this.extra_perms?.entries() ?? []) : undefined} - }); + try { + await FolderService.updateFolder({ + workspace: workspace, + name: remotePath, + requestBody: { + ...changeset, + extra_perms: changeset.extra_perms + ? Object.fromEntries(this.extra_perms?.entries() ?? []) + : undefined, + }, + }); + } catch (e) { + console.error(colors.red.bold(e.body)); + throw e; + } } else { console.log(colors.bold.yellow("Creating new folder: " + remotePath)); await FolderService.createFolder({ @@ -118,7 +137,7 @@ async function push(opts: GlobalOptions, filePath: string, remotePath: string) { const workspace = await resolveWorkspace(opts); await requireLogin(opts); - if (!await validatePath(opts, remotePath)) { + if (!(await validatePath(opts, remotePath))) { return; } @@ -136,11 +155,11 @@ async function push(opts: GlobalOptions, filePath: string, remotePath: string) { export async function pushFolder( workspace: string, filePath: string, - remotePath: string, + remotePath: string ) { - const data = decoverto.type(FolderFile).rawToInstance( - await Deno.readTextFile(filePath), - ); + const data = decoverto + .type(FolderFile) + .rawToInstance(await Deno.readTextFile(filePath)); data.push(workspace, remotePath); } @@ -148,7 +167,7 @@ const command = new Command() .description("resource related commands") .command( "push", - "push a local folder spec. This overrides any remote versions.", + "push a local folder spec. This overrides any remote versions." ) .arguments(" ") .action(push as any);