diff --git a/cli/mod.ts b/cli/mod.ts index 3558108..5b84bae 100755 --- a/cli/mod.ts +++ b/cli/mod.ts @@ -1,4 +1,6 @@ #!/usr/bin/env deno +import * as secrets from "./secrets.ts"; + import * as path from "@std/path"; import * as ini from "@std/ini"; @@ -190,19 +192,7 @@ if (import.meta.main) { console.error("Multiple advent of code session cookies were found??"); Deno.exit(1); } else { - const process = new Deno.Command("secret-tool", { - args: [ - "store", - "--label", - "Advent Of Code Session Token", - "token", - "value" - ], - stdin: "piped" - }).spawn(); - const stdin = process.stdin.getWriter(); - await stdin.write(new TextEncoder().encode(rows[0].value)); - await stdin.close(); + await secrets.saveToken(rows[0].value); console.log("Token saved!"); } @@ -214,12 +204,36 @@ if (import.meta.main) { ) .reset() .command( - "input [day:number]", - "Fetches the input for one day (or all if day # omitted) and writes to input.txt files" + "input ", + "Fetches the input for one day and writes to a dayN/input.txt file" ) - .action((_: unknown, day: number | undefined) => { - if (day != undefined) { + .option( + "-o, --output [path:string]", + "The location to output the text to", + { + + } + ) + .action(async ({ output }, day: number | undefined) => { + if (Date.now() < new Date(`Dec ${day} 2024 00:00:00 GMT-0500`).getTime()) { + console.error("Can't fetch an input for an unreleased day!"); + Deno.exit(1); + } + const token = await secrets.readToken(); + if (day != undefined) { + const inputResponse = await fetch(`https://adventofcode.com/2024/day/${day}/input`, { + headers: { + cookie: `session=${token}`, + 'user-agent': "Ty's Advent of Code CLI (https://git.myriation.xyz/Ty/advent-of-code-2024)" + } + }); + + if (inputResponse.status === 200) { + console.log(await inputResponse.text()) + } else { + console.log(`Error fetching input for day ${day}:\n\n${await inputResponse.text()}`) + } } }) .parse(Deno.args); diff --git a/cli/secrets.ts b/cli/secrets.ts index cd1a0a4..f4f4d0f 100644 --- a/cli/secrets.ts +++ b/cli/secrets.ts @@ -1,3 +1,9 @@ +/** + * 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"; + export async function saveToken(token: string) { const process = new Deno.Command("secret-tool", { args: [ @@ -15,17 +21,13 @@ export async function saveToken(token: string) { } export async function readToken() { - const process = new Deno.Command("secret-tool", { + const { stdout } = await new Deno.Command("secret-tool", { args: [ "lookup", "aoc-deno", "token" ], stdout: "piped" - }).spawn(); - const decoderStream = new TextDecoderStream(); - const stdout = process.stdout.pipeTo(decoderStream.writable); - for await (const v of decoderStream.readable.values()) { - console.log(v); - } + }).spawn().output(); + return new TextDecoder().decode(stdout); } \ No newline at end of file diff --git a/deno.json b/deno.json index 6250651..a8a3683 100644 --- a/deno.json +++ b/deno.json @@ -5,6 +5,7 @@ "@cross/dir": "jsr:@cross/dir@^1.1.0", "@db/sqlite": "jsr:@db/sqlite@^0.12.0", "@std/ini": "jsr:@std/ini@^0.225.2", + "@std/io": "jsr:@std/io@^0.225.0", "@std/path": "jsr:@std/path@^1.0.8" } } \ No newline at end of file diff --git a/deno.lock b/deno.lock index d506f6f..aa26051 100644 --- a/deno.lock +++ b/deno.lock @@ -18,17 +18,21 @@ "jsr:@std/assert@0.217": "0.217.0", "jsr:@std/assert@0.221": "0.221.0", "jsr:@std/assert@~1.0.6": "1.0.9", + "jsr:@std/bytes@^1.0.2": "1.0.4", "jsr:@std/encoding@0.221": "0.221.0", "jsr:@std/encoding@~1.0.5": "1.0.5", "jsr:@std/fmt@0.221": "0.221.0", "jsr:@std/fmt@~1.0.2": "1.0.3", "jsr:@std/fs@0.221": "0.221.0", "jsr:@std/ini@~0.225.2": "0.225.2", + "jsr:@std/io@*": "0.225.0", + "jsr:@std/io@0.225": "0.225.0", "jsr:@std/path@0.217": "0.217.0", "jsr:@std/path@0.221": "0.221.0", "jsr:@std/path@^1.0.8": "1.0.8", "jsr:@std/path@~1.0.6": "1.0.8", - "jsr:@std/text@~1.0.7": "1.0.8" + "jsr:@std/text@~1.0.7": "1.0.8", + "npm:@types/node@*": "22.5.4" }, "jsr": { "@cliffy/ansi@1.0.0-rc.7": { @@ -130,6 +134,9 @@ "@std/assert@1.0.9": { "integrity": "a9f0c611a869cc791b26f523eec54c7e187aab7932c2c8e8bea0622d13680dcd" }, + "@std/bytes@1.0.4": { + "integrity": "11a0debe522707c95c7b7ef89b478c13fb1583a7cfb9a85674cd2cc2e3a28abc" + }, "@std/encoding@0.221.0": { "integrity": "d1dd76ef0dc5d14088411e6dc1dede53bf8308c95d1537df1214c97137208e45" }, @@ -152,6 +159,12 @@ "@std/ini@0.225.2": { "integrity": "c70f6560dacb7e333c2f868aa09aaa4117e6618cc66c60d0b4ca716c135c8e67" }, + "@std/io@0.225.0": { + "integrity": "c1db7c5e5a231629b32d64b9a53139445b2ca640d828c26bf23e1c55f8c079b3", + "dependencies": [ + "jsr:@std/bytes" + ] + }, "@std/path@0.217.0": { "integrity": "1217cc25534bca9a2f672d7fe7c6f356e4027df400c0e85c0ef3e4343bc67d11", "dependencies": [ @@ -171,6 +184,17 @@ "integrity": "40ba34caa095f393e78796e5eda37b8b4e2cc6cfd6f51f34658ad7487b1451e4" } }, + "npm": { + "@types/node@22.5.4": { + "integrity": "sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg==", + "dependencies": [ + "undici-types" + ] + }, + "undici-types@6.19.8": { + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==" + } + }, "remote": { "https://deno.land/x/sqlite@v3.9.1/build/sqlite.js": "2afc7875c7b9c85d89730c4a311ab3a304e5d1bf761fbadd8c07bbdf130f5f9b", "https://deno.land/x/sqlite@v3.9.1/build/vfs.js": "7f7778a9fe499cd10738d6e43867340b50b67d3e39142b0065acd51a84cd2e03", @@ -189,6 +213,7 @@ "jsr:@cross/dir@^1.1.0", "jsr:@db/sqlite@0.12", "jsr:@std/ini@~0.225.2", + "jsr:@std/io@0.225", "jsr:@std/path@^1.0.8" ] } diff --git a/scripts/aoc b/scripts/aoc index d08fd7c..9c0e393 100755 --- a/scripts/aoc +++ b/scripts/aoc @@ -1,4 +1,4 @@ #!/usr/bin/env sh cd "$(dirname "${BASH_SOURCE[0]}")/.." -deno run -A ./main.ts $@ +deno run -A ./cli/mod.ts $@