Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: low Do never (delete?) type: enhancement Improvement to existing feature
Projects
None yet
Development

No branches or pull requests

1 participant