mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
split up utils
This commit is contained in:
parent
4f94309ec2
commit
5b09a99729
3 changed files with 101 additions and 27 deletions
55
tribes-cli/utils/command.ts
Normal file
55
tribes-cli/utils/command.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import { Option, ParsedArgs } from './mod.ts';
|
||||||
|
|
||||||
|
export class Command {
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
action: (args: ParsedArgs) => void | Promise<void> = (_) => {};
|
||||||
|
options: Record<string, Option> = {};
|
||||||
|
commands: Record<string, Command> = {};
|
||||||
|
|
||||||
|
constructor(name: string, description: string) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
get subcommandCount() {
|
||||||
|
return Object.keys(this.commands).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
isValidSubcommand(key: string): boolean {
|
||||||
|
return Object.keys(this.commands).includes(key.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
getSubcommand(subcommand: string | number) {
|
||||||
|
if (!subcommand) {
|
||||||
|
throw new Error('tribes-cli: error: invalid subcommand');
|
||||||
|
}
|
||||||
|
if (typeof subcommand === 'number') {
|
||||||
|
throw new Error('tribes-cli: error: subcommand cannot be a number');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isValidSubcommand(subcommand)) {
|
||||||
|
return this.commands[subcommand];
|
||||||
|
} else {
|
||||||
|
throw new Error(`tribes-cli: error: ${subcommand} is not a valid subcommand`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setAction(action: Command['action']) {
|
||||||
|
this.action = action;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
subcommand(name: string, command: Command) {
|
||||||
|
if (this.isValidSubcommand(name)) {
|
||||||
|
throw new Error(`tribes-cli: error: ${name} is already a subcommand.`);
|
||||||
|
}
|
||||||
|
this.commands[name] = command;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
option(fmt: string, option: Option) {
|
||||||
|
this.options[fmt] = option;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
31
tribes-cli/utils/mod.ts
Normal file
31
tribes-cli/utils/mod.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import { join } from '@std/path';
|
||||||
|
import { ParsedArgs, setupCli } from './parsing.ts';
|
||||||
|
import { Command } from './command.ts';
|
||||||
|
|
||||||
|
export const defaultIdentityFile = async () => {
|
||||||
|
const home = Deno.env.get('HOME');
|
||||||
|
if (!home) throw new Error('tribes-cli: unable to find default identity file');
|
||||||
|
const path = join(home, '.ssh', 'id_rsa');
|
||||||
|
try {
|
||||||
|
await Deno.stat(path);
|
||||||
|
return path;
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
throw new Error('tribes-cli: unable to find default identity file.');
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Option =
|
||||||
|
& { description: string }
|
||||||
|
& ({
|
||||||
|
default?: string;
|
||||||
|
} | {
|
||||||
|
bool: true;
|
||||||
|
default?: boolean;
|
||||||
|
});
|
||||||
|
|
||||||
|
export const cleanArg = (arg: string) => {
|
||||||
|
return arg.replace(/^--?/g, '');
|
||||||
|
};
|
||||||
|
|
||||||
|
export { Command, setupCli };
|
||||||
|
export type { ParsedArgs };
|
||||||
|
|
@ -1,33 +1,21 @@
|
||||||
import { join } from '@std/path';
|
import { parseArgs as stdParseArgs } from '@std/cli';
|
||||||
|
import { cleanArg, Command } from './mod.ts';
|
||||||
|
|
||||||
let identity = '';
|
export const setupCli = (commands: Command, parserArgs: Partial<ParsedSubcommand> = {}) => {
|
||||||
export const defaultIdentityFile = async () => {
|
for (const [_name, body] of Object.entries(commands)) {
|
||||||
if (!identity) {
|
for (const subcommand in body) {
|
||||||
const home = Deno.env.get('HOME');
|
const s = body[subcommand];
|
||||||
if (home) return identity = await Deno.readTextFile(join(home, '.ssh', 'id_rsa'));
|
parserArgs = parseSubcommand(s, parserArgs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return '';
|
|
||||||
|
const parsed = stdParseArgs(Deno.args, parserArgs);
|
||||||
|
return {
|
||||||
|
parsed,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface Subcommand {
|
export type ParsedArgs = ReturnType<typeof setupCli>['parsed'];
|
||||||
action: Function;
|
|
||||||
description: string;
|
|
||||||
options?: Record<
|
|
||||||
string,
|
|
||||||
& { description: string }
|
|
||||||
& ({
|
|
||||||
default?: string;
|
|
||||||
} | {
|
|
||||||
bool: true;
|
|
||||||
default?: boolean;
|
|
||||||
})
|
|
||||||
>;
|
|
||||||
commands?: Record<string, Subcommand>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const cleanArg = (arg: string) => {
|
|
||||||
return arg.replace(/^--?/g, '');
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface ParsedSubcommand {
|
export interface ParsedSubcommand {
|
||||||
string: string[];
|
string: string[];
|
||||||
|
|
@ -45,7 +33,7 @@ export const defaultParsedSubcommand = (): ParsedSubcommand => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parseSubcommand = (command: Subcommand, existing?: Partial<ParsedSubcommand>): ParsedSubcommand => {
|
export const parseSubcommand = (command: Command, existing?: Partial<ParsedSubcommand>): ParsedSubcommand => {
|
||||||
const res = Object.assign(defaultParsedSubcommand(), existing);
|
const res = Object.assign(defaultParsedSubcommand(), existing);
|
||||||
|
|
||||||
if (command.options) {
|
if (command.options) {
|
||||||
Loading…
Add table
Reference in a new issue