ecs-pf
Version:
CLI for port-forwarding to RDS via AWS ECS
44 lines (43 loc) • 1.75 kB
JavaScript
import { input } from "@inquirer/prompts";
import { isDefined } from "remeda";
import { parsePort } from "../../types/parsers.js";
import { findAvailablePort, getPortRange, isPortRange, messages, } from "../../utils/index.js";
import { clearLoadingMessage } from "../ui/display-utils.js";
export async function selectLocalPort(options, selections) {
if (isDefined(options.localPort)) {
const portResult = parsePort(options.localPort);
if (!portResult.success)
throw new Error(portResult.error);
selections.localPort = portResult.data;
messages.success(`✓ Local port (from CLI): ${options.localPort}`);
return `${options.localPort}`;
}
try {
messages.warning("Finding available local port...");
const availablePort = await findAvailablePort(8888);
const portResult = parsePort(availablePort);
if (!portResult.success)
throw new Error(portResult.error);
selections.localPort = portResult.data;
clearLoadingMessage();
return `${availablePort}`;
}
catch {
const port = await input({
message: "Enter local port number:",
default: "8888",
validate: (inputValue) => {
const port = parseInt(inputValue || "8888");
const [minPort, maxPort] = getPortRange();
return isPortRange(port)
? true
: `Please enter a valid port number (${minPort}-${maxPort})`;
},
});
const portResult = parsePort(port);
if (!portResult.success)
throw new Error(portResult.error);
selections.localPort = portResult.data;
return port;
}
}