Skip to content

Commit 65adbf6

Browse files
authored
Added sf ssh to ssh into instances, including if the instance has a port on its IP (#25)
* sketched out ssh * increased table width, added message about how to ssh * fixed formatting
1 parent 0bef873 commit 65adbf6

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/lib/instances.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async function listInstancesAction({
7676
} else {
7777
const table = new Table({
7878
head: tableHeaders,
79-
colWidths: [32, 10, 20],
79+
// colWidths: [32, 10, 20],
8080
});
8181

8282
table.push(
@@ -87,7 +87,11 @@ async function listInstancesAction({
8787
instance.status,
8888
]),
8989
);
90-
console.log(table.toString() + "\n\n");
90+
console.log(
91+
table.toString() +
92+
"\n" +
93+
"To ssh into an instance, run `sf ssh <instance-id>`.\n",
94+
);
9195
}
9296
}
9397

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

153157
// --
154158

155-
async function getInstances({
159+
export async function getInstances({
156160
clusterId,
157161
}: { clusterId?: string }): Promise<InstanceObject[]> {
158162
const loggedIn = await isLoggedIn();

src/lib/ssh.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { $ } from "bun";
12
import type { Command } from "commander";
23
import { apiClient } from "../apiClient";
34
import { isLoggedIn } from "../helpers/config";
45
import { logAndQuit, logLoginMessageAndQuit } from "../helpers/errors";
6+
import { getInstances } from "./instances";
57

68
function isPubkey(key: string): boolean {
79
const pubKeyPattern = /^ssh-/;
@@ -55,6 +57,25 @@ export function registerSSH(program: Command) {
5557
return;
5658
}
5759

60+
if (options.add && name) {
61+
logAndQuit("You can only add a key to all nodes at once");
62+
}
63+
64+
if (name) {
65+
const instances = await getInstances({ clusterId: undefined });
66+
const instance = instances.find((instance) => instance.id === name);
67+
if (!instance) {
68+
logAndQuit(`Instance ${name} not found`);
69+
}
70+
if (instance.ip.split(":").length === 2) {
71+
const [ip, port] = instance.ip.split(":");
72+
await $`ssh -p ${port} ${options.user}@${ip}`;
73+
} else {
74+
await $`ssh ${options.user}@${instance.ip}`;
75+
}
76+
process.exit(0);
77+
}
78+
5879
if (options.add) {
5980
if (!options.user) {
6081
logAndQuit(

0 commit comments

Comments
 (0)