This commit is contained in:
2026-02-07 01:00:45 +08:00
parent 63d2a5b9cd
commit 6739f5b86b
3 changed files with 258 additions and 45 deletions

View File

@@ -1,19 +1,11 @@
#!/usr/bin/env runts -- --runtime-bun
#!/usr/bin/env runts -- --allow-env --allow-read --allow-run
// reference: https://git.hatter.ink/rust-scripts/scriptbase/src/branch/main/ssh-rs/src/main.rs
import { spawn } from "node:child_process";
import { parseArgs } from "node:util";
import {parseArgs} from "node:util";
import fs from "node:fs";
import os from "node:os";
import JSON5 from "json5";
// 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";
import {log, readFileToString, term, execCommandShell} from "../libraries/deno-commons-mod.ts";
import JSON5 from "npm:json5";
class SshTsArgs {
forwardAgent?: boolean;
@@ -57,9 +49,7 @@ function printSshConfig(sshConfig: SshConfig) {
maxProfileHostLength = sshProfile.host.length;
}
}
console.log(
`${GREEN}[OK ]${RESET} Total ${allProfiles.length} server(s):`,
);
console.log(term.auto("[green][OK ][/green] Total 10 server(s):"));
allProfiles.sort((a, b) => a.localeCompare(b));
for (let i = 0; i < allProfiles.length; i++) {
const k = allProfiles[i];
@@ -67,37 +57,38 @@ function printSshConfig(sshConfig: SshConfig) {
const features = [];
if (sshProfile.proxy) features.push("proxy");
if (sshProfile.forward_agent) features.push("forward_agent");
console.log(
`- ${k}${
" ".repeat(maxProfileNameLength - k.length)
} : ${BLUE}${sshProfile.host}${
" ".repeat(maxProfileHostLength - sshProfile.host.length)
}${RESET} ${YELLOW}${
(sshProfile.alias && sshProfile.alias.length == 1)
? "alias "
: "aliases"
}: [${
(sshProfile.alias && sshProfile.alias.join(", ")) || ""
}]${RESET} # ${sshProfile.comment}${
const nameWithPad = `${k}${
" ".repeat(maxProfileNameLength - k.length)
}`;
const hostWithPad = `${sshProfile.host}${
" ".repeat(maxProfileHostLength - sshProfile.host.length)
}`;
const alias = `${
(sshProfile.alias && sshProfile.alias.length == 1)
? "alias "
: "aliases"
}: [${(sshProfile.alias && sshProfile.alias.join(", ")) || ""}]`;
console.log(term.auto(
`- ${nameWithPad} : [blue]${hostWithPad}[/blue] [yellow]${alias}[/yellow] # ${sshProfile.comment}${
(features.length > 0) ? (" ;[" + features.join(" ") + "]") : ""
}`,
);
));
}
if (sshConfig.default_proxy || sshConfig.default_forward_agent) {
const features = [];
if (sshConfig.default_proxy) features.push("proxy");
if (sshConfig.default_forward_agent) features.push("forward_agent");
console.log(
`\n[INFO ] Global default features: [${features.join(" ")}]`,
);
console.log();
log.info(`Global default features: [${features.join(" ")}]`);
}
}
function loadSshConfig(): SshConfig {
const configFile = os.homedir() + "/.config/ssh-rs-config.json";
async function loadSshConfig(): SshConfig {
try {
const sshConfigText = fs.readFileSync(configFile, "utf8");
const sshConfigText = await readFileToString(
"~/.config/ssh-rs-config.json",
);
return JSON5.parse(sshConfigText);
} catch (e) {
throw `Load config file: ${configFile} failed: ${e}`;
@@ -137,7 +128,7 @@ function parseUsernameAndHost(usernameAndHost: string): UsernameAndHost {
}
async function main() {
const sshConfig = loadSshConfig();
const sshConfig = await loadSshConfig();
if (process.argv.length <= 2) {
printSshConfig(sshConfig);
@@ -204,13 +195,10 @@ async function main() {
sshConfig.default_username || "root";
sshArgs.push(`${sshUsername}@${sshProfile.host}`);
console.log(`${GREEN}[OK ]${RESET} ${sshCommand} ${sshArgs.join(" ")}`);
spawn(sshCommand, sshArgs, {
shell: true,
stdio: ["inherit", "inherit", "inherit"],
});
console.log(term.auto(`[green][OK ][/green] ${sshCommand} ${sshArgs.join(" ")}`));
await execCommandShell(sshCommand, sshArgs);
}
await main();
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260126T004612+08:00.MEQCIFxHjeu7+pkPpSWVoCTb
// BbzbHhc+dsAG6vqx+i0H8MqLAiAYcK0dcCVECG1je0Q/ZCIYohHCNrvDIOr3kiNj7XWoXg==
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260207T010008+08:00.MEUCIEmqd5Xb7955nioxaPgT
// iHA794kvFjktf5GysW6x83aQAiEAkYRhu9ycPCRjMZa+iJRmNTxYz14O0WawQ3X32MQVIc0=