UNPKG

ecs-pf

Version:

CLI for port-forwarding to RDS via AWS ECS

74 lines (73 loc) 3.16 kB
import { search } from "@inquirer/prompts"; import { getRDSInstances } from "../../aws-services.js"; import { searchRDS } from "../../search.js"; import { parseDBInstanceIdentifier, parsePort } from "../../types/parsers.js"; import { getDefaultPortForEngine, messages } from "../../utils/index.js"; import { clearLoadingMessage } from "../ui/display-utils.js"; const DEFAULT_PAGE_SIZE = 50; export async function selectRDSInstance(rdsClient, options, selections) { if (options.rds) { const rdsIdResult = parseDBInstanceIdentifier(options.rds); if (!rdsIdResult.success) throw new Error(rdsIdResult.error); selections.rds = rdsIdResult.data; messages.success(`✓ RDS (from CLI): ${options.rds}`); messages.info("Validating RDS instance..."); const rdsInstancesResult = await getRDSInstances(rdsClient); if (!rdsInstancesResult.success) { throw new Error(`Failed to get RDS instances: ${rdsInstancesResult.error}`); } const rdsInstance = rdsInstancesResult.data.find((r) => String(r.dbInstanceIdentifier) === options.rds); if (!rdsInstance) { throw new Error(`RDS instance not found: ${options.rds}`); } return rdsInstance; } messages.warning("Getting RDS instances..."); const rdsInstancesResult = await getRDSInstances(rdsClient); if (!rdsInstancesResult.success) { throw new Error(`Failed to get RDS instances: ${rdsInstancesResult.error}`); } const rdsInstances = rdsInstancesResult.data; if (rdsInstances.length === 0) { throw new Error("No RDS instances found"); } clearLoadingMessage(); const selectedRDS = await search({ message: "Search and select RDS instance:", source: async (input) => { return await searchRDS(rdsInstances, input || ""); }, pageSize: DEFAULT_PAGE_SIZE, }); if (!selectedRDS || typeof selectedRDS !== "object" || !("dbInstanceIdentifier" in selectedRDS)) { throw new Error("Invalid RDS selection"); } const rdsInstance = selectedRDS; const rdsIdResult = parseDBInstanceIdentifier(String(rdsInstance.dbInstanceIdentifier)); if (!rdsIdResult.success) throw new Error(rdsIdResult.error); selections.rds = rdsIdResult.data; return rdsInstance; } export function determineRDSPort(selectedRDS, options, selections) { if (options.rdsPort) { const portResult = parsePort(options.rdsPort); if (!portResult.success) throw new Error(portResult.error); selections.rdsPort = portResult.data; messages.success(`✓ RDS port (from CLI): ${options.rdsPort}`); return options.rdsPort; } const actualRDSPort = selectedRDS.port; const fallbackPort = getDefaultPortForEngine(selectedRDS.engine); const port = actualRDSPort || fallbackPort; const portResult = parsePort(port); if (!portResult.success) throw new Error(portResult.error); selections.rdsPort = portResult.data; messages.success(`✓ RDS port (auto-detected): ${port}`); return `${port}`; }