Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
feat (cli): adds `info` command to get sandbox info (#1214)
Matt Brockmandcaca733e457
diff --git a/.changeset/fine-cows-chew.md b/.changeset/fine-cows-chew.mdnew file mode 100644index 000000000..cef7a54ae--- /dev/null+++ b/.changeset/fine-cows-chew.md@@ -0,0 +1,5 @@+---+'@e2b/cli': minor+---++cli supports info command which is the equivilent of the getInfodiff --git a/packages/cli/src/commands/sandbox/index.ts b/packages/cli/src/commands/sandbox/index.tsindex ac537c0d2..7f634c4ba 100644--- a/packages/cli/src/commands/sandbox/index.ts+++ b/packages/cli/src/commands/sandbox/index.ts@@ -1,6 +1,7 @@import * as commander from 'commander'import { connectCommand } from './connect'+import { infoCommand } from './info'import { listCommand } from './list'import { killCommand } from './kill'import { pauseCommand } from './pause'@@ -14,6 +15,7 @@ export const sandboxCommand = new commander.Command('sandbox').description('work with sandboxes').alias('sbx').addCommand(connectCommand)+ .addCommand(infoCommand).addCommand(listCommand).addCommand(killCommand).addCommand(pauseCommand)diff --git a/packages/cli/src/commands/sandbox/info.ts b/packages/cli/src/commands/sandbox/info.tsnew file mode 100644index 000000000..85395a3b0--- /dev/null+++ b/packages/cli/src/commands/sandbox/info.ts@@ -0,0 +1,114 @@+import * as commander from 'commander'+import { Sandbox } from 'e2b'++import { ensureAPIKey } from 'src/api'+import { asBold } from 'src/utils/format'++const fieldLabels: Partial<Record<string, string>> = {+ sandboxId: 'Sandbox ID',+ templateId: 'Template ID',+ name: 'Alias',+ startedAt: 'Started at',+ endAt: 'End at',+ state: 'State',+ cpuCount: 'vCPUs',+ memoryMB: 'RAM MiB',+ envdVersion: 'Envd version',+ allowInternetAccess: 'Internet access',+ lifecycle: 'Lifecycle',+ network: 'Network',+ sandboxDomain: 'Sandbox domain',+ metadata: 'Metadata',+}++const fieldOrder = [+ 'sandboxId',+ 'templateId',+ 'name',+ 'state',+ 'startedAt',+ 'endAt',+ 'cpuCount',+ 'memoryMB',+ 'envdVersion',+ 'allowInternetAccess',+ 'lifecycle',+ 'network',+ 'sandboxDomain',+ 'metadata',+]++export const infoCommand = new commander.Command('info')+ .description('show information for a sandbox')+ .argument(+ '<sandboxID>',+ `show information for sandbox specified by ${asBold('<sandboxID>')}`+ )+ .alias('in')+ .option('-f, --format <format>', 'output format, eg. json, pretty')+ .action(async (sandboxID: string, options: { format?: string }) => {+ try {+ const format = options.format || 'pretty'+ const apiKey = ensureAPIKey()+ const info = await Sandbox.getFullInfo(sandboxID, { apiKey })+ delete info.envdAccessToken++ if (format === 'pretty') {+ renderPrettyInfo(info as unknown as Record<string, unknown>)+ } else if (format === 'json') {+ console.log(JSON.stringify(info, null, 2))+ } else {+ console.error(`Unsupported output format: ${format}`)+ process.exit(1)+ }+ } catch (err: any) {+ console.error(err)+ process.exit(1)+ }+ })++function renderPrettyInfo(info: Record<string, unknown>) {+ console.log(+ `\nSandbox info for ${asBold(String(info.sandboxId ?? 'unknown'))}:`+ )++ const orderedKeys = [+ ...fieldOrder.filter((key) => key in info),+ ...Object.keys(info).filter((key) => !fieldOrder.includes(key)),+ ]++ for (const key of orderedKeys) {+ const value = info[key]+ if (value === undefined) {+ continue+ }++ const label = fieldLabels[key] ?? key+ const formattedValue = formatValue(value)++ if (formattedValue.includes('\n')) {+ const indentedValue = formattedValue+ .split('\n')+ .map((line) => ` ${line}`)+ .join('\n')+ console.log(`${asBold(label)}:\n${indentedValue}`)+ continue+ }++ console.log(`${asBold(label)}: ${formattedValue}`)+ }++ process.stdout.write('\n')+}++function formatValue(value: unknown): string {+ if (value instanceof Date) {+ return value.toLocaleString()+ }++ if (Array.isArray(value) || (typeof value === 'object' && value !== null)) {+ return JSON.stringify(value, null, 2)+ }++ return String(value)+}diff --git a/packages/cli/tests/commands/sandbox/backend_integration.test.ts b/packages/cli/tests/commands/sandbox/backend_integration.test.tsindex 49642598a..0e46225b2 100644--- a/packages/cli/tests/commands/sandbox/backend_integration.test.ts+++ b/packages/cli/tests/commands/sandbox/backend_integration.test.ts@@ -63,6 +63,29 @@ describe('sandbox cli backend integration', () => {expect(sandboxExistsInList(listResult.stdout, sandbox.sandboxId)).toBe(true)})+ testIf(+ 'info shows the sandbox details',+ { timeout: perTestTimeoutMs },+ async () => {+ const infoResult = runCli([+ 'sandbox',+ 'info',+ sandbox.sandboxId,+ '--format',+ 'json',+ ])+ expect(infoResult.status).toBe(0)++ const info = JSON.parse(bufferToText(infoResult.stdout)) as {+ sandboxId?: string+ state?: string+ }++ expect(info.sandboxId).toBe(sandbox.sandboxId)+ expect(info.state).toBe('running')+ }+ )+testIf('exec runs a command without piped stdin',{ timeout: perTestTimeoutMs },