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
use std::fs::canonicalize;
use std::io::Result;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::process;
use std::process::ExitStatus;
use clap::crate_name;
use crate::command::Command;
pub fn execute_command(cmd: Command, maskfile_path: String) -> Result<ExitStatus> {
if cmd.script.source == "" {
let msg = "Command has no script.";
return Err(Error::new(ErrorKind::Other, msg));
}
if cmd.script.executor == "" {
let msg = "Command script requires a lang code which determines which executor to use.";
return Err(Error::new(ErrorKind::Other, msg));
}
let mut child = prepare_command(&cmd);
child = add_utility_variables(child, maskfile_path);
child = add_flag_variables(child, &cmd);
child.spawn()?.wait()
}
fn prepare_command(cmd: &Command) -> process::Command {
let executor = cmd.script.executor.clone();
let source = cmd.script.source.clone();
match executor.as_ref() {
"js" | "javascript" => {
let mut child;
child = process::Command::new("node");
child.arg("-e").arg(source);
child
}
"py" | "python" => {
let mut child = process::Command::new("python");
child.arg("-c").arg(source);
child
}
"rb" | "ruby" => {
let mut child = process::Command::new("ruby");
child.arg("-e").arg(source);
child
}
"php" => {
let mut child = process::Command::new("php");
child.arg("-r").arg(source);
child
}
#[cfg(windows)]
"cmd" | "batch" => {
let mut child = process::Command::new("cmd.exe");
child.arg("/c").arg(source);
child
}
#[cfg(windows)]
"powershell" => {
let mut child = process::Command::new("powershell.exe");
child.arg("-c").arg(source);
child
}
_ => {
let mut child = process::Command::new(executor);
child.arg("-c").arg(source);
child
}
}
}
fn add_utility_variables(mut child: process::Command, maskfile_path: String) -> process::Command {
let maskfile_path = PathBuf::from(maskfile_path);
let absolute_path = canonicalize(&maskfile_path)
.expect("canonicalize maskfile path failed")
.to_str()
.unwrap()
.to_string();
let absolute_path = Path::new(&absolute_path);
let absolute_path_str = absolute_path.to_str().unwrap();
let parent_dir = absolute_path.parent().unwrap().to_str().unwrap();
child.env(
"MASK",
format!("{} --maskfile {}", crate_name!(), absolute_path_str),
);
child.env("MASKFILE_DIR", parent_dir);
child
}
fn add_flag_variables(mut child: process::Command, cmd: &Command) -> process::Command {
for arg in &cmd.required_args {
child.env(arg.name.clone(), arg.val.clone());
}
for flag in &cmd.option_flags {
if flag.val != "" {
child.env(flag.name.clone(), flag.val.clone());
}
}
child
}