Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -94,13 +136,20 @@ async function main(): Promise<void> {
'path to config file (default: ~/.srt-settings.json)',
)
.option(
'-i, --inline-settings <settings>',
'inline JSON string for config settings',
'-c <command>',
'run command string directly (like sh -c), no escaping applied',
)
.allowUnknownOption()
.action(
async (
commandArgs: string[],
options: {
debug?: boolean
settings?: string
inlineSettings?: string
},
options: { debug?: boolean; settings?: string; c?: string },
) => {
try {
Expand All @@ -109,14 +158,18 @@ async function main(): Promise<void> {
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()
}

Expand Down