Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Improve CLI
Jakub Novak2feef49e3441
diff --git a/packages/cli/src/commands/template/build.ts b/packages/cli/src/commands/template/build.tsindex 95c84d6a2..67dbf0cd4 100644--- a/packages/cli/src/commands/template/build.ts+++ b/packages/cli/src/commands/template/build.ts@@ -54,7 +54,7 @@ export const buildCommand = new commander.Command('build')).option('-n, --name <name>',- 'Specify name of sandbox template. You can use the name to start the sandbox in the SDK',+ 'Specify name of sandbox template. You can use the name to start the sandbox in the SDK. The name must be lowercase and contain only letters, numbers, dashes and underscores.',).alias('bd').action(@@ -87,6 +87,7 @@ export const buildCommand = new commander.Command('build')`Found sandbox template ${asFormattedSandboxTemplate({envID: config.id,+ aliases: config.name ? [config.name] : undefined,},relativeConfigPath,)}`,@@ -107,6 +108,27 @@ export const buildCommand = new commander.Command('build')return}+ if (opts.name && config?.name) {+ console.error(+ `You cannot specify name ${asLocal(opts.name)} because sandbox template ${asFormattedSandboxTemplate(+ {+ envID: config.id,+ aliases: config.name ? [config.name] : undefined,+ },+ relativeConfigPath,+ )} already has name ${asLocal(config.name)}`,+ )+ process.exit(1)+ }++ const name = config?.name || opts.name?.trim()+ if (name && !/[a-z0-9-_]+/.test(name)) {+ console.error(+ `Name ${asLocal(name)} is not valid. Name can only contain lowercase letters, numbers, dashes and underscores.`,+ )+ process.exit(1)+ }+console.log(`Preparing sandbox template building (${filePaths.length} files in Docker build context).`,)@@ -142,6 +164,10 @@ export const buildCommand = new commander.Command('build'))body.append('buildContext', blob, 'env.tar.gz.e2b')+ if (name) {+ body.append('name', name)+ }+const build = await buildTemplate(accessToken, body, envID)console.log(@@ -158,6 +184,7 @@ export const buildCommand = new commander.Command('build'){id: build.envID,dockerfile: dockerfileRelativePath,+ name: name,},true,)@@ -218,7 +245,7 @@ async function waitForBuildFinish(case 'ready':console.log(`\n✅ Building sandbox template ${asFormattedSandboxTemplate(- template.data,+ { aliases: [name], ...template.data },)} finished.\nNow you can start creating your sandboxes from this template. You can find more here:${asPrimary('https://e2b.dev/docs/guide/custom-sandbox')}, section ${asBold('Spawn and control your sandbox')}\n`,@@ -231,7 +258,7 @@ async function waitForBuildFinish()throw new Error(`\n❌ Building sandbox template ${asFormattedSandboxTemplate(- template.data,+ { aliases: [name], ...template.data },)} failed.\nCheck the logs above for more details or contact us ${asPrimary('(https://e2b.dev/docs/getting-help)')} to get help.\n`,)}diff --git a/packages/cli/src/commands/template/list.ts b/packages/cli/src/commands/template/list.tsindex dd4dc022e..752aca26f 100644--- a/packages/cli/src/commands/template/list.ts+++ b/packages/cli/src/commands/template/list.ts@@ -4,6 +4,7 @@ import * as commander from 'commander'import * as e2b from '@e2b/sdk'import { ensureAccessToken } from 'src/api'+import { listAliases } from '../../utils/format'const listTemplates = e2b.withAccessToken(e2b.api.path('/envs').method('get').create(),@@ -31,17 +32,18 @@ export const listCommand = new commander.Command('list')const table = new tablePrinter.Table({title: 'Sandbox templates',columns: [- { name: 'envID', alignment: 'left' , title: "EnvID"},+ { name: 'envID', alignment: 'left', title: 'EnvID' },{ name: 'aliases', alignment: 'left', title: 'Name', color: 'blue' },],disabledColumns: ['public', 'buildID'],- rows: templates.map((template) => ({...template, aliases: template.aliases ? template.aliases.join(" | ") : undefined})),- sort: (row1, row2) => row1.envID.localeCompare(row2.envID)- })+ rows: templates.map((template) => ({ ...template, aliases: listAliases(template.aliases) })),+ sort: (row1, row2) => row1.envID.localeCompare(row2.envID),+ })table.printTable()- process.stdout.write('\n')- }} catch (err: any) {+ process.stdout.write('\n')+ }+ } catch (err: any) {console.error(err)process.exit(1)}diff --git a/packages/cli/src/commands/template/shell.ts b/packages/cli/src/commands/template/shell.tsindex f29dfd178..701525f5e 100644--- a/packages/cli/src/commands/template/shell.ts+++ b/packages/cli/src/commands/template/shell.ts@@ -4,20 +4,59 @@ import * as commander from 'commander'import { ensureAPIKey } from 'src/api'import { spawnConnectedTerminal } from 'src/terminal'import { asBold, asFormattedSandboxTemplate, asPrimary } from 'src/utils/format'+import { getRoot } from '../../utils/filesystem'+import { getConfigPath, loadConfig } from '../../config'+import fs from 'fs'+import { pathOption } from '../../options'+import path from 'path'export const shellCommand = new commander.Command('shell').description('Connect to the terminal in the sandbox').argument('<id>', `Connect to sandbox specified by ${asPrimary('template id')}`)+ .option(+ '-n, --name <name>',+ 'Specify name of sandbox template. You can use the name to start the sandbox in the SDK.',+ )+ .addOption(pathOption).alias('sh')- .action(async (id: string) => {+ .action(async (id: string | undefined, opts: {+ name?: string;+ path?: string;+ }) => {try {const apiKey = ensureAPIKey()+ let envID = id- const template: Pick<e2b.components['schemas']['Environment'], 'envID'> =- {- envID: id,++ const root = getRoot(opts.path)+ const configPath = getConfigPath(root)++ const config = fs.existsSync(configPath)+ ? await loadConfig(configPath)+ : undefined+ const relativeConfigPath = path.relative(root, configPath)++ if (config) {+ console.log(+ `Found sandbox template ${asFormattedSandboxTemplate(+ {+ envID: config.id,+ aliases: config.name ? [config.name] : undefined,+ },+ relativeConfigPath,+ )}`,+ )+ envID = config.id}+ if (!envID) {+ console.error(+ `You need to specify sandbox template ID or path to sandbox template config`,+ )+ process.exit(1)+ }+ const template: Pick<e2b.components['schemas']['Environment'], 'envID'> = { envID: envID }+await connectSandbox({ apiKey, template: template })// We explicitly call exit because the sandbox is keeping the program alive.// We also don't want to call sandbox.close because that would disconnect other users from the edit session.@@ -29,9 +68,9 @@ export const shellCommand = new commander.Command('shell')})async function connectSandbox({- apiKey,- template,-}: {+ apiKey,+ template,+ }: {apiKey: string;template: Pick<e2b.components['schemas']['Environment'], 'envID'>;}) {diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex ddcd0cfe2..87a0921fb 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -9,7 +9,7 @@ export const pkg = packageJSONupdateNotifier.default({pkg,- updateCheckInterval: 1000 * 60 * 60 * 8 // 8 hours+ updateCheckInterval: 1000 * 60 * 60 * 8, // 8 hours}).notify()programdiff --git a/packages/cli/src/utils/format.ts b/packages/cli/src/utils/format.tsindex 7f2de9a80..209174a67 100644--- a/packages/cli/src/utils/format.ts+++ b/packages/cli/src/utils/format.ts@@ -4,15 +4,15 @@ import * as e2b from '@e2b/sdk'import { cwdRelative } from './filesystem'export function asFormattedSandboxTemplate(- template: Pick<e2b.components['schemas']['Environment'], 'envID'>,+ template: Pick<e2b.components['schemas']['Environment'], 'envID' | 'aliases'>,configLocalPath?: string,) {- const id = asBold(template.envID)+ const name = asBold(listAliases(template.aliases) ?? template.envID)const configPath = configLocalPath? asDim(' <-> ') + asLocalRelative(configLocalPath): ''- return `${id}${configPath}`+ return `${name}${configPath}`}export function asFormattedError(text: string | undefined, err?: any) {@@ -52,4 +52,8 @@ export function asBuildLogs(content: string) {export function asHeadline(content: string) {return chalk.default.underline(asPrimary(asBold(content)))+}+export function listAliases(aliases: string[] | undefined) {+ if (!aliases) return undefined+ return aliases.join(' | ')}\ No newline at end of file