log commands as they are executed, dispose of ssh when done

This commit is contained in:
Siddharth Singh 2024-09-30 11:37:55 +05:30
parent 5cbdf30501
commit 339aff435b
No known key found for this signature in database

View file

@ -37,11 +37,11 @@ class TribesClient {
* @returns The standard output of the command.
*/
async do<K extends keyof typeof commands>(name: K, ...args: Parameters<typeof commands[K]>) {
const cmd = commands[name];
// @ts-ignore this is okay because typescript enforces the correct args at the callsite
const res = await this.ssh.execCommand(cmd(...args));
const cmd = commands[name](...args);
console.log(`=> ${cmd}`);
const res = await this.ssh.execCommand(cmd);
if (res.code) throw new Error(`Error executing ${name}: ${res.stderr}`);
console.error(res);
return res.stdout;
}
/**
@ -54,6 +54,9 @@ class TribesClient {
const stdout = await this.do(name, ...args);
console.log(stdout);
}
async disconnect() {
this.ssh.dispose();
}
}
interface HandlerOptions {
@ -68,7 +71,9 @@ export const makeSshAction = (handler: (opts: HandlerOptions) => Promise<void> |
const arg = (name: string) => args[name] as string;
const domain = args._[0] as string;
const tribes = await connect(domain, arg('identity-file'));
handler({ args, arg, tribes, domain });
await handler({ args, arg, tribes, domain });
console.log('Done.');
tribes.disconnect();
};
};