advent-of-code-2024/cli/secrets.ts

33 lines
887 B
TypeScript
Raw Permalink Normal View History

2024-12-10 19:12:08 -07:00
/**
* There is no proper OS-secrets library for deno (or even nodejs!) so this is a
* jank way to get around that on linux at least
*/
import * as io from "@std/io";
2024-12-03 21:42:57 -07:00
export async function saveToken(token: string) {
const process = new Deno.Command("secret-tool", {
args: [
"store",
"--label",
"Advent Of Code Session Token",
"aoc-deno",
"token"
],
stdin: "piped"
}).spawn();
const stdin = process.stdin.getWriter();
await stdin.write(new TextEncoder().encode(token));
await stdin.close();
}
export async function readToken() {
2024-12-10 19:12:08 -07:00
const { stdout } = await new Deno.Command("secret-tool", {
2024-12-03 21:42:57 -07:00
args: [
"lookup",
"aoc-deno",
"token"
],
stdout: "piped"
2024-12-10 19:12:08 -07:00
}).spawn().output();
return new TextDecoder().decode(stdout);
2024-12-03 21:42:57 -07:00
}