diff options
| author | kamtschatka <sschatka@gmail.com> | 2024-06-09 23:30:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-09 22:30:24 +0100 |
| commit | cde97267a90802c6a367aa61ff157983506deead (patch) | |
| tree | 7fc6dbca60ab537ce3e3be657f4d510dcdeb5fdb /apps/cli/src/commands/lists.ts | |
| parent | 6928800a604f05ef62234cb5c3ee1e60fb27ea1a (diff) | |
| download | karakeep-cde97267a90802c6a367aa61ff157983506deead.tar.zst | |
fix(cli): Bookmark list output is not a valid JSON. Fixes #150 (#181)
* bookmark list output is not a valid JSON #150
Reworked the cli to switch over to json output
* changed the logging to log created bookmarks as an array
switch all log output that is just a status to stderr
---------
Co-authored-by: kamtschatka <simon.schatka@gmx.at>
Diffstat (limited to 'apps/cli/src/commands/lists.ts')
| -rw-r--r-- | apps/cli/src/commands/lists.ts | 91 |
1 files changed, 65 insertions, 26 deletions
diff --git a/apps/cli/src/commands/lists.ts b/apps/cli/src/commands/lists.ts index c7b2a5f0..2f85ae7b 100644 --- a/apps/cli/src/commands/lists.ts +++ b/apps/cli/src/commands/lists.ts @@ -1,3 +1,10 @@ +import { getGlobalOptions } from "@/lib/globals"; +import { + printError, + printErrorMessageWithReason, + printObject, + printSuccess, +} from "@/lib/output"; import { getAPIClient } from "@/lib/trpc"; import { Command } from "@commander-js/extra-typings"; import { getBorderCharacters, table } from "table"; @@ -14,19 +21,30 @@ listsCmd .action(async () => { const api = getAPIClient(); - const resp = await api.lists.list.query(); - const { allPaths } = listsToTree(resp.lists); + try { + const resp = await api.lists.list.query(); - const data: string[][] = [["Id", "Name"]]; + if (getGlobalOptions().json) { + printObject(resp); + } else { + const { allPaths } = listsToTree(resp.lists); + const data: string[][] = [["Id", "Name"]]; - allPaths.forEach((path) => { - const name = path.map((p) => `${p.icon} ${p.name}`).join(" / "); - const id = path[path.length - 1].id; - data.push([id, name]); - }); - console.log( - table(data, { border: getBorderCharacters("ramac"), singleLine: true }), - ); + allPaths.forEach((path) => { + const name = path.map((p) => `${p.icon} ${p.name}`).join(" / "); + const id = path[path.length - 1].id; + data.push([id, name]); + }); + console.log( + table(data, { + border: getBorderCharacters("ramac"), + singleLine: true, + }), + ); + } + } catch (error) { + printErrorMessageWithReason("Failed to list all lists", error as object); + } }); listsCmd @@ -36,10 +54,12 @@ listsCmd .action(async (id) => { const api = getAPIClient(); - await api.lists.delete.mutate({ - listId: id, - }); - console.log("Successfully deleted list with id:", id); + await api.lists.delete + .mutate({ + listId: id, + }) + .then(printSuccess(`Successfully deleted list with id "${id}"`)) + .catch(printError(`Failed to delete list with id "${id}"`)); }); listsCmd @@ -50,11 +70,21 @@ listsCmd .action(async (opts) => { const api = getAPIClient(); - await api.lists.addToList.mutate({ - listId: opts.list, - bookmarkId: opts.bookmark, - }); - console.log("Successfully added bookmark from list"); + await api.lists.addToList + .mutate({ + listId: opts.list, + bookmarkId: opts.bookmark, + }) + .then( + printSuccess( + `Successfully added bookmark "${opts.bookmark}" to list with id "${opts.list}"`, + ), + ) + .catch( + printError( + `Failed to add bookmark "${opts.bookmark}" to list with id "${opts.list}"`, + ), + ); }); listsCmd @@ -65,10 +95,19 @@ listsCmd .action(async (opts) => { const api = getAPIClient(); - await api.lists.removeFromList.mutate({ - listId: opts.list, - bookmarkId: opts.bookmark, - }); - - console.log("Successfully removed bookmark from list"); + await api.lists.removeFromList + .mutate({ + listId: opts.list, + bookmarkId: opts.bookmark, + }) + .then( + printSuccess( + `Successfully removed bookmark "${opts.bookmark}" from list with id "${opts.list}"`, + ), + ) + .catch( + printError( + `Failed to remove bookmark "${opts.bookmark}" from list with id "${opts.list}"`, + ), + ); }); |
