diff --git a/src/cli.ts b/src/cli.ts index d8aafcb..a59a29c 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -51,6 +51,48 @@ function loadConfig(filePath: string): SandboxRuntimeConfig | null { } } +/** + * Load and validate sandbox configuration from an inline JSON string. + */ +function loadInlineSettings( + inlineSettings: string, +): SandboxRuntimeConfig | null { + try { + if (!inlineSettings) { + return null + } + const content = inlineSettings + if (content.trim() === '') { + return null + } + + // Parse JSON + const parsed = JSON.parse(content) + + // Validate with zod schema + const result = SandboxRuntimeConfigSchema.safeParse(parsed) + + if (!result.success) { + console.error(`Invalid settings in inline settings:`) + result.error.issues.forEach(issue => { + const path = issue.path.join('.') + console.error(` - ${path}: ${issue.message}`) + }) + return null + } + + return result.data + } catch (error) { + // Log parse errors to help users debug invalid config files + if (error instanceof SyntaxError) { + console.error(`Invalid JSON in inline settings: ${error.message}`) + } else { + console.error(`Failed to load config from inline settings: ${error}`) + } + return null + } +} + /** * Get default config path */ @@ -94,6 +136,8 @@ async function main(): Promise { 'path to config file (default: ~/.srt-settings.json)', ) .option( + '-i, --inline-settings ', + 'inline JSON string for config settings', '-c ', 'run command string directly (like sh -c), no escaping applied', ) @@ -101,6 +145,11 @@ async function main(): Promise { .action( async ( commandArgs: string[], + options: { + debug?: boolean + settings?: string + inlineSettings?: string + }, options: { debug?: boolean; settings?: string; c?: string }, ) => { try { @@ -109,14 +158,18 @@ async function main(): Promise { process.env.DEBUG = 'true' } - // Load config from file - const configPath = options.settings || getDefaultConfigPath() - let runtimeConfig = loadConfig(configPath) + let runtimeConfig: SandboxRuntimeConfig | null = null + // check for inline settings + if (options.inlineSettings) { + runtimeConfig = loadInlineSettings(options.inlineSettings) + } else { + // Load config from file + const configPath = options.settings || getDefaultConfigPath() + runtimeConfig = loadConfig(configPath) + } if (!runtimeConfig) { - logForDebugging( - `No config found at ${configPath}, using default config`, - ) + logForDebugging(`No valid config found, using default config`) runtimeConfig = getDefaultConfig() }