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 { 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<ParsedSubcommand> = {
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();
}

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';
export const remote: Record<string, Subcommand> = {
init: {
action: init,
description: 'Initialise a brand new Ditto remote.',
},
};
export const remote = new Command('remote', 'Manage a Ditto Tribes server')
.subcommand('init', init);

View file

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

View file

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

View file

@ -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<string, Subcommand> = {
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);

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