Skip to content

Commit

Permalink
use variadic positional yargs for command and its arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
GGonryun committed Feb 22, 2024
1 parent 4c4cc49 commit c3148c1
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/commands/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,18 +30,23 @@ const GRANT_TIMEOUT_MILLIS = 60e3;

export const sshCommand = (yargs: yargs.Argv) =>
yargs.command<SshCommandArgs>(
"ssh <instance> [command]",
"ssh <instance> [command [arguments..]]",
"SSH into a virtual machine",
(yargs) =>
yargs
.positional("instance", {
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)
);
Expand Down Expand Up @@ -123,5 +132,12 @@ const ssh = async (args: yargs.ArgumentsCamelCase<SshCommandArgs>) => {
const { id, isPreexisting } = response;
if (!isPreexisting) print2("Waiting for access to be provisioned");
const requestData = await waitForProvisioning<AwsSsh>(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,
});
};

0 comments on commit c3148c1

Please sign in to comment.