|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { execSync, spawn } from 'child_process'; |
|
|
import yargs from 'yargs'; |
|
|
import { hideBin } from 'yargs/helpers'; |
|
|
|
|
|
try { |
|
|
execSync('node scripts/sandbox_command.js -q'); |
|
|
} catch { |
|
|
console.error('ERROR: sandboxing disabled. See docs to enable sandboxing.'); |
|
|
process.exit(1); |
|
|
} |
|
|
|
|
|
const argv = yargs(hideBin(process.argv)).option('i', { |
|
|
alias: 'interactive', |
|
|
type: 'boolean', |
|
|
default: false, |
|
|
}).argv; |
|
|
|
|
|
if (argv.i && !process.stdin.isTTY) { |
|
|
console.error( |
|
|
'ERROR: interactive mode (-i) requested without a terminal attached', |
|
|
); |
|
|
process.exit(1); |
|
|
} |
|
|
|
|
|
const image = 'gemini-cli-sandbox'; |
|
|
const sandboxCommand = execSync('node scripts/sandbox_command.js') |
|
|
.toString() |
|
|
.trim(); |
|
|
|
|
|
const sandboxes = execSync( |
|
|
`${sandboxCommand} ps --filter "ancestor=${image}" --format "{{.Names}}"`, |
|
|
) |
|
|
.toString() |
|
|
.trim() |
|
|
.split('\n') |
|
|
.filter(Boolean); |
|
|
|
|
|
let sandboxName; |
|
|
const firstArg = argv._[0]; |
|
|
|
|
|
if (firstArg) { |
|
|
if (firstArg.startsWith(image) || /^\d+$/.test(firstArg)) { |
|
|
sandboxName = firstArg.startsWith(image) |
|
|
? firstArg |
|
|
: `${image}-${firstArg}`; |
|
|
argv._.shift(); |
|
|
} |
|
|
} |
|
|
|
|
|
if (!sandboxName) { |
|
|
if (sandboxes.length === 0) { |
|
|
console.error( |
|
|
'No sandboxes found. Are you running gemini-cli with sandboxing enabled?', |
|
|
); |
|
|
process.exit(1); |
|
|
} |
|
|
if (sandboxes.length > 1) { |
|
|
console.error('Multiple sandboxes found:'); |
|
|
sandboxes.forEach((s) => console.error(` ${s}`)); |
|
|
console.error( |
|
|
'Sandbox name or index (0,1,...) must be specified as first argument', |
|
|
); |
|
|
process.exit(1); |
|
|
} |
|
|
sandboxName = sandboxes[0]; |
|
|
} |
|
|
|
|
|
if (!sandboxes.includes(sandboxName)) { |
|
|
console.error(`unknown sandbox ${sandboxName}`); |
|
|
console.error('known sandboxes:'); |
|
|
sandboxes.forEach((s) => console.error(` ${s}`)); |
|
|
process.exit(1); |
|
|
} |
|
|
|
|
|
const execArgs = []; |
|
|
let commandToRun = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (argv._.length > 0) { |
|
|
if (argv.i) { |
|
|
execArgs.push('-it'); |
|
|
} |
|
|
} else { |
|
|
execArgs.push('-it'); |
|
|
} |
|
|
|
|
|
|
|
|
if (argv._.length > 0) { |
|
|
|
|
|
const userCommand = argv._.join(' '); |
|
|
|
|
|
|
|
|
commandToRun = ['bash', '-l', '-c', userCommand]; |
|
|
} else { |
|
|
|
|
|
commandToRun = ['bash', '-l']; |
|
|
} |
|
|
|
|
|
const spawnArgs = ['exec', ...execArgs, sandboxName, ...commandToRun]; |
|
|
|
|
|
|
|
|
spawn(sandboxCommand, spawnArgs, { stdio: 'inherit' }); |
|
|
|