Skip to content

Commit 1ec4b00

Browse files
committed
add prompt to sandbox names
1 parent de30c2d commit 1ec4b00

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

packages/cli/src/commands/sandbox/create.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { randomBytes } from "node:crypto";
22
import { createMcClient } from "@bunny.net/openapi-client";
3+
import prompts from "prompts";
34
import { resolveConfig, setSandbox } from "../../config/index.ts";
45
import { clientOptions } from "../../core/client-options.ts";
56
import { defineCommand } from "../../core/define-command.ts";
@@ -104,15 +105,15 @@ async function waitUntilActive(
104105
}
105106

106107
interface CreateArgs {
107-
name: string;
108+
name?: string;
108109
region: string;
109110
}
110111

111112
export const sandboxCreateCommand = defineCommand<CreateArgs>({
112113
command: "create [name]",
113114
describe: "Create and start a new sandbox.",
114115
examples: [
115-
["$0 sandbox create", "Create a sandbox with a generated name"],
116+
["$0 sandbox create", "Interactive: prompts for a sandbox name"],
116117
["$0 sandbox create my-sandbox", "Create a sandbox named my-sandbox"],
117118
[
118119
"$0 sandbox create my-sandbox --region NY",
@@ -124,7 +125,6 @@ export const sandboxCreateCommand = defineCommand<CreateArgs>({
124125
yargs
125126
.positional("name", {
126127
type: "string",
127-
default: "sandbox",
128128
describe: "Name for the sandbox",
129129
})
130130
.option("region", {
@@ -133,9 +133,24 @@ export const sandboxCreateCommand = defineCommand<CreateArgs>({
133133
describe: "Region ID to deploy the sandbox in (e.g. AMS, NY, LA)",
134134
}),
135135

136-
handler: async ({ profile, verbose, apiKey, name, region }) => {
136+
handler: async ({ profile, verbose, apiKey, name, region, output }) => {
137137
const config = resolveConfig(profile, apiKey, verbose);
138138
const client = createMcClient(clientOptions(config, verbose));
139+
140+
// JSON output stays non-interactive; the name must come from the positional.
141+
const interactive = output !== "json";
142+
143+
let sandboxName = name;
144+
if (!sandboxName && interactive) {
145+
const { value } = await prompts({
146+
type: "text",
147+
name: "value",
148+
message: "Sandbox name:",
149+
});
150+
sandboxName = value;
151+
}
152+
if (!sandboxName) throw new UserError("A sandbox name is required.");
153+
139154
const agentToken = generateToken();
140155

141156
const spin = spinner("Creating sandbox...");
@@ -145,7 +160,7 @@ export const sandboxCreateCommand = defineCommand<CreateArgs>({
145160
"/apps",
146161
{
147162
body: {
148-
name,
163+
name: sandboxName,
149164
runtimeType: "shared",
150165
autoScaling: { min: 1, max: 1 },
151166
regionSettings: {
@@ -215,11 +230,13 @@ export const sandboxCreateCommand = defineCommand<CreateArgs>({
215230
agent_token: agentToken,
216231
ssh_host: sshHost,
217232
};
218-
setSandbox(name, record);
233+
setSandbox(sandboxName, record);
219234

220-
logger.log(`Sandbox "${name}" is ready.`);
235+
logger.log(`Sandbox "${sandboxName}" is ready.`);
221236
logger.log(` App ID: ${appId}`);
222237
logger.log(` SSH: ${sshHost}`);
223-
logger.log(`\nRun commands with: bunny sandbox exec ${name} <command>`);
238+
logger.log(
239+
`\nRun commands with: bunny sandbox exec ${sandboxName} <command>`,
240+
);
224241
},
225242
});

0 commit comments

Comments
 (0)