simple-fs-server/keyout.ts

31 lines
871 B
TypeScript
Raw Normal View History

2023-01-05 18:18:07 +09:00
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")
2023-01-06 18:24:27 +09:00
.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 }) => {
2023-01-05 18:18:07 +09:00
const key = await prepareSecretKey();
2023-01-06 17:48:51 +09:00
const keyout = await crypto.subtle.exportKey("jwk", key);
let out = JSON.stringify(keyout);
2023-01-06 18:24:27 +09:00
if (dotenv) {
out = [`SECRET_KEY='${out}'`].join("\n");
2023-01-06 17:48:51 +09:00
}
2023-01-06 18:24:27 +09:00
if (file) {
await Deno.writeTextFile(file, out);
2023-01-06 17:48:51 +09:00
} else {
2023-01-06 18:24:27 +09:00
console.log(out);
2023-01-06 17:48:51 +09:00
}
2023-01-06 18:24:27 +09:00
});
2023-01-05 18:18:07 +09:00
2023-01-06 18:24:27 +09:00
if (import.meta.main) {
await key_out_cmd.parse(Deno.args);
}