Skip to content

Commit ce4c4bc

Browse files
committed
Simplify exec command parsing and tests
1 parent 12aa7e2 commit ce4c4bc

2 files changed

Lines changed: 26 additions & 27 deletions

File tree

packages/cli/src/commands/exec.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,35 @@ export function execCommand(): Command {
66
return new Command('exec')
77
.description('Execute a command in a sandbox')
88
.argument('<sandbox_id>', 'Sandbox ID')
9-
.argument('<cmd>', 'Command to execute (quote if it contains spaces)')
10-
.option('--no-stream', 'Wait for completion instead of streaming')
9+
.argument('<cmd...>', 'Command to execute')
10+
.allowUnknownOption(true)
1111
.option('--json', 'Output as JSON')
1212
.action(
1313
async (
1414
sandboxId: string,
15-
cmd: string,
16-
options: { stream: boolean; json?: boolean },
15+
cmdParts: string[],
16+
options: { json?: boolean },
1717
) => {
18+
const cmd = cmdParts.join(' ')
1819
try {
1920
const client = getClient()
2021
const sandbox = await client.get(sandboxId)
22+
const result = await sandbox.exec(cmd)
2123

22-
if (options.json || !options.stream) {
23-
const result = await sandbox.exec(cmd)
24-
25-
if (options.json) {
26-
printJson({
27-
exec_id: result.execId,
28-
exit_code: result.exitCode,
29-
stdout: result.stdout,
30-
stderr: result.stderr,
31-
duration_ms: result.durationMs,
32-
})
33-
} else {
34-
if (result.stdout) process.stdout.write(result.stdout)
35-
if (result.stderr) process.stderr.write(result.stderr)
36-
}
37-
38-
if (result.exitCode !== 0) process.exit(1)
39-
} else {
40-
const result = await sandbox.exec(cmd, {
41-
onStdout: (data) => process.stdout.write(data),
42-
onStderr: (data) => process.stderr.write(data),
24+
if (options.json) {
25+
printJson({
26+
exec_id: result.execId,
27+
exit_code: result.exitCode,
28+
stdout: result.stdout,
29+
stderr: result.stderr,
30+
duration_ms: result.durationMs,
4331
})
44-
45-
if (result.exitCode !== 0) process.exit(1)
32+
} else {
33+
if (result.stdout) process.stdout.write(result.stdout)
34+
if (result.stderr) process.stderr.write(result.stderr)
4635
}
36+
37+
if (result.exitCode !== 0) process.exit(1)
4738
} catch (err) {
4839
handleError(err)
4940
}

packages/cli/src/commands/mcp-init.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ describe('mcp init command', () => {
1313
let tempDir: string
1414
let previousHome: string | undefined
1515
let previousApiKey: string | undefined
16+
let previousXdg: string | undefined
1617
let logSpy: ReturnType<typeof spyOn>
1718

1819
beforeEach(() => {
1920
tempDir = mkdtempSync(join(tmpdir(), 'sandchest-mcp-init-'))
2021
previousHome = process.env['HOME']
2122
previousApiKey = process.env['SANDCHEST_API_KEY']
23+
previousXdg = process.env['XDG_CONFIG_HOME']
2224
process.env['HOME'] = tempDir
25+
process.env['XDG_CONFIG_HOME'] = join(tempDir, '.config')
2326
process.env['SANDCHEST_API_KEY'] = 'sk_test_key'
2427
process.env['NO_COLOR'] = '1'
2528
logSpy = spyOn(console, 'log').mockImplementation(() => {})
@@ -37,6 +40,11 @@ describe('mcp init command', () => {
3740
} else {
3841
process.env['SANDCHEST_API_KEY'] = previousApiKey
3942
}
43+
if (previousXdg === undefined) {
44+
delete process.env['XDG_CONFIG_HOME']
45+
} else {
46+
process.env['XDG_CONFIG_HOME'] = previousXdg
47+
}
4048
delete process.env['NO_COLOR']
4149
logSpy.mockRestore()
4250
})

0 commit comments

Comments
 (0)