all repos — ssh-portal @ 4f6d8725ca79cf18bec78f4d71034f4d74a8fda2

SSH abstraction tool to be productive in my work

main.ts (view raw)

 1import { Client } from "npm:ssh2";
 2import { Select } from "https://deno.land/x/cliffy@v1.0.0-rc.4/prompt/select.ts";
 3import { tty } from "https://deno.land/x/cliffy@v1.0.0-rc.4/ansi/tty.ts";
 4import { colors } from "https://deno.land/x/cliffy@v1.0.0-rc.4/ansi/colors.ts";
 5
 6const instances = (
 7  await import("./instances.json", {
 8    with: { type: "json" },
 9  })
10).default;
11const actions = (
12  await import("./actions.json", {
13    with: { type: "json" },
14  })
15).default;
16
17const baseConfig = {
18  host: "gate.hidora.com",
19  port: 3022,
20  privateKey: Deno.readTextFileSync("/home/tim/.ssh/id_rsa"),
21};
22
23const instancesByCategory = Object.groupBy(instances, i => i.category);
24const configOptions = Object.entries(instancesByCategory).map(
25  ([category, instances]) => {
26    return {
27      name: category,
28      options: instances?.map(instance => ({
29        name: instance.name,
30        value: instance,
31      })),
32    };
33  }
34);
35const selectedConfig = await Select.prompt<(typeof instances)[number]>({
36  message: "Instance",
37  search: true,
38  groupIcon: "🔵",
39  options: configOptions,
40});
41
42const selectedAction = await Select.prompt<(typeof actions)[number]>({
43  message: "Action",
44  options: actions.map(c => ({ name: c.name, value: c })),
45});
46
47const secondaryText = colors.gray;
48
49const client = new Client().connect({ ...selectedConfig, ...baseConfig });
50console.log(`🚀 Connecting to ${selectedConfig.name}...`);
51client.on("ready", () => {
52  tty.cursorTo(0, 0).eraseScreen();
53  console.log(secondaryText(`✅ Connected to ${selectedConfig.name}`));
54  console.log(secondaryText(`$ ${selectedAction.cmd}`));
55  client.exec(selectedAction.cmd, (err: Error, stream: any) => {
56    if (err) throw err;
57    stream
58      .on("close", () => {
59        client.end();
60      })
61      .on("data", (data: string) => {
62        const date = new Date();
63        const dateText = colors.bold.black.bgBlue;
64        console.log(dateText(`${date.toLocaleString()}\n`) + data);
65      })
66      .stderr.on("data", (data: string) => {
67        const date = new Date();
68        const dateText = colors.bold.black.bgRed;
69        console.log(`${dateText(date.toLocaleString())}) - ERROR\n` + data);
70      });
71  });
72});