sandbox_runtime/
cli.rs

1//! CLI parsing and execution.
2
3use std::path::PathBuf;
4
5use clap::Parser;
6
7/// Sandbox Runtime - OS-level sandboxing tool
8#[derive(Parser, Debug)]
9#[command(name = "srt")]
10#[command(about = "Sandbox Runtime - enforce filesystem and network restrictions on processes")]
11#[command(version)]
12pub struct Cli {
13    /// Enable debug logging
14    #[arg(short = 'd', long = "debug")]
15    pub debug: bool,
16
17    /// Path to settings file (default: ~/.srt-settings.json)
18    #[arg(short = 's', long = "settings")]
19    pub settings: Option<PathBuf>,
20
21    /// Run command string directly (sh -c mode)
22    #[arg(short = 'c')]
23    pub command: Option<String>,
24
25    /// Read config updates from file descriptor (JSON lines protocol)
26    #[arg(long = "control-fd")]
27    pub control_fd: Option<i32>,
28
29    /// Command and arguments to run
30    #[arg(trailing_var_arg = true)]
31    pub args: Vec<String>,
32}
33
34impl Cli {
35    /// Parse CLI arguments.
36    pub fn parse_args() -> Self {
37        Cli::parse()
38    }
39
40    /// Get the command to execute.
41    /// Returns (command_string, shell_mode)
42    /// - shell_mode = true when using -c flag
43    /// - shell_mode = false when using positional args
44    pub fn get_command(&self) -> Option<(String, bool)> {
45        if let Some(ref cmd) = self.command {
46            Some((cmd.clone(), true))
47        } else if !self.args.is_empty() {
48            // Join args with proper quoting
49            let cmd = crate::utils::join_args(&self.args);
50            Some((cmd, false))
51        } else {
52            None
53        }
54    }
55
56    /// Get the settings file path.
57    pub fn get_settings_path(&self) -> Option<PathBuf> {
58        self.settings.clone().or_else(crate::config::default_settings_path)
59    }
60}