3 Commits
+27 -4
View File
@@ -8,15 +8,17 @@ type Args = {
help: boolean
configDir: string
knownHosts?: string | true
localOnly?: string | true
}
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("Options:");
console.log(" -h | --help Show this help message");
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(" -l | --local-only [user] Include known_hosts only (root if no user provided)");
}
const parseArgs = (argv: string[]): Args => {
@@ -57,6 +59,18 @@ const parseArgs = (argv: string[]): Args => {
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:
throw new Error(`Unknown argument: ${arg}`);
}
@@ -97,7 +111,8 @@ const loadConfig = async () => {
port: 22,
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("[")) {
const hostparts: any = parts[0].split(":");
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();
await loadHosts();
}
return config;
}
@@ -166,7 +185,7 @@ const tuiMenu = async () => {
records += ` | ${host.user} | ${host.port}`;
});
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) {
process.exit(1);
}
@@ -186,4 +205,8 @@ const hostIndex = Number(await tuiMenu());
const cmd = buildSSHCommnand(config.hosts[hostIndex]);
console.log(cmd.join(" "));
await $`${cmd}`;
try {
await $`${cmd}`;
} catch (error) {
process.exit(1);
}