-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
162 lines (142 loc) · 4.14 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use cfg_if::cfg_if;
#[allow(unused)]
use lazy_static::lazy_static;
#[allow(unused)]
use std::sync::atomic::{AtomicBool, Ordering};
#[allow(unused)]
use std::sync::Mutex;
use std::time::Instant;
pub mod fs;
pub mod mount;
pub mod network;
#[derive(Debug)]
pub struct CommandResult {
pub command: String,
pub stdout: String,
pub stderr: String,
pub status: std::process::ExitStatus,
}
impl std::fmt::Display for CommandResult {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"Command '{}' executed and failed with status: {}",
self.command, self.status
)?;
write!(f, " stdout: {}", self.stdout)?;
write!(f, " stderr: {}", self.stderr)
}
}
#[derive(thiserror::Error, Debug)]
pub enum CommandExecutionError {
#[error("Failed to start execution of '{command}': {err}")]
ExecutionStart {
command: String,
err: std::io::Error,
},
#[error("{0}")]
CommandFailure(Box<CommandResult>),
}
#[cfg_attr(
any(test, automock, feature = "testing"),
mockall::automock,
allow(dead_code)
)]
pub mod inner {
use super::*;
pub fn to_string(command: &std::process::Command) -> String {
format!(
"{} {}",
command.get_program().to_string_lossy(),
command
.get_args()
.map(|s| s.to_string_lossy().into())
.collect::<Vec<String>>()
.join(" ")
)
}
pub fn output_to_exec_error(
command: &std::process::Command,
output: &std::process::Output,
) -> CommandExecutionError {
CommandExecutionError::CommandFailure(Box::new(CommandResult {
command: to_string(command),
status: output.status,
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
}))
}
pub fn internal_exec(
cmd: &mut std::process::Command,
) -> Result<std::process::Output, CommandExecutionError> {
let start = Instant::now();
let output = cmd
.output()
.map_err(|err| CommandExecutionError::ExecutionStart {
command: to_string(cmd),
err,
})?;
if !output.status.success() {
return Err(output_to_exec_error(cmd, &output));
}
let duration = start.elapsed();
log::trace!("Command {:?} executed in {}ms", cmd, duration.as_millis());
Ok(output)
}
pub fn internal_exec_spawn(
cmd: &mut std::process::Command,
) -> Result<std::process::Child, CommandExecutionError> {
let output = cmd
.spawn()
.map_err(|err| CommandExecutionError::ExecutionStart {
command: to_string(cmd),
err,
})?;
Ok(output)
}
}
#[cfg(any(test, feature = "testing"))]
pub static USE_MOCKS: AtomicBool = AtomicBool::new(true);
pub fn exec(
cmd: &mut std::process::Command,
) -> Result<std::process::Output, CommandExecutionError> {
log::trace!(
"Executing command {:?} with args {:?}",
cmd.get_program(),
cmd.get_args()
);
cfg_if! {
if #[cfg(any(test, feature = "testing"))] {
if USE_MOCKS.load(Ordering::SeqCst) {
mock_inner::internal_exec(cmd)
} else {
inner::internal_exec(cmd)
}
} else {
inner::internal_exec(cmd)
}
}
}
pub fn exec_spawn(
cmd: &mut std::process::Command,
) -> Result<std::process::Child, CommandExecutionError> {
log::trace!(
"Executing command {:?} with args {:?}",
cmd.get_program(),
cmd.get_args()
);
cfg_if! {
if #[cfg(any(test, feature = "testing"))] {
if USE_MOCKS.load(Ordering::SeqCst) {
mock_inner::internal_exec_spawn(cmd)
} else {
inner::internal_exec_spawn(cmd)
}
} else {
inner::internal_exec_spawn(cmd)
}
}
}
lazy_static! {
pub static ref MTX: Mutex<()> = Mutex::new(());
}