From 65adbf6e16ecbac8e61c87310f0d34a70bcab441 Mon Sep 17 00:00:00 2001 From: Alex Gajewski Date: Fri, 20 Sep 2024 17:40:23 -0400 Subject: [PATCH] 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 --- src/lib/instances.ts | 10 +++++++--- src/lib/ssh.ts | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lib/instances.ts b/src/lib/instances.ts index 6b24fde..c63d448 100644 --- a/src/lib/instances.ts +++ b/src/lib/instances.ts @@ -76,7 +76,7 @@ async function listInstancesAction({ } else { const table = new Table({ head: tableHeaders, - colWidths: [32, 10, 20], + // colWidths: [32, 10, 20], }); table.push( @@ -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 `.\n", + ); } } @@ -152,7 +156,7 @@ const colorInstanceType = (instanceType: InstanceType) => // -- -async function getInstances({ +export async function getInstances({ clusterId, }: { clusterId?: string }): Promise { const loggedIn = await isLoggedIn(); diff --git a/src/lib/ssh.ts b/src/lib/ssh.ts index 7f05752..b99c5a3 100644 --- a/src/lib/ssh.ts +++ b/src/lib/ssh.ts @@ -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-/; @@ -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(