switch over to new CLI parser

This commit is contained in:
Siddharth Singh 2024-09-27 21:25:01 +05:30
parent 5b09a99729
commit 8511ae2137
No known key found for this signature in database
7 changed files with 46 additions and 70 deletions

View file

@ -1,50 +1,31 @@
import { parseArgs } from '@std/cli';
import { tribe } from './tribe/mod.ts'; import { tribe } from './tribe/mod.ts';
import { remote } from './remote/mod.ts'; import { remote } from './remote/mod.ts';
import { ParsedSubcommand, parseSubcommand } from './utils.ts'; import { Command, defaultIdentityFile, setupCli } from './utils/mod.ts';
const commands = { async function main() {
tribe, const tribes = new Command('tribes-cli', 'Create and manage Ditto Tribes servers.')
remote, .subcommand('tribe', tribe)
}; .subcommand('remote', remote);
let parserArgs: Partial<ParsedSubcommand> = { const { parsed } = setupCli(tribes, {
string: ['identity-file'], string: ['identity-file'],
}; default: {
'identity-file': await defaultIdentityFile(),
},
});
for (const [_name, body] of Object.entries(commands)) { const [s, v] = parsed._;
for (const subcommand in body) {
const s = body[subcommand]; let cmd = tribes;
parserArgs = parseSubcommand(s, parserArgs); 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); if (import.meta.main) {
console.log(parserArgs); await main();
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);
}
} }

View file

@ -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');

View file

@ -1,9 +1,5 @@
import { Subcommand } from '../utils.ts'; import { Command } from '../utils/mod.ts';
import { init } from './init.ts'; import { init } from './init.ts';
export const remote: Record<string, Subcommand> = { export const remote = new Command('remote', 'Manage a Ditto Tribes server')
init: { .subcommand('init', init);
action: init,
description: 'Initialise a brand new Ditto remote.',
},
};

View file

@ -1,5 +1,6 @@
import { Command } from '../utils/mod.ts';
/** /**
* Change a tribe's configuration. * Change a tribe's configuration.
*/ */
export const config = () => { export const config = new Command('config', "Modify a tribe's configuration");
};

View file

@ -2,5 +2,6 @@
* Destroy a tribe. Non-reversible. * 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!');

View file

@ -1,18 +1,7 @@
import { create } from './new.ts'; import { create } from './new.ts';
import { config } from './config.ts'; import { config } from './config.ts';
import { Subcommand } from '../utils.ts'; import { Command } from '../utils/mod.ts';
export const tribe: Record<string, Subcommand> = { export const tribe = new Command('tribe', 'Create and manage tribes.')
create: { .subcommand('create', create)
action: create, .subcommand('config', config);
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.',
},
},
},
};

View file

@ -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.',
});