From c3148c15dcd626893e81b94cfaa0d04b832c5ab3 Mon Sep 17 00:00:00 2001 From: Miguel Campos Date: Thu, 22 Feb 2024 11:29:38 -0800 Subject: [PATCH] use variadic positional yargs for command and its arguments --- src/commands/ssh.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/commands/ssh.ts b/src/commands/ssh.ts index bcc49fe..b3dea8a 100644 --- a/src/commands/ssh.ts +++ b/src/commands/ssh.ts @@ -17,7 +17,11 @@ import { getDoc, onSnapshot } from "firebase/firestore"; import { pick } from "lodash"; import yargs from "yargs"; -type SshCommandArgs = { instance: string; command?: string }; +type SshCommandArgs = { + instance: string; + command?: string; + arguments: string[]; +}; /** Maximum amount of time to wait after access is approved to wait for access * to be configured @@ -26,7 +30,7 @@ const GRANT_TIMEOUT_MILLIS = 60e3; export const sshCommand = (yargs: yargs.Argv) => yargs.command( - "ssh [command]", + "ssh [command [arguments..]]", "SSH into a virtual machine", (yargs) => yargs @@ -34,10 +38,15 @@ export const sshCommand = (yargs: yargs.Argv) => type: "string", demandOption: true, }) - .option("command", { - alias: "c", + .positional("command", { type: "string", - describe: "Command to run on the remote machine", + describe: "Optional command to run on the remote machine", + }) + .positional("arguments", { + describe: "Optional arguments to append to the command", + array: true, + string: true, + default: [] as string[], }), guard(ssh) ); @@ -123,5 +132,12 @@ const ssh = async (args: yargs.ArgumentsCamelCase) => { const { id, isPreexisting } = response; if (!isPreexisting) print2("Waiting for access to be provisioned"); const requestData = await waitForProvisioning(authn, id); - await ssm(authn, { ...requestData, id, command: args.command }); + await ssm(authn, { + ...requestData, + id, + // the command to run on the remote machine, if any + command: args.command + ? `${args.command} ${args.arguments.join(" ")}` + : undefined, + }); };