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
#[cfg(test)]
#[path = "./command_test.rs"]
mod command_test;
use run_script;
use run_script::{ScriptError, ScriptOptions};
use std::io;
use std::io::Error;
use std::process::{Command, ExitStatus, Output, Stdio};
use types::Step;
pub fn get_exit_code(
exit_status: Result<ExitStatus, Error>,
force: bool,
) -> i32 {
match exit_status {
Ok(code) => {
if !code.success() {
match code.code() {
Some(value) => value,
None => -1,
}
} else {
0
}
}
Err(error) => {
if !force {
error!("Error while executing command, error: {:#?}", error);
}
-1
}
}
}
pub fn get_exit_code_from_output(
output: &io::Result<Output>,
force: bool,
) -> i32 {
match output {
&Ok(ref output_struct) => get_exit_code(Ok(output_struct.status), force),
&Err(ref error) => {
if !force {
error!("Error while executing command, error: {:#?}", error);
}
-1
}
}
}
pub fn validate_exit_code(code: i32) {
if code == -1 {
error!("Error while executing command, unable to extract exit code.");
} else if code != 0 {
error!("Error while executing command, exit code: {}", code);
}
}
pub fn run_script_get_output(
script_lines: &Vec<String>,
script_runner: Option<String>,
capture_output: bool,
) -> Result<(i32, String, String), ScriptError> {
let mut options = ScriptOptions::new();
options.runner = script_runner.clone();
options.capture_output = capture_output;
options.exit_on_error = true;
options.print_commands = true;
run_script::run(script_lines.join("\n").as_str(), &vec![], &options)
}
pub fn run_script(
script_lines: &Vec<String>,
script_runner: Option<String>,
validate: bool,
) -> i32 {
let output = run_script_get_output(&script_lines, script_runner, false);
let exit_code = match output {
Ok(output_struct) => output_struct.0,
_ => -1,
};
if validate {
validate_exit_code(exit_code);
}
exit_code
}
pub fn run_command_get_output(
command_string: &str,
args: &Option<Vec<String>>,
capture_output: bool,
) -> io::Result<Output> {
debug!("Execute Command: {}", &command_string);
let mut command = Command::new(&command_string);
match *args {
Some(ref args_vec) => {
for arg in args_vec.iter() {
command.arg(arg);
}
}
None => debug!("No command args defined."),
};
command.stdin(Stdio::inherit());
if !capture_output {
command.stdout(Stdio::inherit()).stderr(Stdio::inherit());
}
info!("Execute Command: {:#?}", &command);
let output = command.output();
debug!("Output: {:#?}", &output);
output
}
pub fn run_command(
command_string: &str,
args: &Option<Vec<String>>,
validate: bool,
) -> i32 {
let output = run_command_get_output(&command_string, &args, false);
let exit_code = get_exit_code_from_output(&output, !validate);
if validate {
validate_exit_code(exit_code);
}
exit_code
}
pub fn run(step: &Step) {
let validate = !step.config.is_force();
match step.config.command {
Some(ref command_string) => {
run_command(&command_string, &step.config.args, validate);
}
None => {
match step.config.script {
Some(ref script) => {
run_script(script, step.config.script_runner.clone(), validate);
()
}
None => debug!("No script defined."),
};
}
};
}