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/lib | |
| 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/lib')
| -rw-r--r-- | apps/cli/src/lib/globals.ts | 1 | ||||
| -rw-r--r-- | apps/cli/src/lib/output.ts | 61 |
2 files changed, 62 insertions, 0 deletions
diff --git a/apps/cli/src/lib/globals.ts b/apps/cli/src/lib/globals.ts index 771136da..8a301cfe 100644 --- a/apps/cli/src/lib/globals.ts +++ b/apps/cli/src/lib/globals.ts @@ -1,6 +1,7 @@ export interface GlobalOptions { apiKey: string; serverAddr: string; + json?: true; } export let globalOpts: GlobalOptions | undefined = undefined; diff --git a/apps/cli/src/lib/output.ts b/apps/cli/src/lib/output.ts new file mode 100644 index 00000000..34d86461 --- /dev/null +++ b/apps/cli/src/lib/output.ts @@ -0,0 +1,61 @@ +import { InspectOptions } from "util"; +import chalk from "chalk"; + +import { getGlobalOptions } from "./globals"; + +/** + * Prints an object either in a nicely formatted way or as JSON (depending on the command flag --json) + * + * @param output + */ +export function printObject( + output: object, + extraOptions?: InspectOptions, +): void { + if (getGlobalOptions().json) { + console.log(JSON.stringify(output, undefined, 4)); + } else { + console.dir(output, extraOptions); + } +} + +/** + * Used to output a status (success/error) and a message either as string or as JSON (depending on the command flag --json) + * + * @param success if the message is a successful message or an error + * @param output the message to output + */ +export function printStatusMessage(success: boolean, message: unknown): void { + const status = success ? "Success" : "Error"; + const colorFunction = success ? chalk.green : chalk.red; + console.error(colorFunction(`${status}: ${message}`)); +} + +/** + * @param message The message that will be printed as a successful message + * @returns a function that can be used in a Promise on success + */ +export function printSuccess(message: string) { + return () => { + printStatusMessage(true, message); + }; +} + +/** + * @param message The message that will be printed as an error message + * @returns a function that can be used in a Promise on rejection + */ +export function printError(message: string) { + return (error: object) => { + printErrorMessageWithReason(message, error); + }; +} + +/** + * @param message The message that will be printed as an error message + * @param error an error object with the reason for the error + */ +export function printErrorMessageWithReason(message: string, error: object) { + const errorMessage = "message" in error ? error.message : error; + printStatusMessage(false, `${message}. Reason: ${errorMessage}`); +} |
