Skip to content

Commit

Permalink
Add flag for killing all running sandboxes to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Oct 12, 2024
1 parent f2de8cb commit 125cda6
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions packages/cli/src/commands/sandbox/kill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

0 comments on commit 125cda6

Please sign in to comment.