ecs-pf
Version:
CLI for port-forwarding to RDS via AWS ECS
188 lines (187 loc) • 8.1 kB
JavaScript
import chalk from "chalk";
export const messages = {
info: (message) => {
console.log(chalk.blue(message));
},
success: (message) => {
console.log(chalk.green(message));
},
error: (message) => {
console.log(chalk.red(message));
},
warning: (message) => {
console.log(chalk.yellow(message));
},
log: (message) => {
console.log(message);
},
warn: (message) => {
console.warn(chalk.yellow(message));
},
debug: (message) => {
console.log(chalk.gray(`[DEBUG] ${message}`));
},
clearLines: (count) => {
for (const _ of Array(count)) {
process.stdout.write("\x1b[1A");
process.stdout.write("\x1b[2K");
}
},
clearPreviousLine: () => {
process.stdout.write("\x1b[1A\x1b[2K\r");
},
clearCurrentLine: () => {
process.stdout.write("\r\x1b[2K");
},
clearAndReplace: (newMessage, color = "success") => {
process.stdout.write("\x1b[1A");
process.stdout.write("\x1b[2K");
process.stdout.write("\r");
messages[color](newMessage);
},
cyan: (message) => {
console.log(chalk.cyan(message));
},
white: (message) => {
console.log(chalk.white(message));
},
gray: (message) => {
console.log(chalk.gray(message));
},
bold: {
info: (message) => {
console.log(chalk.blue.bold(message));
},
success: (message) => {
console.log(chalk.green.bold(message));
},
error: (message) => {
console.log(chalk.red.bold(message));
},
warning: (message) => {
console.log(chalk.yellow.bold(message));
},
white: (message) => {
console.log(chalk.white.bold(message));
},
gray: (message) => {
console.log(chalk.gray.bold(message));
},
},
empty: () => {
console.log("");
},
ui: {
displaySelectionState: (selections) => {
console.clear();
console.log(chalk.bold.white("Select Network Configuration"));
messages.empty();
const formatSelectionLine = (label, value, isPlaceholder = false, placeholderLabel) => {
const labelWidth = 12;
const totalWidth = 60;
const paddedLabel = label.padEnd(labelWidth, " ");
if (!value) {
const placeholder = "____________";
const spaces = " ".repeat(Math.max(0, totalWidth - labelWidth - 3 - placeholder.length));
return ` ${paddedLabel}: ${spaces}${chalk.gray(placeholder)}`;
}
if (isPlaceholder && placeholderLabel) {
const formattedLabel = `(${placeholderLabel})`;
const paddedPlaceholderLabel = formattedLabel.padEnd(labelWidth, " ");
const spaces = " ".repeat(Math.max(0, totalWidth - labelWidth - 3 - value.length));
return ` ${chalk.gray.italic(paddedPlaceholderLabel)}: ${spaces}${chalk.gray.italic(value)}`;
}
if (isPlaceholder) {
const formattedValue = `(${value})`;
const spaces = " ".repeat(Math.max(0, totalWidth - labelWidth - 3 - formattedValue.length));
return ` ${paddedLabel}: ${spaces}${chalk.gray.italic(formattedValue)}`;
}
const spaces = " ".repeat(Math.max(0, totalWidth - labelWidth - 3 - value.length));
return ` ${paddedLabel}: ${spaces}${chalk.cyan(value)}`;
};
console.log(formatSelectionLine("Region", selections.region));
console.log(formatSelectionLine("RDS", selections.rds));
if (selections.rds && selections.rdsPort) {
console.log(formatSelectionLine("", selections.rdsPort, true, "RDS port"));
}
else if (selections.rds) {
console.log(formatSelectionLine("", "determining port...", true));
}
else {
console.log(formatSelectionLine("", undefined));
}
console.log(formatSelectionLine("ECS Target", selections.ecsTarget));
if (selections.ecsTarget && selections.ecsCluster) {
console.log(formatSelectionLine("", selections.ecsCluster, true, "ECS Cluster"));
}
else if (selections.ecsTarget) {
console.log(formatSelectionLine("", "determining cluster...", true));
}
else {
console.log(formatSelectionLine("", undefined));
}
console.log(formatSelectionLine("Local Port", selections.localPort));
messages.empty();
},
displayExecSelectionState: (selections) => {
console.clear();
console.log(chalk.bold.white("ECS Execute Command Configuration"));
messages.empty();
const formatSelectionLine = (label, value) => {
const labelWidth = 12;
const totalWidth = 60;
const paddedLabel = label.padEnd(labelWidth, " ");
if (!value) {
const placeholder = "____________";
const spaces = " ".repeat(Math.max(0, totalWidth - labelWidth - 3 - placeholder.length));
return ` ${paddedLabel}: ${spaces}${chalk.gray(placeholder)}`;
}
const spaces = " ".repeat(Math.max(0, totalWidth - labelWidth - 3 - value.length));
return ` ${paddedLabel}: ${spaces}${chalk.cyan(value)}`;
};
console.log(formatSelectionLine("Region", selections.region));
console.log(formatSelectionLine("Cluster", selections.cluster));
console.log(formatSelectionLine("Task", selections.task));
console.log(formatSelectionLine("Container", selections.container));
console.log(formatSelectionLine("Command", selections.command));
messages.empty();
},
},
dryRun: {
header: () => {
console.log("");
console.log(chalk.cyan("🏃 Dry Run Mode - Commands that would be executed:"));
console.log("");
},
awsCommand: (cmd) => {
console.log(chalk.blue("AWS Command:"));
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
console.log(cmd);
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
console.log("");
},
reproducibleCommand: (cmd) => {
console.log(chalk.green("Reproducible Command:"));
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
console.log(cmd);
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
console.log("");
},
sessionInfo: (info) => {
console.log(chalk.yellow("Session Information:"));
console.log(`Region: ${info.region}`);
console.log(`Cluster: ${info.cluster}`);
console.log(`Task: ${info.task}`);
if (info.rds) {
console.log(`RDS: ${info.rds}`);
console.log(`RDS Port: ${info.rdsPort}`);
console.log(`Local Port: ${info.localPort}`);
}
if (info.container) {
console.log(`Container: ${info.container}`);
console.log(`Command: ${info.command}`);
}
console.log("");
},
},
};