|
1 | 1 | use color_eyre::Result; |
| 2 | +use std::path::Path; |
2 | 3 | use tracing::info; |
3 | 4 |
|
4 | 5 | use crate::{config::Config, runner::CmdSpec}; |
@@ -66,6 +67,78 @@ pub fn run_nargo_command(cfg: &Config, base_args: &[&str]) -> Result<()> { |
66 | 67 | cfg.runner.run(&spec) |
67 | 68 | } |
68 | 69 |
|
| 70 | +/// Run a nargo command in a specific working directory |
| 71 | +/// |
| 72 | +/// This is similar to `run_nargo_command` but allows specifying a working directory |
| 73 | +/// for the command execution. If `working_dir` is None, behaves like `run_nargo_command`. |
| 74 | +/// |
| 75 | +/// # Arguments |
| 76 | +/// * `cfg` - The global configuration containing all flags |
| 77 | +/// * `base_args` - Base command arguments to pass to nargo |
| 78 | +/// * `working_dir` - Optional working directory for command execution |
| 79 | +/// |
| 80 | +/// # Returns |
| 81 | +/// * `Result<()>` - Success or error from command execution |
| 82 | +pub fn run_nargo_command_in_directory( |
| 83 | + cfg: &Config, |
| 84 | + base_args: &[&str], |
| 85 | + working_dir: Option<&Path>, |
| 86 | +) -> Result<()> { |
| 87 | + let args = build_nargo_args(cfg, base_args)?; |
| 88 | + |
| 89 | + if cfg.verbose && !cfg.quiet { |
| 90 | + info!("Running: nargo {}", args.join(" ")); |
| 91 | + } |
| 92 | + |
| 93 | + // Create command specification for nargo |
| 94 | + let mut spec = CmdSpec::new("nargo".to_string(), args); |
| 95 | + |
| 96 | + // Set working directory if provided |
| 97 | + if let Some(dir) = working_dir { |
| 98 | + spec = spec.with_cwd(dir.to_path_buf()); |
| 99 | + } |
| 100 | + |
| 101 | + // Use the runner to execute the command (handles dry-run automatically) |
| 102 | + cfg.runner.run(&spec) |
| 103 | +} |
| 104 | + |
| 105 | +/// Run any external tool in a specific working directory |
| 106 | +/// |
| 107 | +/// This is similar to `run_tool` but allows specifying a working directory |
| 108 | +/// for the command execution. If `working_dir` is None, behaves like `run_tool`. |
| 109 | +/// |
| 110 | +/// # Arguments |
| 111 | +/// * `cfg` - The global configuration containing all flags and runner |
| 112 | +/// * `tool` - The tool command to run (bb, garaga, forge, cast, nargo, etc.) |
| 113 | +/// * `args` - Arguments to pass to the tool |
| 114 | +/// * `working_dir` - Optional working directory for command execution |
| 115 | +/// |
| 116 | +/// # Returns |
| 117 | +/// * `Result<()>` - Success or error from command execution |
| 118 | +pub fn run_tool_in_directory( |
| 119 | + cfg: &Config, |
| 120 | + tool: &str, |
| 121 | + args: &[&str], |
| 122 | + working_dir: Option<&Path>, |
| 123 | +) -> Result<()> { |
| 124 | + let args_vec: Vec<String> = args.iter().map(|s| s.to_string()).collect(); |
| 125 | + |
| 126 | + if cfg.verbose && !cfg.quiet { |
| 127 | + info!("Running: {} {}", tool, args_vec.join(" ")); |
| 128 | + } |
| 129 | + |
| 130 | + // Create command specification for the tool |
| 131 | + let mut spec = CmdSpec::new(tool.to_string(), args_vec); |
| 132 | + |
| 133 | + // Set working directory if provided |
| 134 | + if let Some(dir) = working_dir { |
| 135 | + spec = spec.with_cwd(dir.to_path_buf()); |
| 136 | + } |
| 137 | + |
| 138 | + // Use the runner to execute the command (handles dry-run automatically) |
| 139 | + cfg.runner.run(&spec) |
| 140 | +} |
| 141 | + |
69 | 142 | /// Run any external tool with unified command execution |
70 | 143 | /// |
71 | 144 | /// This is the unified helper for executing external tools consistently across all |
@@ -139,3 +212,44 @@ pub fn run_tool_capture(cfg: &Config, tool: &str, args: &[&str]) -> Result<Strin |
139 | 212 | // Use the runner to execute the command and capture output |
140 | 213 | cfg.runner.run_capture(&spec) |
141 | 214 | } |
| 215 | + |
| 216 | +/// Run any external tool in a specific working directory and capture its stdout |
| 217 | +/// |
| 218 | +/// This is similar to `run_tool_capture` but allows specifying a working directory |
| 219 | +/// for the command execution. If `working_dir` is None, behaves like `run_tool_capture`. |
| 220 | +/// |
| 221 | +/// # Arguments |
| 222 | +/// * `cfg` - The global configuration containing all flags and runner |
| 223 | +/// * `tool` - The tool command to run (bb, garaga, forge, cast, nargo, etc.) |
| 224 | +/// * `args` - Arguments to pass to the tool |
| 225 | +/// * `working_dir` - Optional working directory for command execution |
| 226 | +/// |
| 227 | +/// # Returns |
| 228 | +/// * `Result<String>` - Stdout from command execution or error |
| 229 | +pub fn run_tool_capture_in_directory( |
| 230 | + cfg: &Config, |
| 231 | + tool: &str, |
| 232 | + args: &[&str], |
| 233 | + working_dir: Option<&Path>, |
| 234 | +) -> Result<String> { |
| 235 | + let args_vec: Vec<String> = args.iter().map(|s| s.to_string()).collect(); |
| 236 | + |
| 237 | + if cfg.verbose && !cfg.quiet { |
| 238 | + info!( |
| 239 | + "Running (capturing output): {} {}", |
| 240 | + tool, |
| 241 | + args_vec.join(" ") |
| 242 | + ); |
| 243 | + } |
| 244 | + |
| 245 | + // Create command specification for the tool |
| 246 | + let mut spec = CmdSpec::new(tool.to_string(), args_vec); |
| 247 | + |
| 248 | + // Set working directory if provided |
| 249 | + if let Some(dir) = working_dir { |
| 250 | + spec = spec.with_cwd(dir.to_path_buf()); |
| 251 | + } |
| 252 | + |
| 253 | + // Use the runner to execute the command and capture output |
| 254 | + cfg.runner.run_capture(&spec) |
| 255 | +} |
0 commit comments