Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

[cli] use Deno KV to persist flag values for better DX #42

Open
miguelrk opened this issue Sep 6, 2023 · 0 comments
Open

[cli] use Deno KV to persist flag values for better DX #42

miguelrk opened this issue Sep 6, 2023 · 0 comments
Assignees
Labels
priority: low Do never (delete?) type: enhancement Improvement to existing feature

Comments

@miguelrk
Copy link
Contributor

miguelrk commented Sep 6, 2023

TLDR: revent flag values like --api-key and --project could be persisted using Deno KV in unique database constructed from the file URL of the project root (e.g. using Deno.openKv(import.meta.url)?)

Context

Refer to the managing state section of the Build a Cross-Platform CLI with Deno in 5 minutes blog post.

image

Example

This example is also taken from the blogpost and simply used for illustrative purposes of how the code could look like.

- function main(inputArgs: string[]): void {
+ async function main(inputArgs: string[]): Promise<void> {

  const args = parseArguments(inputArgs);

  // If help flag enabled, print help.
  if (args.help) {
    printHelp();
    Deno.exit(0);
  }

  let name: string | null = args.name;
  let color: string | null = args.color;
  let save: boolean = args.save;

+  const kv = await Deno.openKv("/tmp/kv.db");
+  let askToSave = false;

+  if (!name) {
+    name = (await kv.get(["name"])).value as string;
+  }
+  if (!color) {
+    color = (await kv.get(["color"])).value as string;
+  }
+  if (save) {
+    await kv.set(["name"], name);
+    await kv.set(["color"], color);
+  }

  console.log(
    `%c${
      greetings[Math.floor(Math.random() * greetings.length) - 1]
    }, ${name}!`,
    `color: ${color}; font-weight: bold`,
  );
}
@miguelrk miguelrk added priority: low Do never (delete?) type: enhancement Improvement to existing feature labels Sep 6, 2023
@miguelrk miguelrk self-assigned this Sep 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
priority: low Do never (delete?) type: enhancement Improvement to existing feature
Projects
None yet
Development

No branches or pull requests

1 participant