import { Command } from "https://deno.land/x/cliffy@v0.25.6/mod.ts";
import { fromFileUrl, join } from "path/mod.ts";
import { prepareSecretKey } from "./util/secret.ts";

export const key_out_cmd = new Command();
key_out_cmd.name("keyout")
.description("Output the secret key.")
.option("-f, --file <file:string>", "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 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 ){
    await key_out_cmd.parse(Deno.args);
}