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
use std::fmt::{self, Display};
use nix::sched::CloneFlags;
use crate::Command;
#[derive(Clone, Debug)]
pub struct Style {
cmd_only: bool,
print_env: bool,
show_path: bool,
}
pub struct Printer<'a>(&'a Command, &'a Style);
impl Style {
pub fn debug() -> Style {
Style {
cmd_only: false,
print_env: true,
show_path: true,
}
}
pub fn short() -> Style {
Style {
cmd_only: true,
print_env: false,
show_path: false,
}
}
pub fn env(mut self, enable: bool) -> Style {
self.print_env = enable;
self
}
pub fn path(mut self, enable: bool) -> Style {
self.show_path = enable;
self
}
}
impl<'a> fmt::Display for Printer<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let Printer(cmd, opt) = *self;
if opt.cmd_only {
if opt.show_path {
write!(fmt, "{:?}", cmd.filename)?;
if cmd.args[0] != cmd.filename {
write!(fmt, " ({:?})", &cmd.args[0])?;
}
} else {
let path = if cmd.args[0] != cmd.filename {
&cmd.args[0]
} else {
&cmd.filename
};
let last_slash = path.as_bytes().iter()
.rposition(|&x| x == b'/');
if let Some(off) = last_slash {
write!(fmt, "{:?}",
&String::from_utf8_lossy(&path.as_bytes()[off+1..]))?;
} else {
write!(fmt, "{:?}", path)?;
}
}
for arg in cmd.args[1..].iter() {
write!(fmt, " {:?}", arg)?;
}
} else {
write!(fmt, "<Command {:?}", cmd.filename)?;
if cmd.args[0] != cmd.filename {
write!(fmt, " ({:?})", &cmd.args[0])?;
}
for arg in cmd.args[1..].iter() {
write!(fmt, " {:?}", arg)?;
}
if opt.print_env {
if let Some(ref env) = cmd.environ {
write!(fmt, "; environ: {{")?;
for (ref k, ref v) in env.iter() {
write!(fmt, "{:?}={:?},", k, v)?;
}
write!(fmt, "}}")?;
}
} else {
if let Some(ref env) = cmd.environ {
write!(fmt, "; environ[{}]", env.len())?;
}
}
if let Some(ref dir) = cmd.chroot_dir {
write!(fmt, "; chroot={:?}", dir)?;
}
if let Some((ref new, ref old, unmount)) = cmd.pivot_root {
write!(fmt, "; pivot_root=({:?};{:?};{})", new, old, unmount)?;
}
if cmd.config.namespaces != CloneFlags::empty() {
}
if let Some(ref dir) = cmd.config.work_dir {
write!(fmt, "; work-dir={:?}", dir)?;
}
if let Some((ref uidm, ref gidm)) = cmd.config.id_maps {
write!(fmt, "; uid_map={:?}", uidm)?;
write!(fmt, "; gid_map={:?}", gidm)?;
}
if let Some(ref uid) = cmd.config.uid {
write!(fmt, "; uid={}", uid)?;
}
if let Some(ref gid) = cmd.config.gid {
write!(fmt, "; gid={}", gid)?;
}
if let Some(ref gids) = cmd.config.supplementary_gids {
write!(fmt, "; gids={:?}", gids)?;
}
write!(fmt, ">")?
}
Ok(())
}
}
impl Command {
pub fn display<'a>(&'a self, style: &'a Style) -> Printer<'a> {
Printer(self, style)
}
}
impl fmt::Debug for Command {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
Printer(self, &Style::debug()).fmt(fmt)
}
}
#[cfg(test)]
mod test {
use crate::{Command, Style};
#[test]
fn test_debug() {
let mut cmd = Command::new("/bin/hello");
cmd.env_clear();
cmd.env("A", "B");
assert_eq!(&format!("{:?}", cmd),
r#"<Command "/bin/hello"; environ: {"A"="B",}>"#);
}
#[test]
fn test_comprehensive() {
let mut cmd = Command::new("/bin/hello");
cmd.env_clear();
cmd.env("A", "B");
assert_eq!(&format!("{}", cmd.display(&Style::debug())),
r#"<Command "/bin/hello"; environ: {"A"="B",}>"#);
}
#[test]
fn test_pretty() {
let mut cmd = Command::new("/bin/hello");
cmd.env_clear();
cmd.arg("world!");
assert_eq!(&format!("{}", cmd.display(&Style::short())),
r#""hello" "world!""#);
}
#[test]
fn test_no_env() {
let mut cmd = Command::new("/bin/hello");
cmd.env_clear();
cmd.env("A", "B");
assert_eq!(&format!("{}", cmd.display(&Style::debug().env(false))),
r#"<Command "/bin/hello"; environ[1]>"#);
}
}