-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathcommand.rs
209 lines (169 loc) · 5.03 KB
/
command.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
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
207
208
209
use crate::{async_command::AsyncCommand, command_inspector::CommandInspector, shell::Shell};
use moon_common::color;
use moon_console::Console;
use rustc_hash::FxHashMap;
use std::{
ffi::{OsStr, OsString},
path::{Path, PathBuf},
sync::Arc,
};
use tokio::process::Command as TokioCommand;
pub struct Command {
pub args: Vec<OsString>,
pub bin: OsString,
pub cwd: Option<PathBuf>,
pub env: FxHashMap<OsString, OsString>,
/// Convert non-zero exits to errors
pub error_on_nonzero: bool,
/// Escape/quote arguments when joining.
pub escape_args: bool,
/// Values to pass to stdin
pub input: Vec<OsString>,
/// Prefix to prepend to all log lines
pub prefix: Option<String>,
/// Log the command to the terminal before running
pub print_command: bool,
/// Shell to wrap executing commands in
pub shell: Option<Shell>,
/// Console to write output to
pub console: Option<Arc<Console>>,
}
impl Command {
pub fn new<S: AsRef<OsStr>>(bin: S) -> Self {
Command {
bin: bin.as_ref().to_os_string(),
args: vec![],
cwd: None,
env: FxHashMap::default(),
error_on_nonzero: true,
escape_args: true,
input: vec![],
prefix: None,
print_command: false,
shell: Some(Shell::default()),
console: None,
}
}
pub fn arg<A: AsRef<OsStr>>(&mut self, arg: A) -> &mut Command {
self.args.push(arg.as_ref().to_os_string());
self
}
pub fn arg_if_missing<A: AsRef<OsStr>>(&mut self, arg: A) -> &mut Command {
let arg = arg.as_ref();
let present = self.args.iter().any(|a| a == arg);
if !present {
self.arg(arg);
}
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Command
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
for arg in args {
self.arg(arg);
}
self
}
pub fn cwd<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
self.cwd = Some(dir.as_ref().to_path_buf());
self
}
pub fn create_async(&self) -> AsyncCommand {
let inspector = self.inspect();
let command_line = inspector.get_command_line();
let mut command = TokioCommand::new(&command_line.command[0]);
command.args(&command_line.command[1..]);
command.envs(&self.env);
command.kill_on_drop(true);
if let Some(cwd) = &self.cwd {
command.current_dir(cwd);
}
AsyncCommand {
console: self.console.clone(),
inner: command,
inspector,
current_id: None,
}
}
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.env
.insert(key.as_ref().to_os_string(), val.as_ref().to_os_string());
self
}
pub fn env_if_missing<K, V>(&mut self, key: K, val: V) -> &mut Command
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
let key = key.as_ref();
if !self.env.contains_key(key) {
self.env(key, val);
}
self
}
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
for (k, v) in vars {
self.env(k, v);
}
self
}
pub fn inherit_colors(&mut self) -> &mut Command {
let level = color::supports_color().to_string();
self.env("FORCE_COLOR", &level);
self.env("CLICOLOR_FORCE", &level);
// Force a terminal width so that we have consistent sizing
// in our cached output, and its the same across all machines
// https://help.gnome.org/users/gnome-terminal/stable/app-terminal-sizes.html.en
self.env("COLUMNS", "80");
self.env("LINES", "24");
self
}
pub fn input<I, V>(&mut self, input: I) -> &mut Command
where
I: IntoIterator<Item = V>,
V: AsRef<OsStr>,
{
for i in input {
self.input.push(i.as_ref().to_os_string());
}
self
}
pub fn inspect(&self) -> CommandInspector {
CommandInspector::new(self)
}
pub fn set_print_command(&mut self, state: bool) -> &mut Command {
self.print_command = state;
self
}
pub fn set_error_on_nonzero(&mut self, state: bool) -> &mut Command {
self.error_on_nonzero = state;
self
}
pub fn set_prefix(&mut self, prefix: &str) -> &mut Command {
self.prefix = Some(prefix.to_owned());
self
}
pub fn with_console(&mut self, console: Arc<Console>) -> &mut Command {
self.console = Some(console);
self
}
pub fn with_shell(&mut self, shell: Shell) -> &mut Command {
self.shell = Some(shell);
self
}
pub fn without_shell(&mut self) -> &mut Command {
self.shell = None;
self
}
}