Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Improve examples priting in CLi

Tomas Valenta287ea971b180
diff --git a/.changeset/tidy-buses-lay.md b/.changeset/tidy-buses-lay.md
new file mode 100644
index 000000000..a3547600f
--- /dev/null
+++ b/.changeset/tidy-buses-lay.md
@@ -0,0 +1,5 @@
+---
+"@e2b/cli": patch
+---
+
+Improve examples printing
diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts
index 13b301ace..d3d999a7e 100644
--- a/packages/cli/src/commands/build.ts
+++ b/packages/cli/src/commands/build.ts
@@ -3,6 +3,7 @@ import * as fs from 'fs'
import * as path from 'path'
import * as e2b from '@e2b/sdk'
import * as stripAnsi from 'strip-ansi'
+import * as boxen from 'boxen'
import { wait } from 'src/utils/wait'
import { ensureAccessToken } from 'src/api'
@@ -16,6 +17,8 @@ import {
asPrimary,
asPython,
asTypescript,
+ withDelimiter,
+ withMargin,
} from 'src/utils/format'
import { pathOption } from 'src/options'
import { createBlobFromFiles } from 'src/docker/archive'
@@ -181,7 +184,6 @@ export const buildCommand = new commander.Command('build')
},
true,
)
- console.log(`Created config ${asLocalRelative(relativeConfigPath)}`)
} catch (err: any) {
console.error(err)
process.exit(1)
@@ -236,45 +238,50 @@ async function waitForBuildFinish(
process.stdout.write(asBuildLogs(stripAnsi.default(line))),
)
break
- case 'ready':
- console.log(
- `\n✅ Building sandbox template ${asFormattedSandboxTemplate(
- { aliases, ...template.data },
- )} finished.\n
- Now 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.')}
+ case 'ready': {
+
+ const pythonExample = asPython(`from e2b import Sandbox
+
+# Start sandbox
+sandbox = Sandbox(id="${aliases?.length ? aliases[0] : template.data.envID}")
+
+# Interact with sandbox. Learn more here:
+# https://e2b.dev/docs/sandbox/overview
- | ${asBold('Python SDK')} |
- ==============
- ${asPython(`from e2b import Sandbox
+# Close sandbox once done
+sandbox.close()`)
- # Start sandbox
- sandbox = Sandbox(id="${aliases?.length ? aliases[0] : template.data.envID}")
+ const typescriptExample = asTypescript(`import { Sandbox } from '@e2b/sdk'
- # Interact with sandbox. Learn more here:
- # https://e2b.dev/docs/sandbox/overview
+// Start sandbox
+const sandbox = await Sandbox.create({ id: '${aliases?.length ? aliases[0] : template.data.envID}' })
- # Close sandbox once done
- sandbox.close()`)
- }
+// Interact with sandbox. Learn more here:
+// https://e2b.dev/docs/sandbox/overview
- | ${asBold('JS SDK')} |
- ==========
- ${asTypescript(`import { Sandbox } from '@e2b/sdk'
+// Close sandbox once done
+await sandbox.close()`)
- // Start sandbox
- const sandbox = await Sandbox.create({ id: '${aliases?.length ? aliases[0] : template.data.envID}' })
- // Interact with sandbox. Learn more here:
- // https://e2b.dev/docs/sandbox/overview
+ const examplesMessage = `You can use E2B Python or JS SDK to spawn sandboxes now.
+Find more here - ${asPrimary('https://e2b.dev/docs/guide/custom-sandbox')} in ${asBold('Spawn and control your sandbox')} section.`
- // Close sandbox once done
- await sandbox.close()`)
- }
- `,
+ const exampleUsage = `${withMargin(examplesMessage)}\n${withDelimiter(pythonExample, 'Python SDK')}${withDelimiter(typescriptExample, 'JS SDK', true)}`
+
+ console.log(
+ `\n✅ Building sandbox template ${asFormattedSandboxTemplate(
+ {
+ aliases, ...template.data
+ },
+ )} finished.\n${boxen.default(exampleUsage, {
+ float: 'left',
+ padding: 0,
+ margin: 1,
+ title: 'Usage examples',
+ })}`,
)
break
-
+ }
case 'error':
template.data.logs.forEach((line) =>
process.stdout.write(asBuildLogs(stripAnsi.default(line))),
diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts
index 8d94549bf..457ceb38a 100644
--- a/packages/cli/src/commands/index.ts
+++ b/packages/cli/src/commands/index.ts
@@ -9,6 +9,7 @@ import { initCommand } from './init'
import { asPrimary } from 'src/utils/format'
+
export const program = new commander.Command()
.description(`Create sandbox templates from Dockerfiles by running ${asPrimary('e2b build')} then use our SDKs to create sandboxes from these templates.
@@ -19,4 +20,4 @@ Visit ${asPrimary('E2B docs (https://e2b.dev/docs)')} to learn how to create san
.addCommand(buildCommand)
.addCommand(listCommand)
.addCommand(shellCommand)
- .addCommand(initCommand)
\ No newline at end of file
+ .addCommand(initCommand)
diff --git a/packages/cli/src/utils/format.ts b/packages/cli/src/utils/format.ts
index 83900231c..0d4b304bf 100644
--- a/packages/cli/src/utils/format.ts
+++ b/packages/cli/src/utils/format.ts
@@ -1,6 +1,7 @@
import * as chalk from 'chalk'
import * as e2b from '@e2b/sdk'
import * as highlight from 'cli-highlight'
+import * as boxen from 'boxen'
import { cwdRelative } from './filesystem'
@@ -67,3 +68,52 @@ export function asTypescript(code: string) {
export function asPython(code: string) {
return highlight.default(code, { language: 'python', ignoreIllegals: true })
}
+
+const borderStyle = {
+ topLeft: '',
+ topRight: '',
+ bottomLeft: '',
+ bottomRight: '',
+ top: '',
+ bottom: '',
+ left: '',
+ right: ''
+} as const
+
+const horizontalPadding = 2
+const verticalPadding = 1
+
+export function withDelimiter(content: string, title: string, isLast?: boolean) {
+ return boxen.default(content, {
+ borderStyle: {
+ ...borderStyle,
+ top: '─',
+ },
+ titleAlignment: 'center',
+ float: 'left',
+ title: title ? asBold(title) : undefined,
+ margin: 0,
+ height: 0,
+ fullscreen: w => [w, 0],
+ padding: {
+ bottom: isLast ? 0 : verticalPadding,
+ left: horizontalPadding,
+ right: horizontalPadding,
+ top: verticalPadding,
+ },
+ })
+}
+
+export function withMargin(content: string) {
+ return boxen.default(content, {
+ borderStyle,
+ margin: 0,
+ float: 'left',
+ padding: {
+ bottom: 0,
+ left: horizontalPadding,
+ right: horizontalPadding,
+ top: 0,
+ }
+ })
+}