import { Client } from "npm:ssh2"; import { Select } from "https://deno.land/x/cliffy@v1.0.0-rc.4/prompt/select.ts"; import { tty } from "https://deno.land/x/cliffy@v1.0.0-rc.4/ansi/tty.ts"; import { colors } from "https://deno.land/x/cliffy@v1.0.0-rc.4/ansi/colors.ts"; const instances = ( await import("./instances.json", { with: { type: "json" }, }) ).default; const actions = ( await import("./actions.json", { with: { type: "json" }, }) ).default; const baseConfig = { host: "gate.hidora.com", port: 3022, privateKey: Deno.readTextFileSync("/home/tim/.ssh/id_rsa"), }; const instancesByCategory = Object.groupBy(instances, i => i.category); const configOptions = Object.entries(instancesByCategory).map( ([category, instances]) => { return { name: category, options: instances?.map(instance => ({ name: instance.name, value: instance, })), }; } ); const selectedConfig = await Select.prompt<(typeof instances)[number]>({ message: "Instance", search: true, groupIcon: "🔵", options: configOptions, }); const selectedAction = await Select.prompt<(typeof actions)[number]>({ message: "Action", options: actions.map(c => ({ name: c.name, value: c })), }); const secondaryText = colors.gray; const client = new Client().connect({ ...selectedConfig, ...baseConfig }); console.log(`🚀 Connecting to ${selectedConfig.name}...`); client.on("ready", () => { tty.cursorTo(0, 0).eraseScreen(); console.log(secondaryText(`✅ Connected to ${selectedConfig.name}`)); console.log(secondaryText(`$ ${selectedAction.cmd}`)); client.exec(selectedAction.cmd, (err: Error, stream: any) => { if (err) throw err; stream .on("close", () => { client.end(); }) .on("data", (data: string) => { const date = new Date(); const dateText = colors.bold.black.bgBlue; console.log(dateText(`${date.toLocaleString()}\n`) + data); }) .stderr.on("data", (data: string) => { const date = new Date(); const dateText = colors.bold.black.bgRed; console.log(`${dateText(date.toLocaleString())}) - ERROR\n` + data); }); }); });