From 5e4decbe967ec28f9263bcb1d9907ee86262b91e Mon Sep 17 00:00:00 2001 From: kamtschatka Date: Sat, 27 Jul 2024 22:32:37 +0200 Subject: feature(cli): Allow updating tags/lists from CLI (#211) * Improve the CLI #209 added the possibility to assign tags to bookmarks while creating added the possibility to assign a newly created to a list right away added the possibility to add and remove tags from bookmarks * minor tweaks --------- Co-authored-by: MohamedBassem --- apps/cli/src/commands/bookmarks.ts | 108 +++++++++++++++++++++++++++++++++---- apps/cli/src/commands/lists.ts | 38 +++++++------ 2 files changed, 118 insertions(+), 28 deletions(-) (limited to 'apps/cli/src/commands') diff --git a/apps/cli/src/commands/bookmarks.ts b/apps/cli/src/commands/bookmarks.ts index 6efb41d9..51dd037b 100644 --- a/apps/cli/src/commands/bookmarks.ts +++ b/apps/cli/src/commands/bookmarks.ts @@ -1,4 +1,5 @@ import * as fs from "node:fs"; +import { addToList } from "@/commands/lists"; import { printError, printObject, @@ -23,7 +24,11 @@ function collect(val: T, acc: T[]) { return acc; } -function normalizeBookmark(bookmark: ZBookmark) { +type Bookmark = Omit & { + tags: string[]; +}; + +function normalizeBookmark(bookmark: ZBookmark): Bookmark { const ret = { ...bookmark, tags: bookmark.tags.map((t) => t.name), @@ -58,10 +63,20 @@ bookmarkCmd [], ) .option("--stdin", "reads the data from stdin and store it as a note") + .option( + "--list-id ", + "if set, the bookmark(s) will be added to this list", + ) + .option( + "--tag-name ", + "if set, this tag will be added to the bookmark(s). Specify multiple times to add multiple tags", + collect, + [], + ) .action(async (opts) => { const api = getAPIClient(); - const results: object[] = []; + const results: Bookmark[] = []; const promises = [ ...opts.link.map((url) => @@ -104,6 +119,13 @@ bookmarkCmd await Promise.allSettled(promises); printObject(results); + + await Promise.allSettled( + results.flatMap((r) => [ + updateTags(opts.tagName, [], r.id), + opts.listId ? addToList(opts.listId, r.id) : Promise.resolve(), + ]), + ); }); bookmarkCmd @@ -118,6 +140,48 @@ bookmarkCmd .catch(printError(`Failed to get the bookmark with id "${id}"`)); }); +function printTagMessage( + tags: { tagName: string }[], + bookmarkId: string, + action: "Added" | "Removed", +) { + tags.forEach((tag) => { + printStatusMessage( + true, + `${action} the tag ${tag.tagName} ${action === "Added" ? "to" : "from"} the bookmark with id ${bookmarkId}`, + ); + }); +} + +async function updateTags(addTags: string[], removeTags: string[], id: string) { + const tagsToAdd = addTags.map((addTag) => { + return { tagName: addTag }; + }); + + const tagsToRemove = removeTags.map((removeTag) => { + return { tagName: removeTag }; + }); + + if (tagsToAdd.length > 0 || tagsToRemove.length > 0) { + const api = getAPIClient(); + await api.bookmarks.updateTags + .mutate({ + bookmarkId: id, + attach: tagsToAdd, + detach: tagsToRemove, + }) + .then(() => { + printTagMessage(tagsToAdd, id, "Added"); + printTagMessage(tagsToRemove, id, "Removed"); + }) + .catch( + printError( + `Failed to add/remove tags to/from bookmark with id "${id}"`, + ), + ); + } +} + bookmarkCmd .command("update") .description("update a bookmark") @@ -127,18 +191,40 @@ bookmarkCmd .option("--no-archive", "if set, the bookmark will be unarchived") .option("--favourite", "if set, the bookmark will be favourited") .option("--no-favourite", "if set, the bookmark will be unfavourited") + .option( + "--add-tag ", + "if set, this tag will be added to the bookmark. Specify multiple times to add multiple tags", + collect, + [], + ) + .option( + "--remove-tag ", + "if set, this tag will be removed from the bookmark. Specify multiple times to remove multiple tags", + collect, + [], + ) .argument("", "the id of the bookmark to get") .action(async (id, opts) => { const api = getAPIClient(); - await api.bookmarks.updateBookmark - .mutate({ - bookmarkId: id, - archived: opts.archive, - favourited: opts.favourite, - title: opts.title, - }) - .then(printObject) - .catch(printError(`Failed to update bookmark with id "${id}"`)); + await updateTags(opts.addTag, opts.removeTag, id); + + if ( + "archive" in opts || + "favourite" in opts || + "title" in opts || + "note" in opts + ) { + await api.bookmarks.updateBookmark + .mutate({ + bookmarkId: id, + archived: opts.archive, + favourited: opts.favourite, + title: opts.title, + note: opts.note, + }) + .then(printObject) + .catch(printError(`Failed to update bookmark with id "${id}"`)); + } }); bookmarkCmd diff --git a/apps/cli/src/commands/lists.ts b/apps/cli/src/commands/lists.ts index 2f85ae7b..855624d6 100644 --- a/apps/cli/src/commands/lists.ts +++ b/apps/cli/src/commands/lists.ts @@ -62,29 +62,33 @@ listsCmd .catch(printError(`Failed to delete list with id "${id}"`)); }); +export async function addToList(listId: string, bookmarkId: string) { + const api = getAPIClient(); + + await api.lists.addToList + .mutate({ + listId, + bookmarkId, + }) + .then( + printSuccess( + `Successfully added bookmark "${bookmarkId}" to list with id "${listId}"`, + ), + ) + .catch( + printError( + `Failed to add bookmark "${bookmarkId}" to list with id "${listId}"`, + ), + ); +} + listsCmd .command("add-bookmark") .description("add a bookmark to list") .requiredOption("--list ", "the id of the list") .requiredOption("--bookmark ", "the id of the bookmark") .action(async (opts) => { - const api = getAPIClient(); - - 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}"`, - ), - ); + await addToList(opts.list, opts.bookmark); }); listsCmd -- cgit v1.2.3-70-g09d2