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
use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::io::Write;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::process::{Command, Output, Stdio};
use serde::Serialize;
#[derive(Serialize)]
pub struct Info {
program: String,
args: Vec<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
env: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
stdin: Option<String>,
}
fn describe_program(cmd: &OsStr) -> String {
let filename = Path::new(cmd).file_name().unwrap();
let name = filename.to_string_lossy();
let mut name = &name as &str;
if !env::consts::EXE_SUFFIX.is_empty() {
name = name.strip_suffix(env::consts::EXE_SUFFIX).unwrap_or(name);
}
name.into()
}
impl Info {
fn from_std_command(cmd: &Command) -> Info {
Info {
program: describe_program(cmd.get_program()),
args: cmd
.get_args()
.map(|x| x.to_string_lossy().into_owned())
.collect(),
env: cmd
.get_envs()
.map(|(k, v)| {
(
k.to_string_lossy().into_owned(),
v.unwrap_or(OsStr::new("")).to_string_lossy().into_owned(),
)
})
.collect(),
stdin: None,
}
}
}
pub trait Spawn {
#[doc(hidden)]
fn spawn_with_info(&mut self) -> (Info, Output);
}
impl Spawn for Command {
fn spawn_with_info(&mut self) -> (Info, Output) {
let info = Info::from_std_command(self);
let output = self.output().unwrap();
(info, output)
}
}
impl<'a> Spawn for &'a mut Command {
fn spawn_with_info(&mut self) -> (Info, Output) {
<Command as Spawn>::spawn_with_info(self)
}
}
pub struct StdinCommand {
command: Command,
stdin: Vec<u8>,
}
impl StdinCommand {
pub fn new<S: AsRef<OsStr>, I: Into<Vec<u8>>>(program: S, stdin: I) -> StdinCommand {
let mut command = Command::new(program);
command.stdin(Stdio::piped());
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
StdinCommand {
command,
stdin: stdin.into(),
}
}
}
impl Deref for StdinCommand {
type Target = Command;
fn deref(&self) -> &Self::Target {
&self.command
}
}
impl DerefMut for StdinCommand {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.command
}
}
impl Spawn for StdinCommand {
fn spawn_with_info(&mut self) -> (Info, Output) {
let mut info = Info::from_std_command(&self.command);
let mut child = self.command.spawn().unwrap();
let mut stdin = child.stdin.take().expect("Failed to open stdin");
let to_write = mem::take(&mut self.stdin);
info.stdin = Some(String::from_utf8_lossy(&to_write).into());
std::thread::spawn(move || {
stdin
.write_all(&to_write)
.expect("Failed to write to stdin");
});
let output = child.wait_with_output().unwrap();
(info, output)
}
}
impl<'a, T: AsRef<OsStr>> Spawn for &'a [T] {
fn spawn_with_info(&mut self) -> (Info, Output) {
let mut cmd = Command::new(self.get(0).expect("expected program name as first item"));
for arg in &self[1..] {
cmd.arg(arg);
}
cmd.spawn_with_info()
}
}
impl<T: AsRef<OsStr>, const N: usize> Spawn for [T; N] {
fn spawn_with_info(&mut self) -> (Info, Output) {
(&self[..]).spawn_with_info()
}
}
impl<T: AsRef<OsStr>> Spawn for Vec<T> {
fn spawn_with_info(&mut self) -> (Info, Output) {
(&self[..]).spawn_with_info()
}
}