From 8511ae21374c92e81d233f30cd14e998d684fc36 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Fri, 27 Sep 2024 21:25:01 +0530 Subject: [PATCH] switch over to new CLI parser --- tribes-cli/cli.ts | 61 +++++++++++++------------------------ tribes-cli/remote/init.ts | 5 +-- tribes-cli/remote/mod.ts | 10 ++---- tribes-cli/tribe/config.ts | 5 +-- tribes-cli/tribe/destroy.ts | 5 +-- tribes-cli/tribe/mod.ts | 19 +++--------- tribes-cli/tribe/new.ts | 11 +++++-- 7 files changed, 46 insertions(+), 70 deletions(-) diff --git a/tribes-cli/cli.ts b/tribes-cli/cli.ts index 9f1d9913..dc1949b1 100644 --- a/tribes-cli/cli.ts +++ b/tribes-cli/cli.ts @@ -1,50 +1,31 @@ -import { parseArgs } from '@std/cli'; import { tribe } from './tribe/mod.ts'; import { remote } from './remote/mod.ts'; -import { ParsedSubcommand, parseSubcommand } from './utils.ts'; +import { Command, defaultIdentityFile, setupCli } from './utils/mod.ts'; -const commands = { - tribe, - remote, -}; +async function main() { + const tribes = new Command('tribes-cli', 'Create and manage Ditto Tribes servers.') + .subcommand('tribe', tribe) + .subcommand('remote', remote); -let parserArgs: Partial = { - string: ['identity-file'], -}; + const { parsed } = setupCli(tribes, { + string: ['identity-file'], + default: { + 'identity-file': await defaultIdentityFile(), + }, + }); -for (const [_name, body] of Object.entries(commands)) { - for (const subcommand in body) { - const s = body[subcommand]; - parserArgs = parseSubcommand(s, parserArgs); + const [s, v] = parsed._; + + let cmd = tribes; + if (s && !v) { + cmd = tribes.getSubcommand(s); + } else if (s && v) { + cmd = tribes.getSubcommand(s).getSubcommand(v); } - // TODO: construct a help string here + await cmd.action(parsed); } -const parsed = parseArgs(Deno.args, parserArgs); -console.log(parserArgs); -console.log(parsed); - -const [subcommand, verb] = parsed._; - -const isValidSubcommand = (key: string): key is keyof typeof commands => { - return Object.keys(commands).includes(key.toString()); -}; - -if (typeof subcommand === 'number') { - console.error('Error: subcommand cannot be a number'); - Deno.exit(1); -} - -if (!subcommand) { - // todo: output help, probably -} else if (subcommand && !verb) { - // todo: output help only for specific subcommand -} else { - if (isValidSubcommand(subcommand)) { - console.log(commands[subcommand]); - } else { - console.error(`Error: ${subcommand} is not a valid subcommand`); - Deno.exit(1); - } +if (import.meta.main) { + await main(); } diff --git a/tribes-cli/remote/init.ts b/tribes-cli/remote/init.ts index ad07e351..2201c023 100644 --- a/tribes-cli/remote/init.ts +++ b/tribes-cli/remote/init.ts @@ -1,2 +1,3 @@ -export const init = () => { -}; +import { Command } from '../utils/mod.ts'; + +export const init = new Command('init', 'Initialise a brand-new Ditto remote'); diff --git a/tribes-cli/remote/mod.ts b/tribes-cli/remote/mod.ts index 188c96f8..715dd62e 100644 --- a/tribes-cli/remote/mod.ts +++ b/tribes-cli/remote/mod.ts @@ -1,9 +1,5 @@ -import { Subcommand } from '../utils.ts'; +import { Command } from '../utils/mod.ts'; import { init } from './init.ts'; -export const remote: Record = { - init: { - action: init, - description: 'Initialise a brand new Ditto remote.', - }, -}; +export const remote = new Command('remote', 'Manage a Ditto Tribes server') + .subcommand('init', init); diff --git a/tribes-cli/tribe/config.ts b/tribes-cli/tribe/config.ts index 9d8aeb6d..94791bbf 100644 --- a/tribes-cli/tribe/config.ts +++ b/tribes-cli/tribe/config.ts @@ -1,5 +1,6 @@ +import { Command } from '../utils/mod.ts'; + /** * Change a tribe's configuration. */ -export const config = () => { -}; +export const config = new Command('config', "Modify a tribe's configuration"); diff --git a/tribes-cli/tribe/destroy.ts b/tribes-cli/tribe/destroy.ts index f1282701..1eda8494 100644 --- a/tribes-cli/tribe/destroy.ts +++ b/tribes-cli/tribe/destroy.ts @@ -2,5 +2,6 @@ * Destroy a tribe. Non-reversible. */ -export const destroy = () => { -}; +import { Command } from '../utils/mod.ts'; + +export const destroy = new Command('destroy', 'Destroy a tribe. NON-REVERSIBLE!'); diff --git a/tribes-cli/tribe/mod.ts b/tribes-cli/tribe/mod.ts index 99bcc733..b5aac37c 100644 --- a/tribes-cli/tribe/mod.ts +++ b/tribes-cli/tribe/mod.ts @@ -1,18 +1,7 @@ import { create } from './new.ts'; import { config } from './config.ts'; -import { Subcommand } from '../utils.ts'; +import { Command } from '../utils/mod.ts'; -export const tribe: Record = { - create: { - action: create, - description: 'Create a new tribe.', - options: { - '-c --custom-domain': { - description: 'Do not use a subdomain of the tribes server; instead, use a custom domain.', - }, - '-s --subdomain': { - description: 'Use a subdomain of the tribes server to host the new Ditto instance.', - }, - }, - }, -}; +export const tribe = new Command('tribe', 'Create and manage tribes.') + .subcommand('create', create) + .subcommand('config', config); diff --git a/tribes-cli/tribe/new.ts b/tribes-cli/tribe/new.ts index e94d1765..2991e84e 100644 --- a/tribes-cli/tribe/new.ts +++ b/tribes-cli/tribe/new.ts @@ -1,2 +1,9 @@ -export const create = () => { -}; +import { Command } from '../utils/mod.ts'; + +export const create = new Command('create', 'Create a new tribe.') + .option('-c --custom-domain', { + description: 'Do not use a subdomain of the tribes server; instead, use a custom domain.', + }) + .option('-s --subdomain', { + description: 'Use a subdomain of the tribes server to host the new Ditto instance.', + });