Skip to content

Commit

Permalink
Added sf ssh to ssh into instances, including if the instance has a…
Browse files Browse the repository at this point in the history
… port on its IP (#25)

* sketched out ssh

* increased table width, added message about how to ssh

* fixed formatting
  • Loading branch information
agajews authored Sep 20, 2024
1 parent 0bef873 commit 65adbf6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/lib/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function listInstancesAction({
} else {
const table = new Table({
head: tableHeaders,
colWidths: [32, 10, 20],
// colWidths: [32, 10, 20],
});

table.push(
Expand All @@ -87,7 +87,11 @@ async function listInstancesAction({
instance.status,
]),
);
console.log(table.toString() + "\n\n");
console.log(
table.toString() +
"\n" +
"To ssh into an instance, run `sf ssh <instance-id>`.\n",
);
}
}

Expand Down Expand Up @@ -152,7 +156,7 @@ const colorInstanceType = (instanceType: InstanceType) =>

// --

async function getInstances({
export async function getInstances({
clusterId,
}: { clusterId?: string }): Promise<InstanceObject[]> {
const loggedIn = await isLoggedIn();
Expand Down
21 changes: 21 additions & 0 deletions src/lib/ssh.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { $ } from "bun";
import type { Command } from "commander";
import { apiClient } from "../apiClient";
import { isLoggedIn } from "../helpers/config";
import { logAndQuit, logLoginMessageAndQuit } from "../helpers/errors";
import { getInstances } from "./instances";

function isPubkey(key: string): boolean {
const pubKeyPattern = /^ssh-/;
Expand Down Expand Up @@ -55,6 +57,25 @@ export function registerSSH(program: Command) {
return;
}

if (options.add && name) {
logAndQuit("You can only add a key to all nodes at once");
}

if (name) {
const instances = await getInstances({ clusterId: undefined });
const instance = instances.find((instance) => instance.id === name);
if (!instance) {
logAndQuit(`Instance ${name} not found`);
}
if (instance.ip.split(":").length === 2) {
const [ip, port] = instance.ip.split(":");
await $`ssh -p ${port} ${options.user}@${ip}`;
} else {
await $`ssh ${options.user}@${instance.ip}`;
}
process.exit(0);
}

if (options.add) {
if (!options.user) {
logAndQuit(
Expand Down

0 comments on commit 65adbf6

Please sign in to comment.