1use super::BuildahError;
3use sal_process::CommandResult;
4use std::process::Command;
5
6pub fn execute_buildah_command(args: &[&str]) -> Result<CommandResult, BuildahError> {
16 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 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 !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 println!("Command execution failed: {}", e);
75 Err(BuildahError::CommandExecutionFailed(e))
76 }
77 }
78}
79
80thread_local! {
82 static DEBUG: std::cell::RefCell<bool> = std::cell::RefCell::new(false);
83}
84
85pub fn set_thread_local_debug(debug: bool) {
87 DEBUG.with(|cell| {
88 *cell.borrow_mut() = debug;
89 });
90}
91
92pub fn thread_local_debug() -> bool {
94 DEBUG.with(|cell| *cell.borrow())
95}
96
97