Skip to content

Commit

Permalink
CLI
Browse files Browse the repository at this point in the history
pklaschka committed Sep 6, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 25c046c commit b9e2126
Showing 7 changed files with 92 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ $$ n \in [r_{min}, r]; n = r_{min} + t \mod (r - r_{min}) $$
- [x] Connection to a database
- [x] REST API
- [x] Visual Web Interface
- [ ] CLI
- [x] CLI
- [ ] Bump counter to avoid collissions after restoring backups (where ASNs
could have been generated after the time of the backup)
- [ ] Analyze time between ASNs, providing the possibility to specify a
@@ -61,4 +61,4 @@ $$ n \in [r_{min}, r]; n = r_{min} + t \mod (r - r_{min}) $$
- [ ] Publish on JSR
- [ ] Publish on Docker Hub
- [x] Publish on GitHub Container Registry
- [ ] UI for quickly searching for ASNs in a DMS
- [x] UI for quickly searching for ASNs in a DMS
9 changes: 5 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -8,13 +8,14 @@
},
"imports": {
"$/": "./",
"$cli/": "./lib/cli/",
"$common/": "./lib/common/",
"$http/": "./lib/http/",
"$cli/": "./lib/cli/",
"@hono/hono": "jsr:@hono/hono@^4.5.11",
"@std/datetime": "jsr:@std/datetime@^0.225.2",
"@collinhacks/zod": "npm:zod@^3.23.8",
"@metafloor/bwip-js": "npm:bwip-js@^4.5.1"
"@hono/hono": "jsr:@hono/hono@^4.5.11",
"@metafloor/bwip-js": "npm:bwip-js@^4.5.1",
"@std/cli": "jsr:@std/cli@^1.0.5",
"@std/datetime": "jsr:@std/datetime@^0.225.2"
},
"compilerOptions": {
"jsx": "precompile",
5 changes: 5 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/cli/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { generateASN } from "$common/asn.ts";
import z from "@collinhacks/zod";

const generateArgs = z.object({
count: z.number().default(1),
});

export async function runGenerate(args: unknown) {
const parsedArgs = generateArgs.parse(args);

new Array(parsedArgs.count).fill(0).map(() =>
generateASN({
client: "cli",
generatedCount: parsedArgs.count,
}).then(asn => asn.asn)
).forEach(async (x) => console.log(await x));
}
16 changes: 16 additions & 0 deletions lib/cli/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function printHelp() {
const cmd = Deno.args[0];
console.log(`
Usage: ${cmd} [command] [options]
Commands:
server Start the server (default)
--port <port> Port to listen on (defaults to the PORT environment variable)
--host <host> Hostname to listen on (defaults to localhost)
generate Generate new ASNs
--count <n> Number of ASNs to generate (defaults to 1)
Options:
--help Show this help message
`.trim());
}
29 changes: 29 additions & 0 deletions lib/cli/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { z } from "@collinhacks/zod";
import { CONFIG, logPaths } from "$common/mod.ts";
import { httpApp } from "$http/mod.tsx";
import metadata from "$/deno.json" with { type: "json" };

const serverArgs = z.object({
port: z.number().default(CONFIG.PORT),
host: z.string().default("localhost"),
});

export function runServer(args: unknown) {
console.log(`Running ${metadata.name} v${metadata.version}`);
console.log();

const parsedArgs = serverArgs.parse(args);

console.log(`Starting server on ${parsedArgs.host}:${parsedArgs.port}`);

console.log("Environment Configuration:", CONFIG);
console.log("Arguments:", parsedArgs);
console.log("Paths:");
logPaths();

console.log();
Deno.serve(
{ port: parsedArgs.port, hostname: parsedArgs.host },
httpApp.fetch,
);
}
32 changes: 18 additions & 14 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { getFormatDescription } from "./lib/common/asn.ts";
import { CONFIG, logPaths, validateDB } from "$common/mod.ts";
import { httpApp } from "./lib/http/mod.tsx";
import metadata from "./deno.json" with { type: "json" };
import { parseArgs } from "@std/cli/parse-args";
import { validateDB } from "$common/mod.ts";

if (import.meta.main) {
console.log(`Running ${metadata.name} v${metadata.version}`);
console.log();

logPaths();

console.log();
import { runServer } from "$cli/server.ts";
import { printHelp } from "$cli/help.ts";
import { runGenerate } from "$cli/generate.ts";

if (import.meta.main) {
await validateDB();
console.log(getFormatDescription());
const args = parseArgs(Deno.args);

if (args.help) {
printHelp();
Deno.exit();
}

console.log();
if (args._[0] === "generate") {
await runGenerate(args);
}

Deno.serve({ port: CONFIG.PORT }, httpApp.fetch);
if (args._[0] === "server" || args._.length === 0) {
await runServer(args);
}
}

0 comments on commit b9e2126

Please sign in to comment.