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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use super::errors;
use super::eval;
use super::model;
pub fn run<S>(cmd: &[S]) -> Result<(), errors::GardenError>
where
S: AsRef<std::ffi::OsStr>,
{
let mut exit_status: i32 = 1;
if let Ok(mut p) = subprocess::Popen::create(cmd, subprocess::PopenConfig::default()) {
exit_status = status(p.wait());
}
result_from_exit_status(exit_status)
}
pub fn result_from_exit_status(exit_status: i32) -> Result<(), errors::GardenError> {
match exit_status {
0 => Ok(()),
_ => Err(errors::GardenError::ExitStatus(exit_status)),
}
}
pub fn status(result: subprocess::Result<subprocess::ExitStatus>) -> i32 {
let mut exit_status: i32 = 1;
if let Ok(status_result) = result {
match status_result {
subprocess::ExitStatus::Exited(status) => {
exit_status = status as i32;
}
subprocess::ExitStatus::Signaled(status) => {
exit_status = status as i32;
}
subprocess::ExitStatus::Other(status) => {
exit_status = status;
}
_ => (),
}
}
exit_status
}
pub fn trim_stdout(capture: &subprocess::CaptureData) -> String {
capture.stdout_str().trim_end().into()
}
pub fn capture_stdout(
exec: subprocess::Exec,
) -> Result<subprocess::CaptureData, subprocess::PopenError> {
exec.stdout(subprocess::Redirection::Pipe).capture()
}
pub fn exec_cmd<S>(command: &[S]) -> subprocess::Exec
where
S: AsRef<std::ffi::OsStr>,
{
if command.len() > 1 {
subprocess::Exec::cmd(&command[0]).args(&command[1..])
} else {
subprocess::Exec::cmd(&command[0])
}
}
pub fn exec_in_dir<P, S>(command: &[S], path: P) -> subprocess::Exec
where
P: AsRef<std::path::Path>,
S: AsRef<std::ffi::OsStr>,
{
exec_cmd(&command).cwd(path)
}
pub fn exec_in_context<S>(
config: &mut model::Configuration,
context: &model::TreeContext,
quiet: bool,
verbose: bool,
command: &[S],
) -> Result<(), errors::GardenError>
where
S: AsRef<std::ffi::OsStr>,
{
let path;
{
let tree = &config.trees[context.tree];
path = tree.path_as_ref()?.clone();
if !model::print_tree(&tree, verbose, quiet) {
return Ok(());
}
}
let env = eval::environment(config, context);
let command_vec = resolve_command(&command, &env);
let mut exec = exec_in_dir(&command_vec, &path);
for (name, value) in &env {
exec = exec.env(name, value);
}
result_from_exit_status(status(exec.join()))
}
pub fn resolve_command<S>(command: &[S], env: &[(String, String)]) -> Vec<String>
where
S: AsRef<std::ffi::OsStr>,
{
let mut cmd_path = std::path::PathBuf::from(&command[0]);
if !cmd_path.is_absolute() {
for (name, value) in env {
if name == "PATH" {
if let Some(path_buf) = std::env::split_paths(&value)
.filter_map(|dir| {
let full_path = dir.join(&cmd_path);
if full_path.is_file() {
Some(full_path)
} else {
None
}
})
.next()
{
cmd_path = path_buf;
}
break;
}
}
}
let mut command_vec: Vec<String> = Vec::new();
command_vec.reserve(command.len());
command_vec.push(cmd_path.to_string_lossy().to_string());
for arg in &command[1..] {
let curpath = std::path::PathBuf::from(arg);
command_vec.push(curpath.to_string_lossy().into());
}
command_vec
}
pub fn split_on_dash<S>(strings: &[S], pre_dash: &mut Vec<String>, post_dash: &mut Vec<String>)
where
S: AsRef<std::ffi::OsStr> + std::string::ToString + std::cmp::PartialEq,
{
let mut is_pre_dash = true;
for string in strings {
if is_pre_dash {
if string.as_ref() == "--" {
is_pre_dash = false;
continue;
}
pre_dash.push(string.to_string());
} else {
post_dash.push(string.to_string());
}
}
}
pub fn current_exe() -> String {
match std::env::current_exe() {
Err(_) => "garden".into(),
Ok(path) => path.to_string_lossy().into(),
}
}
pub fn parse_args(parser: argparse::ArgumentParser, arguments: Vec<String>) {
parser
.parse(arguments, &mut std::io::stdout(), &mut std::io::stderr())
.map_err(|exit_status| std::process::exit(exit_status))
.ok();
}