Skip to content

Commit

Permalink
add support for asyncd
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaque committed Oct 10, 2024
1 parent 1d535ae commit 271a9d5
Show file tree
Hide file tree
Showing 7 changed files with 2,662 additions and 2,626 deletions.
Binary file modified bun.lockb
Binary file not shown.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@inquirer/prompts": "^5.1.2",
"axios": "^1.7.2",
"boxen": "^8.0.1",
"chalk": "^5.3.0",
"chrono-node": "^2.7.6",
"cli-table3": "^0.6.5",
Expand All @@ -22,11 +23,13 @@
"node-fetch": "^3.3.2",
"openapi-fetch": "^0.11.1",
"ora": "^8.1.0",
"parse-duration": "^1.1.0"
"parse-duration": "^1.1.0",
"semver": "^7.6.3"
},
"devDependencies": {
"@biomejs/biome": "^1.8.2",
"@types/bun": "latest"
"@types/bun": "latest",
"@types/semver": "^7.5.8"
},
"peerDependencies": {
"typescript": "^5.6.2"
Expand Down
45 changes: 45 additions & 0 deletions src/checkVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import boxen from "boxen";
import chalk from "chalk";
import { version } from "../package.json";
import semver from "semver";

async function checkProductionCLIVersion() {
try {
const response = await fetch(
"https://raw.githubusercontent.com/sfcompute/cli/refs/heads/main/package.json",
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const latestVersion = data.version;
return latestVersion;
} catch (error) {
console.error("Failed to check for latest CLI version:", error);
return null;
}
}

export async function checkVersion() {
const latestVersion = await checkProductionCLIVersion();
if (latestVersion && version !== latestVersion) {
const isOutdated = semver.lt(version, latestVersion);
if (isOutdated) {
const message = `
Please update your CLI.
Your version: ${version}
Latest version: ${latestVersion}
Run 'sf upgrade' to update to the latest version
`;
console.log(
boxen(chalk.yellow(message), {
padding: 1,
borderColor: "yellow",
borderStyle: "round",
}),
);
}
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import { registerSSH } from "./lib/ssh";
import { registerTokens } from "./lib/tokens";
import { registerDown, registerUp } from "./lib/updown";
import { registerUpgrade } from "./lib/upgrade";
import { checkVersion } from "./checkVersion";

const program = new Command();

await checkVersion();

program
.name("sf")
.description("The San Francisco Compute command line tool.")
Expand Down
14 changes: 10 additions & 4 deletions src/lib/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ interface InstanceObject {
object: "instance";
id: string;
type: InstanceType;
ip: string;
public_ip: string;
private_ip: string;
status: string;
ssh_port: number | undefined;
}

async function listInstancesAction({
Expand All @@ -60,6 +62,7 @@ async function listInstancesAction({
chalk.gray("Instance Id"),
chalk.gray("Type"),
chalk.gray("IP Address"),
chalk.gray("SSH Port"),
chalk.gray("Status"),
];

Expand All @@ -83,7 +86,8 @@ async function listInstancesAction({
...instances.map((instance) => [
instance.id,
colorInstanceType(instance.type),
instance.ip,
instance.public_ip,
instance.ssh_port,
instance.status,
]),
);
Expand All @@ -110,7 +114,7 @@ const sortInstancesByTypeAndIp = () => {
const priorityB =
InstanceTypeSortPriority[b.type] || Number.MAX_SAFE_INTEGER;
if (priorityA === priorityB) {
return compareIPs(a.ip, b.ip); // secondary sort on ips
return compareIPs(a.public_ip, b.public_ip); // secondary sort on public ips
}
return priorityA - priorityB;
};
Expand Down Expand Up @@ -189,7 +193,9 @@ export async function getInstances({
object: instance.object,
id: instance.id,
type: instance.type as InstanceType,
ip: instance.ip,
public_ip: instance.public_ip,
private_ip: instance.private_ip,
status: instance.status,
ssh_port: instance.ssh_port,
}));
}
14 changes: 10 additions & 4 deletions src/lib/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
unreachable,
} from "../helpers/errors";
import { getInstances } from "./instances";
import chalk from "chalk";

// openssh-client doesn't check $HOME while homedir() does. This function is to
// make it easy to fix if it causes issues.
Expand Down Expand Up @@ -315,19 +316,24 @@ export function registerSSH(program: Command) {
if (!instance) {
logAndQuit(`Instance ${name} not found`);
}
if (instance.ip.split(":").length === 2) {
const [ip, port] = instance.ip.split(":");
if (instance.public_ip.split(":").length === 2 || instance.ssh_port) {
const [ip, port] = instance.public_ip.split(":");

const sshPort = instance.ssh_port?.toString() || port || "22";

console.log(chalk.dim(`ssh -p ${sshPort} ${options.user}@${ip}`));
procResult = Bun.spawnSync(
["ssh", "-p", port, util.format("%s@%s", options.user, ip)],
["ssh", "-p", sshPort, util.format("%s@%s", options.user, ip)],
{
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
},
);
} else {
console.log(chalk.dim(`ssh ${options.user}@${instance.public_ip}`));
procResult = Bun.spawnSync(
["ssh", util.format("%s@%s", options.user, instance.ip)],
["ssh", util.format("%s@%s", options.user, instance.public_ip)],
{
stdin: "inherit",
stdout: "inherit",
Expand Down
Loading

0 comments on commit 271a9d5

Please sign in to comment.