Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/commands/devbox/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { spawn } from "child_process";
import { getClient } from "../../utils/client.js";
import { cliStatus } from "../../utils/cliStatus.js";
import { output, outputError } from "../../utils/output.js";
import { processUtils } from "../../utils/processUtils.js";
import {
Expand Down Expand Up @@ -36,11 +37,14 @@ export async function sshDevbox(devboxId: string, options: SSHOptions = {}) {

// Wait for devbox to be ready unless --no-wait is specified
if (!options.noWait) {
console.error(`Waiting for devbox ${devboxId} to be ready...`);
if (!options.configOnly) {
cliStatus(`Waiting for devbox ${devboxId} to be ready...`);
}
const isReady = await waitForReady(
devboxId,
options.timeout || 180,
options.pollInterval || 3,
{ quiet: options.configOnly },
);
if (!isReady) {
outputError(`Devbox ${devboxId} is not ready. Please try again later.`);
Expand All @@ -64,7 +68,12 @@ export async function sshDevbox(devboxId: string, options: SSHOptions = {}) {
sshInfo!.keyfilePath,
sshInfo!.url,
);
output({ config }, { format: options.output, defaultFormat: "text" });
const format = options.output ?? "text";
if (format === "text") {
console.log(config);
} else {
output({ config }, { format, defaultFormat: "text" });
}
return;
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils/cliStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Status / progress lines for the CLI. Uses stderr so stdout stays free for
* data users redirect or pipe (e.g. SSH config snippets, JSON).
*
* Prefer this over console.error for non-failure messages—console.error reads
* like a runtime error to humans and tools.
*/
export function cliStatus(message: string): void {
process.stderr.write(`${message}\n`);
}
33 changes: 22 additions & 11 deletions src/utils/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { writeFile, mkdir, chmod } from "fs/promises";
import { join } from "path";
import { homedir } from "os";
import { getClient } from "./client.js";
import { cliStatus } from "./cliStatus.js";
import { processUtils } from "./processUtils.js";

const execAsync = promisify(exec);
Expand Down Expand Up @@ -71,14 +72,21 @@ export async function getSSHKey(devboxId: string): Promise<SSHKeyInfo | null> {
}
}

export interface WaitForReadyOptions {
/** If true, omit periodic poll lines (e.g. when stdout must stay machine-clean). */
quiet?: boolean;
}

/**
* Wait for a devbox to be ready
*/
export async function waitForReady(
devboxId: string,
timeoutSeconds: number = 180,
pollIntervalSeconds: number = 3,
waitOptions?: WaitForReadyOptions,
): Promise<boolean> {
const quiet = waitOptions?.quiet ?? false;
const startTime = Date.now();
const client = getClient();

Expand All @@ -89,25 +97,26 @@ export async function waitForReady(
const remaining = timeoutSeconds - elapsed;

if (devbox.status === "running") {
console.log(`Devbox ${devboxId} is ready!`);
return true;
} else if (devbox.status === "failure") {
console.log(
cliStatus(
`Devbox ${devboxId} failed to start (status: ${devbox.status})`,
);
return false;
} else if (["shutdown", "suspended"].includes(devbox.status)) {
console.log(
cliStatus(
`Devbox ${devboxId} is not running (status: ${devbox.status})`,
);
return false;
} else {
console.log(
`Devbox ${devboxId} is still ${devbox.status}... (elapsed: ${elapsed.toFixed(0)}s, remaining: ${remaining.toFixed(0)}s)`,
);
if (!quiet) {
cliStatus(
`Devbox ${devboxId} is still ${devbox.status}... (elapsed: ${elapsed.toFixed(0)}s, remaining: ${remaining.toFixed(0)}s)`,
);
}

if (elapsed >= timeoutSeconds) {
console.log(
cliStatus(
`Timeout waiting for devbox ${devboxId} to be ready after ${timeoutSeconds} seconds`,
);
return false;
Expand All @@ -120,15 +129,17 @@ export async function waitForReady(
} catch (error) {
const elapsed = (Date.now() - startTime) / 1000;
if (elapsed >= timeoutSeconds) {
console.log(
cliStatus(
`Timeout waiting for devbox ${devboxId} to be ready after ${timeoutSeconds} seconds (error: ${error})`,
);
return false;
}

console.log(
`Error checking devbox status: ${error}, retrying in ${pollIntervalSeconds} seconds...`,
);
if (!quiet) {
cliStatus(
`Error checking devbox status: ${error}, retrying in ${pollIntervalSeconds} seconds...`,
);
}
await new Promise((resolve) =>
setTimeout(resolve, pollIntervalSeconds * 1000),
);
Expand Down
Loading