feat: ssh.ts

This commit is contained in:
2026-01-11 13:45:16 +08:00
parent 0ff683fa4f
commit 97647ec2f7

View File

@@ -7,9 +7,16 @@ const { parseArgs } = require("node:util");
const fs = require("node:fs"); const fs = require("node:fs");
const os = require("node:os"); const os = require("node:os");
// reference: https://bun.com/docs/runtime/color
const GREEN = Bun.color("green", "ansi");
const BLUE = Bun.color("lightblue", "ansi");
const YELLOW = Bun.color("yellow", "ansi");
const RESET = "\x1B[0m";
class SshTsArgs { class SshTsArgs {
forwardAgent?: boolean; forwardAgent?: boolean;
proxy?: boolean; proxy?: boolean;
help?: boolean;
host?: string; host?: string;
} }
@@ -48,11 +55,6 @@ function printSshConfig(sshConfig: SshConfig) {
maxProfileHostLength = sshProfile.host.length; maxProfileHostLength = sshProfile.host.length;
} }
} }
// reference: https://bun.com/docs/runtime/color
const GREEN = Bun.color("green", "ansi");
const BLUE = Bun.color("lightblue", "ansi");
const YELLOW = Bun.color("yellow", "ansi");
const RESET = "\x1B[0m";
console.log( console.log(
`${GREEN}[OK ]${RESET} Total ${allProfiles.length} server(s):`, `${GREEN}[OK ]${RESET} Total ${allProfiles.length} server(s):`,
); );
@@ -140,15 +142,36 @@ async function main() {
type: "boolean", type: "boolean",
short: "p", short: "p",
}, },
"help": {
type: "boolean",
short: "h",
},
"host": { "host": {
type: "string", type: "string",
short: "h", short: "H",
}, },
}; };
const { values, tokens } = parseArgs({ args, options, tokens: true }); const { values, positionals } = parseArgs({
args,
options,
allowPositionals: true,
tokens: true,
});
const sshTsArgs = values as SshTsArgs; const sshTsArgs = values as SshTsArgs;
if (sshTsArgs.help) {
console.log("ssh.ts [-h|--help]");
console.log(
"ssh.ts [-f|--forward-agent] [-p|--proxy] [-H|--host] [username@]host",
);
console.log("ssh.ts [-f|--forward-agent] [-p|--proxy] [username@]host");
return;
}
sshTsArgs.forwardAgent = values["forward-agent"]; sshTsArgs.forwardAgent = values["forward-agent"];
if (!sshTsArgs.host && positionals && positionals.length > 0) {
sshTsArgs.host = positionals[0];
}
if (!sshTsArgs.host) { if (!sshTsArgs.host) {
console.error("[ERROR] --host required"); console.error("[ERROR] --host required");
return; return;
@@ -182,7 +205,7 @@ async function main() {
`${sshUsername}@${sshProfile.host}`, `${sshUsername}@${sshProfile.host}`,
); );
console.log(`Command: ${sshCommand} ${sshArgs.join(" ")}`); console.log(`${GREEN}[OK ]${RESET} ${sshCommand} ${sshArgs.join(" ")}`);
spawn(sshCommand, sshArgs, { spawn(sshCommand, sshArgs, {
shell: true, shell: true,
stdio: ["inherit", "inherit", "inherit"], stdio: ["inherit", "inherit", "inherit"],