Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add flag for killing all running sandboxes to the CLI

Tomas Valenta125cda6cc75d
diff --git a/packages/cli/src/commands/sandbox/kill.ts b/packages/cli/src/commands/sandbox/kill.ts
index 86cd0d494..278365926 100644
--- a/packages/cli/src/commands/sandbox/kill.ts
+++ b/packages/cli/src/commands/sandbox/kill.ts
@@ -4,31 +4,61 @@ import { ensureAPIKey } from 'src/api'
import { asBold } from 'src/utils/format'
import * as e2b from 'e2b'
+async function killSandbox(sandboxID: string, apiKey: string) {
+ const killed = await e2b.Sandbox.kill(sandboxID, { apiKey })
+ if (killed) {
+ console.log(`Sandbox ${asBold(sandboxID)} has been killed`)
+ } else {
+ console.error(`Sandbox ${asBold(sandboxID)} wasn't found`)
+ }
+}
+
export const killCommand = new commander.Command('kill')
.description('kill sandbox')
.argument(
- '<sandboxID>',
- `kill the sandbox specified by ${asBold('<sandboxID>')}`,
+ '[sandboxID]',
+ `kill the sandbox specified by ${asBold('[sandboxID]')}`,
)
.alias('kl')
- .action(async (sandboxID: string) => {
+ .option('-a, --all', 'kill all running sandboxes')
+ .action(async (sandboxID: string, { all }: { all: boolean }) => {
try {
const apiKey = ensureAPIKey()
- if (!sandboxID) {
- console.error('You need to specify sandbox ID')
+ if (!sandboxID && !all) {
+ console.error(
+ `You need to specify ${asBold('[sandboxID]')} or use ${asBold(
+ '-a/--all',
+ )} flag`,
+ )
process.exit(1)
}
- await e2b.Sandbox.kill(sandboxID, { apiKey })
+ if (all && sandboxID) {
+ console.error(
+ `You cannot use ${asBold('-a/--all')} flag while specifying ${asBold(
+ '[sandboxID]',
+ )}`,
+ )
+ process.exit(1)
+ }
- console.log(`Sandbox ${asBold(sandboxID)} has been killed`)
- } catch (err: any) {
- if (err?.status === 404) {
- console.error(`Sandbox ${asBold(sandboxID)} wasn't found`)
+ if (all) {
+ const sandboxes = await e2b.Sandbox.list({ apiKey })
+
+ if (sandboxes.length === 0) {
+ console.log('No running sandboxes')
+ process.exit(0)
+ }
+
+ await Promise.all(
+ sandboxes.map((sandbox) => killSandbox(sandbox.sandboxId, apiKey)),
+ )
} else {
- console.error(err)
+ await killSandbox(sandboxID, apiKey)
}
+ } catch (err: any) {
+ console.error(err)
process.exit(1)
}
})