3 Commits
+26 -3
View File
@@ -8,15 +8,17 @@ type Args = {
help: boolean help: boolean
configDir: string configDir: string
knownHosts?: string | true knownHosts?: string | true
localOnly?: string | true
} }
const usage = () => { const usage = () => {
console.log("Usage: sshm [--config-dir DIR] [-k [user]]"); console.log("Usage: sshm [--config-dir DIR] [ [-k [user]] || [-l [user]] ]");
console.log(""); console.log("");
console.log("Options:"); console.log("Options:");
console.log(" -h | --help Show this help message"); console.log(" -h | --help Show this help message");
console.log(" -c | --config-dir DIR Use a custom config directory"); console.log(" -c | --config-dir DIR Use a custom config directory");
console.log(" -k | --known-hosts [user] Include known_hosts (root if no user provided)"); console.log(" -k | --known-hosts [user] Include known_hosts (root if no user provided)");
console.log(" -l | --local-only [user] Include known_hosts only (root if no user provided)");
} }
const parseArgs = (argv: string[]): Args => { const parseArgs = (argv: string[]): Args => {
@@ -57,6 +59,18 @@ const parseArgs = (argv: string[]): Args => {
break; break;
} }
case "-l":
case "--local-only": {
const next = argv[i + 1];
if (next && !next.startsWith("-")) {
args.localOnly = next;
i++;
} else {
args.localOnly = true;
}
break;
}
default: default:
throw new Error(`Unknown argument: ${arg}`); throw new Error(`Unknown argument: ${arg}`);
} }
@@ -97,7 +111,8 @@ const loadConfig = async () => {
port: 22, port: 22,
name: '', name: '',
} }
if (args.knownHosts != true) target.user = args.knownHosts as string; if (args.knownHosts && args.knownHosts != true) target.user = args.knownHosts as string;
if (args.localOnly && args.localOnly != true) target.user = args.localOnly as string;
if (parts[0].startsWith("[")) { if (parts[0].startsWith("[")) {
const hostparts: any = parts[0].split(":"); const hostparts: any = parts[0].split(":");
target.host = hostparts[0].substring(1, hostparts[0].length - 1); target.host = hostparts[0].substring(1, hostparts[0].length - 1);
@@ -146,8 +161,12 @@ const loadConfig = async () => {
} }
} }
if (args.localOnly) {
await loadKnownHosts();
} else {
if (args.knownHosts) await loadKnownHosts(); if (args.knownHosts) await loadKnownHosts();
await loadHosts(); await loadHosts();
}
return config; return config;
} }
@@ -166,7 +185,7 @@ const tuiMenu = async () => {
records += ` | ${host.user} | ${host.port}`; records += ` | ${host.user} | ${host.port}`;
}); });
try { try {
return await $`echo "${records}" | fzf -e --layout=reverse --with-nth=2,3,4,5,6 --accept-nth=1 --delimiter="|"`.text(); return await $`echo "${records}" | column -t -s "|" -o "|" | fzf -e --layout=reverse --with-nth=2,3,4,5,6 --accept-nth=1 --delimiter="|"`.text();
} catch (error) { } catch (error) {
process.exit(1); process.exit(1);
} }
@@ -186,4 +205,8 @@ const hostIndex = Number(await tuiMenu());
const cmd = buildSSHCommnand(config.hosts[hostIndex]); const cmd = buildSSHCommnand(config.hosts[hostIndex]);
console.log(cmd.join(" ")); console.log(cmd.join(" "));
try {
await $`${cmd}`; await $`${cmd}`;
} catch (error) {
process.exit(1);
}