Skip to main content

sal_virt/buildah/
cmd.rs

1// Basic buildah operations for container management
2use super::BuildahError;
3use sal_process::CommandResult;
4use std::process::Command;
5
6/// Execute a buildah command and return the result
7///
8/// # Arguments
9///
10/// * `args` - The command arguments
11///
12/// # Returns
13///
14/// * `Result<CommandResult, BuildahError>` - Command result or error
15pub fn execute_buildah_command(args: &[&str]) -> Result<CommandResult, BuildahError> {
16    // Get the debug flag from thread-local storage
17    let debug = thread_local_debug();
18
19    if debug {
20        println!("Executing buildah command: buildah {}", args.join(" "));
21    }
22
23    let output = Command::new("buildah").args(args).output();
24
25    match output {
26        Ok(output) => {
27            let stdout = String::from_utf8_lossy(&output.stdout).to_string();
28            let stderr = String::from_utf8_lossy(&output.stderr).to_string();
29
30            let result = CommandResult {
31                stdout,
32                stderr,
33                success: output.status.success(),
34                code: output.status.code().unwrap_or(-1),
35            };
36
37            // Always output stdout/stderr when debug is true
38            if debug {
39                if !result.stdout.is_empty() {
40                    println!("Command stdout: {}", result.stdout);
41                }
42
43                if !result.stderr.is_empty() {
44                    println!("Command stderr: {}", result.stderr);
45                }
46
47                if result.success {
48                    println!("Command succeeded with code {}", result.code);
49                } else {
50                    println!("Command failed with code {}", result.code);
51                }
52            }
53
54            if result.success {
55                Ok(result)
56            } else {
57                // If command failed and debug is false, output stderr
58                if !debug {
59                    println!(
60                        "Command failed with code {}: {}",
61                        result.code,
62                        result.stderr.trim()
63                    );
64                }
65                Err(BuildahError::CommandFailed(format!(
66                    "Command failed with code {}: {}",
67                    result.code,
68                    result.stderr.trim()
69                )))
70            }
71        }
72        Err(e) => {
73            // Always output error information
74            println!("Command execution failed: {}", e);
75            Err(BuildahError::CommandExecutionFailed(e))
76        }
77    }
78}
79
80// Thread-local storage for debug flag
81thread_local! {
82    static DEBUG: std::cell::RefCell<bool> = std::cell::RefCell::new(false);
83}
84
85/// Set the debug flag for the current thread
86pub fn set_thread_local_debug(debug: bool) {
87    DEBUG.with(|cell| {
88        *cell.borrow_mut() = debug;
89    });
90}
91
92/// Get the debug flag for the current thread
93pub fn thread_local_debug() -> bool {
94    DEBUG.with(|cell| *cell.borrow())
95}
96
97// This function is no longer needed as the debug functionality is now integrated into execute_buildah_command