ecs-pf
Version:
CLI for port-forwarding to RDS via AWS ECS
63 lines (62 loc) • 2.9 kB
JavaScript
import { search } from "@inquirer/prompts";
import { isEmpty } from "remeda";
import { inferECSTargets } from "../../inference/index.js";
import { isTaskArnShape } from "../../regex.js";
import { searchInferenceResults } from "../../search.js";
import { unwrapBrandedString } from "../../types.js";
import { messages } from "../../utils/index.js";
import { clearLoadingMessage } from "../ui/display-utils.js";
const DEFAULT_PAGE_SIZE = 50;
export async function selectECSTarget(params) {
const { ecsClient, selectedRDS, options, selections } = params;
messages.warning("Searching all ECS clusters for targets with exec capability that can connect to this RDS...");
const inferenceResults = await inferECSTargets({
ecsClient,
selectedRDS,
});
if (isEmpty(inferenceResults)) {
throw new Error("No ECS targets found with exec capability that can connect to this RDS instance");
}
clearLoadingMessage();
messages.success(`Found ${inferenceResults.length} potential ECS targets`);
messages.empty();
if (options.cluster && options.task) {
const matchingResult = inferenceResults.find((result) => {
const clusterMatch = result.cluster.clusterName === options.cluster;
if (isTaskArnShape(options.task)) {
return (clusterMatch &&
result.task.taskArn === options.task);
}
else {
return clusterMatch && result.task.taskId === options.task;
}
});
if (matchingResult) {
const inference = matchingResult;
const task = matchingResult.task.taskArn;
selections.ecsCluster = unwrapBrandedString(matchingResult.cluster.clusterName);
selections.ecsTarget = unwrapBrandedString(matchingResult.task.taskArn);
messages.success(`✓ ECS cluster (from CLI): ${options.cluster}`);
messages.success(`✓ ECS task (from CLI): ${options.task}`);
return { selectedInference: inference, selectedTask: task };
}
messages.warning(`Specified cluster/task not found in inference results`);
}
const selectedInference = await search({
message: "Select ECS target:",
source: async (input) => {
return await searchInferenceResults(inferenceResults, input || "");
},
pageSize: DEFAULT_PAGE_SIZE,
});
if (!selectedInference ||
typeof selectedInference !== "object" ||
!("task" in selectedInference)) {
throw new Error("Invalid inference selection");
}
const inference = selectedInference;
const task = inference.task.taskArn;
selections.ecsCluster = unwrapBrandedString(inference.cluster.clusterName);
selections.ecsTarget = unwrapBrandedString(inference.task.taskArn);
return { selectedInference: inference, selectedTask: task };
}