From 94b624f312261604393c61ecbb6cb33f8682ace7 Mon Sep 17 00:00:00 2001 From: monoid Date: Fri, 6 Jan 2023 17:48:51 +0900 Subject: [PATCH] add keyout option --- keyout.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/keyout.ts b/keyout.ts index d41403c..93b6281 100644 --- a/keyout.ts +++ b/keyout.ts @@ -5,10 +5,21 @@ import { prepareSecretKey } from "./util/secret.ts"; export const key_out_cmd = new Command(); key_out_cmd.name("keyout") .description("Output the secret key.") -.action(async () => { +.option("-f, --file ", "The file to output the key to. (default: stdout)") +.option("--dotenv", "Output the key in dotenv format.") +.action(async ({file, dotenv}) => { const key = await prepareSecretKey(); - const out = await crypto.subtle.exportKey("jwk", key); - console.log(JSON.stringify(out)); + const keyout = await crypto.subtle.exportKey("jwk", key); + let out = JSON.stringify(keyout); + if (dotenv){ + out = [`SECRET_KEY='${out}'`].join("\n"); + } + + if (file){ + await Deno.writeTextFile(file, out); + } else { + console.log(out); + } }); if ( import.meta.main ){