Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
GGonryun committed Feb 26, 2024
1 parent 0516d39 commit 44aebc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/commands/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { getDoc, onSnapshot } from "firebase/firestore";
import { pick } from "lodash";
import yargs from "yargs";

const LOCAL_PORT_FORWARD_PATTERN = /^\d+:\d+$/;

type SshCommandArgs = {
instance: string;
command?: string;
Expand Down Expand Up @@ -59,11 +61,19 @@ export const sshCommand = (yargs: yargs.Argv) =>
string: true,
default: [] as string[],
})
.check((argv: yargs.ArgumentsCamelCase<SshCommandArgs>) => {
if (argv.L == null) return true;

return (
argv.L.match(LOCAL_PORT_FORWARD_PATTERN) ||
"Local port forward should be in the format `local_port:remote_port`"
);
})
.option("L", {
type: "string",
describe:
// the order of the sockets in the address matches the ssh man page
'Forward a local port to the remote host ["local_socket:remote_socket"]',
"Forward a local port to the remote host; `local_socket:remote_socket`",
}),
guard(ssh)
);
Expand Down
15 changes: 8 additions & 7 deletions src/plugins/aws/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ const accessPropagationGuard = (
};

const createSsmCommand = (args: Omit<SsmArgs, "requestId">) => {
const hasCommand = args.command && args.command.trim();

if (hasCommand && args.forwardPortAddress) {
throw "Invalid arguments. Specify either a command or port forwarding, not both.";
}

const ssmCommand = [
"aws",
"ssm",
Expand All @@ -106,15 +112,10 @@ const createSsmCommand = (args: Omit<SsmArgs, "requestId">) => {
: args.documentName,
];

if (args.command && args.command.trim()) {
if (hasCommand) {
ssmCommand.push("--parameters", `command='${args.command}'`);
} else if (args.forwardPortAddress) {
const [localPort, remotePort] = args.forwardPortAddress
.split(":")
.map(Number);
if (!localPort || isNaN(localPort) || !remotePort || isNaN(remotePort)) {
throw "Invalid port forwarding address specified. Please use format <local_socket>:<remote_socket>";
}
const [localPort, remotePort] = args.forwardPortAddress.split(":");

ssmCommand.push(
"--parameters",
Expand Down

0 comments on commit 44aebc2

Please sign in to comment.