Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Added format option to CLI sandbox + templates list (#903)

Mish Ushakov2a346bfcd2a2
diff --git a/.changeset/modern-wombats-float.md b/.changeset/modern-wombats-float.md
new file mode 100644
index 000000000..e891ba05e
--- /dev/null
+++ b/.changeset/modern-wombats-float.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': patch
+---
+
+fixes sandboxes list
diff --git a/.changeset/popular-badgers-hear.md b/.changeset/popular-badgers-hear.md
new file mode 100644
index 000000000..55c73c5bd
--- /dev/null
+++ b/.changeset/popular-badgers-hear.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': minor
+---
+
+added json output format to template list and sandbox list commands
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 074c53b22..4dbeb3e5a 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -41,6 +41,7 @@
"lint": "eslint src",
"format": "prettier --write src",
"test": "pnpm build && cd testground/demo-basic && ../../dist/index.js template build",
+ "test:interactive": "pnpm build && ./dist/index.js",
"check-deps": "knip",
"update-deps": "ncu -u && pnpm i",
"generate-ref": "./scripts/generate_sdk_ref.sh"
diff --git a/packages/cli/src/commands/sandbox/list.ts b/packages/cli/src/commands/sandbox/list.ts
index 205e2f2e9..2861fb12e 100644
--- a/packages/cli/src/commands/sandbox/list.ts
+++ b/packages/cli/src/commands/sandbox/list.ts
@@ -5,6 +5,14 @@ import { components, Sandbox, SandboxInfo } from 'e2b'
import { ensureAPIKey } from 'src/api'
import { parseMetadata } from './utils'
+function getStateTitle(state?: components['schemas']['SandboxState'][]) {
+ if (state?.length === 1) {
+ if (state?.includes('running')) return 'Running sandboxes'
+ if (state?.includes('paused')) return 'Paused sandboxes'
+ }
+ return 'Sandboxes'
+}
+
export const listCommand = new commander.Command('list')
.description('list all sandboxes, by default it list only running ones')
.alias('ls')
@@ -19,80 +27,24 @@ export const listCommand = new commander.Command('list')
'limit the number of sandboxes returned',
(value) => parseInt(value)
)
+ .option('-f, --format <format>', 'output format, eg. json, table')
.action(async (options) => {
try {
const state = options.state || ['running']
+ const format = options.format || 'table'
const sandboxes = await listSandboxes({
limit: options.limit,
state,
metadataRaw: options.metadata,
})
- if (!sandboxes?.length) {
- console.log('No sandboxes found')
+ if (format === 'table') {
+ renderTable(sandboxes, state)
+ } else if (format === 'json') {
+ console.log(JSON.stringify(sandboxes, null, 2))
} else {
- const table = new tablePrinter.Table({
- title: 'Running sandboxes',
- columns: [
- { name: 'sandboxID', alignment: 'left', title: 'Sandbox ID' },
- {
- name: 'templateID',
- alignment: 'left',
- title: 'Template ID',
- maxLen: 20,
- },
- { name: 'alias', alignment: 'left', title: 'Alias' },
- { name: 'startedAt', alignment: 'left', title: 'Started at' },
- { name: 'endAt', alignment: 'left', title: 'End at' },
- { name: 'state', alignment: 'left', title: 'State' },
- { name: 'cpuCount', alignment: 'left', title: 'vCPUs' },
- { name: 'memoryMB', alignment: 'left', title: 'RAM MiB' },
- { name: 'metadata', alignment: 'left', title: 'Metadata' },
- ],
- disabledColumns: ['clientID'],
- rows: sandboxes
- .map((sandbox) => ({
- ...sandbox,
- sandboxID: sandbox.sandboxId,
- startedAt: new Date(sandbox.startedAt).toLocaleString(),
- endAt: new Date(sandbox.endAt).toLocaleString(),
- state:
- sandbox.state.charAt(0).toUpperCase() + sandbox.state.slice(1), // capitalize
- metadata: JSON.stringify(sandbox.metadata),
- }))
- .sort(
- (a, b) =>
- a.startedAt.localeCompare(b.startedAt) ||
- a.sandboxID.localeCompare(b.sandboxID)
- ),
- style: {
- headerTop: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- headerBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- tableBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- vertical: '',
- },
- colorMap: {
- orange: '\x1b[38;5;216m',
- },
- })
- table.printTable()
-
- process.stdout.write('\n')
+ console.error(`Unsupported output format: ${format}`)
+ process.exit(1)
}
} catch (err: any) {
console.error(err)
@@ -100,6 +52,78 @@ export const listCommand = new commander.Command('list')
}
})
+function renderTable(
+ sandboxes: SandboxInfo[],
+ state: components['schemas']['SandboxState'][]
+) {
+ if (!sandboxes?.length) {
+ console.log('No sandboxes found')
+ return
+ }
+
+ const table = new tablePrinter.Table({
+ title: getStateTitle(state),
+ columns: [
+ { name: 'sandboxId', alignment: 'left', title: 'Sandbox ID' },
+ {
+ name: 'templateId',
+ alignment: 'left',
+ title: 'Template ID',
+ maxLen: 20,
+ },
+ { name: 'name', alignment: 'left', title: 'Alias' },
+ { name: 'startedAt', alignment: 'left', title: 'Started at' },
+ { name: 'endAt', alignment: 'left', title: 'End at' },
+ { name: 'state', alignment: 'left', title: 'State' },
+ { name: 'cpuCount', alignment: 'left', title: 'vCPUs' },
+ { name: 'memoryMB', alignment: 'left', title: 'RAM MiB' },
+ { name: 'envdVersion', alignment: 'left', title: 'Envd version' },
+ { name: 'metadata', alignment: 'left', title: 'Metadata' },
+ ],
+ disabledColumns: ['clientID'],
+ rows: sandboxes
+ .map((sandbox) => ({
+ ...sandbox,
+ startedAt: new Date(sandbox.startedAt).toLocaleString(),
+ endAt: new Date(sandbox.endAt).toLocaleString(),
+ state: sandbox.state.charAt(0).toUpperCase() + sandbox.state.slice(1), // capitalize
+ metadata: JSON.stringify(sandbox.metadata),
+ }))
+ .sort(
+ (a, b) =>
+ a.startedAt.localeCompare(b.startedAt) ||
+ a.sandboxId.localeCompare(b.sandboxId)
+ ),
+ style: {
+ headerTop: {
+ left: '',
+ right: '',
+ mid: '',
+ other: '',
+ },
+ headerBottom: {
+ left: '',
+ right: '',
+ mid: '',
+ other: '',
+ },
+ tableBottom: {
+ left: '',
+ right: '',
+ mid: '',
+ other: '',
+ },
+ vertical: '',
+ },
+ colorMap: {
+ orange: '\x1b[38;5;216m',
+ },
+ })
+ table.printTable()
+
+ process.stdout.write('\n')
+}
+
type ListSandboxesOptions = {
limit?: number
state?: components['schemas']['SandboxState'][]
diff --git a/packages/cli/src/commands/template/list.ts b/packages/cli/src/commands/template/list.ts
index c48c9e796..d41acd2ec 100644
--- a/packages/cli/src/commands/template/list.ts
+++ b/packages/cli/src/commands/template/list.ts
@@ -13,8 +13,10 @@ export const listCommand = new commander.Command('list')
.description('list sandbox templates')
.alias('ls')
.addOption(teamOption)
- .action(async (opts: { team: string }) => {
+ .option('-f, --format <format>', 'output format, eg. json, table')
+ .action(async (opts: { team: string; format: string }) => {
try {
+ const format = opts.format || 'table'
const userConfig = getUserConfig()
ensureAccessToken()
process.stdout.write('\n')
@@ -27,69 +29,13 @@ export const listCommand = new commander.Command('list')
sortTemplatesAliases(template.aliases)
}
- if (!templates?.length) {
- console.log('No templates found.')
+ if (format === 'table') {
+ renderTable(templates)
+ } else if (format === 'json') {
+ console.log(JSON.stringify(templates, null, 2))
} else {
- const table = new tablePrinter.Table({
- title: 'Sandbox templates',
- columns: [
- { name: 'visibility', alignment: 'left', title: 'Access' },
- { name: 'templateID', alignment: 'left', title: 'Template ID' },
- {
- name: 'aliases',
- alignment: 'left',
- title: 'Template Name',
- color: 'orange',
- maxLen: 20,
- },
- { name: 'cpuCount', alignment: 'right', title: 'vCPUs' },
- { name: 'memoryMB', alignment: 'right', title: 'RAM MiB' },
- { name: 'createdBy', alignment: 'right', title: 'Created by' },
- { name: 'createdAt', alignment: 'right', title: 'Created at' },
- ],
- disabledColumns: [
- 'public',
- 'buildID',
- 'buildCount',
- 'lastSpawnedAt',
- 'spawnCount',
- 'updatedAt',
- ],
- rows: templates.map((template) => ({
- ...template,
- visibility: template.public ? 'Public' : 'Private',
- aliases: listAliases(template.aliases),
- createdBy: template.createdBy?.email,
- createdAt: new Date(template.createdAt).toLocaleDateString(),
- })),
- style: {
- headerTop: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- headerBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- tableBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- vertical: '',
- },
- colorMap: {
- orange: '\x1b[38;5;216m',
- },
- })
- table.printTable()
-
- process.stdout.write('\n')
+ console.error(`Unsupported output format: ${format}`)
+ process.exit(1)
}
} catch (err: any) {
console.error(err)
@@ -97,6 +43,76 @@ export const listCommand = new commander.Command('list')
}
})
+function renderTable(templates: e2b.components['schemas']['Template'][]) {
+ if (!templates?.length) {
+ console.log('No templates found.')
+ return
+ }
+
+ const table = new tablePrinter.Table({
+ title: 'Sandbox templates',
+ columns: [
+ { name: 'visibility', alignment: 'left', title: 'Access' },
+ { name: 'templateID', alignment: 'left', title: 'Template ID' },
+ {
+ name: 'aliases',
+ alignment: 'left',
+ title: 'Template Name',
+ color: 'orange',
+ maxLen: 20,
+ },
+ { name: 'cpuCount', alignment: 'right', title: 'vCPUs' },
+ { name: 'memoryMB', alignment: 'right', title: 'RAM MiB' },
+ { name: 'createdBy', alignment: 'right', title: 'Created by' },
+ { name: 'createdAt', alignment: 'right', title: 'Created at' },
+ { name: 'diskSizeMB', alignment: 'right', title: 'Disk size MiB' },
+ { name: 'envdVersion', alignment: 'right', title: 'Envd version' },
+ ],
+ disabledColumns: [
+ 'public',
+ 'buildID',
+ 'buildCount',
+ 'lastSpawnedAt',
+ 'spawnCount',
+ 'updatedAt',
+ ],
+ rows: templates.map((template) => ({
+ ...template,
+ visibility: template.public ? 'Public' : 'Private',
+ aliases: listAliases(template.aliases),
+ createdBy: template.createdBy?.email,
+ createdAt: new Date(template.createdAt).toLocaleDateString(),
+ })),
+ style: {
+ headerTop: {
+ left: '',
+ right: '',
+ mid: '',
+ other: '',
+ },
+ headerBottom: {
+ left: '',
+ right: '',
+ mid: '',
+ other: '',
+ },
+ tableBottom: {
+ left: '',
+ right: '',
+ mid: '',
+ other: '',
+ },
+ vertical: '',
+ },
+ colorMap: {
+ orange: '\x1b[38;5;216m',
+ },
+ })
+ table.printTable()
+
+ process.stdout.write('\n')
+}
+
export async function listSandboxTemplates({
teamID,
}: {