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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Cross-platform, asynchronous library to run commands in pipelines.
//!
//! The core concept of this library is to simplify the execution of
//! commands, following these rules:
//!
//! 1. Commands are executed asynchronously, using the [tokio] async
//! runtime.
//!
//! 2. Commands work on all major platforms (windows, macos and
//! linux).
//!
//! 3. Commands can be executed in a pipeline, which means the output
//! of the previous command is send as input of the next one.

use log::{debug, error};
use once_cell::sync::Lazy;
use std::{
    env, io,
    ops::{Deref, DerefMut},
    process::Stdio,
    result,
    string::FromUtf8Error,
};
use thiserror::Error;
use tokio::io::AsyncWriteExt;

const TOKIO_CMD: Lazy<tokio::process::Command> = Lazy::new(|| {
    let windows = cfg!(target_os = "windows")
        && !(env::var("MSYSTEM")
            .map(|env| env.starts_with("MINGW"))
            .unwrap_or_default());

    let (shell, arg) = if windows { ("cmd", "/C") } else { ("sh", "-c") };

    let mut cmd = tokio::process::Command::new(shell);
    cmd.arg(arg);
    cmd
});

/// The global `Error` enum of the library.
#[derive(Debug, Error)]
pub enum Error {
    #[error("cannot run command: {1}")]
    SpawnProcessError(#[source] io::Error, String),
    #[error("cannot get standard input")]
    GetStdinError,
    #[error("cannot wait for exit status code of command: {1}")]
    WaitForExitStatusCodeError(#[source] io::Error, String),
    #[error("cannot get exit status code of command: {0}")]
    GetExitStatusCodeNotAvailableError(String),
    #[error("command {0} returned non-zero exit status code {1}: {2}")]
    InvalidExitStatusCodeNonZeroError(String, i32, String),
    #[error("cannot write data to standard input")]
    WriteStdinError(#[source] io::Error),
    #[error("cannot get standard output")]
    GetStdoutError,
    #[error("cannot read data from standard output")]
    ReadStdoutError(#[source] io::Error),
    #[error("cannot get standard error")]
    GetStderrError,
    #[error("cannot read data from standard error")]
    ReadStderrError(#[source] io::Error),
    #[error("cannot get command output")]
    GetOutputError(#[source] io::Error),
    #[error("cannot parse command output as string")]
    ParseOutputAsUtf8StringError(#[source] FromUtf8Error),

    #[error(transparent)]
    IoError(#[from] io::Error),
}

/// The global `Result` alias of the library.
pub type Result<T> = result::Result<T, Error>;

/// The main command structure.
///
/// A command can be either a single command or a pipeline.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Cmd {
    /// The single command variant.
    SingleCmd(SingleCmd),

    /// The pipeline variant.
    Pipeline(Pipeline),
}

impl Cmd {
    /// Wrapper around [`alloc::str::replace`].
    ///
    /// This function is particularly useful when you need to replace
    /// placeholders on all inner commands.
    pub fn replace(mut self, from: impl AsRef<str>, to: impl AsRef<str>) -> Self {
        match &mut self {
            Self::SingleCmd(SingleCmd { cmd, .. }) => {
                *cmd = cmd.replace(from.as_ref(), to.as_ref())
            }
            Self::Pipeline(Pipeline(cmds)) => {
                for SingleCmd { cmd, .. } in cmds {
                    *cmd = cmd.replace(from.as_ref(), to.as_ref());
                }
            }
        }
        self
    }

    /// Runs the command without piped input.
    pub async fn run(&self) -> Result<CmdOutput> {
        self.run_with([]).await
    }

    /// Runs the command with the given piped input.
    pub async fn run_with(&self, input: impl AsRef<[u8]>) -> Result<CmdOutput> {
        debug!("running command: {}", self.to_string());

        match self {
            Self::SingleCmd(cmd) => cmd.run_with(input).await,
            Self::Pipeline(cmds) => cmds.run_with(input).await,
        }
    }
}

impl Default for Cmd {
    fn default() -> Self {
        Self::Pipeline(Pipeline::default())
    }
}

impl From<String> for Cmd {
    fn from(cmd: String) -> Self {
        Self::SingleCmd(cmd.into())
    }
}

impl From<&String> for Cmd {
    fn from(cmd: &String) -> Self {
        cmd.clone().into()
    }
}

impl From<&str> for Cmd {
    fn from(cmd: &str) -> Self {
        cmd.to_owned().into()
    }
}

impl From<Vec<String>> for Cmd {
    fn from(cmd: Vec<String>) -> Self {
        Self::Pipeline(cmd.into())
    }
}

impl From<Vec<&String>> for Cmd {
    fn from(cmd: Vec<&String>) -> Self {
        Self::Pipeline(cmd.into())
    }
}

impl From<Vec<&str>> for Cmd {
    fn from(cmd: Vec<&str>) -> Self {
        Self::Pipeline(cmd.into())
    }
}

impl From<&[String]> for Cmd {
    fn from(cmd: &[String]) -> Self {
        Self::Pipeline(cmd.into())
    }
}

impl From<&[&String]> for Cmd {
    fn from(cmd: &[&String]) -> Self {
        Self::Pipeline(cmd.into())
    }
}

impl From<&[&str]> for Cmd {
    fn from(cmd: &[&str]) -> Self {
        Self::Pipeline(cmd.into())
    }
}

impl ToString for Cmd {
    fn to_string(&self) -> String {
        match self {
            Self::SingleCmd(cmd) => cmd.to_string(),
            Self::Pipeline(pipeline) => pipeline.to_string(),
        }
    }
}

/// The single command structure.
///
/// Represents commands that are only composed of one single command.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SingleCmd {
    cmd: String,
    output_piped: bool,
}

impl SingleCmd {
    pub fn with_output_piped(mut self, piped: bool) -> Self {
        self.output_piped = piped;
        self
    }

    pub async fn run(&self) -> Result<CmdOutput> {
        self.run_with([]).await
    }

    /// Runs the single command with the given input.
    ///
    /// If the given input is empty, the command gets straight the
    /// output. Otherwise the commands pipes this input to the
    /// standard input channel then waits for the output on the
    /// standard output channel.
    pub async fn run_with(&self, input: impl AsRef<[u8]>) -> Result<CmdOutput> {
        let input = input.as_ref();

        let stdin = if input.is_empty() {
            Stdio::inherit()
        } else {
            Stdio::piped()
        };

        let stdout = || {
            if self.output_piped {
                Stdio::piped()
            } else {
                Stdio::inherit()
            }
        };

        let mut cmd = TOKIO_CMD;
        let mut cmd = cmd
            .arg(&self.cmd)
            .stdin(stdin)
            .stdout(stdout())
            .stderr(stdout())
            .spawn()?;

        if !input.is_empty() {
            cmd.stdin
                .as_mut()
                .ok_or(Error::GetStdinError)?
                .write_all(input)
                .await
                .map_err(Error::WriteStdinError)?;
        }

        let output = cmd
            .wait_with_output()
            .await
            .map_err(Error::GetOutputError)?;

        let code = output
            .status
            .code()
            .ok_or_else(|| Error::GetExitStatusCodeNotAvailableError(self.to_string()))?;

        if code != 0 {
            let cmd = self.to_string();
            let err = String::from_utf8_lossy(&output.stderr).to_string();
            return Err(Error::InvalidExitStatusCodeNonZeroError(cmd, code, err));
        }

        Ok(output.stdout.into())
    }
}

impl Deref for SingleCmd {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.cmd
    }
}

impl DerefMut for SingleCmd {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.cmd
    }
}

impl From<String> for SingleCmd {
    fn from(cmd: String) -> Self {
        Self {
            cmd,
            output_piped: true,
        }
    }
}

impl From<&String> for SingleCmd {
    fn from(cmd: &String) -> Self {
        cmd.as_str().into()
    }
}

impl From<&str> for SingleCmd {
    fn from(cmd: &str) -> Self {
        cmd.to_owned().into()
    }
}

impl ToString for SingleCmd {
    fn to_string(&self) -> String {
        self.cmd.clone()
    }
}

/// The command pipeline structure.
///
/// Represents commands that are composed of multiple single
/// commands. Commands are run in a pipeline, which means the output
/// of the previous command is piped to the input of the next one, and
/// so on.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Pipeline(Vec<SingleCmd>);

impl Pipeline {
    /// Runs the command pipeline with the given input.
    pub async fn run_with(&self, input: impl AsRef<[u8]>) -> Result<CmdOutput> {
        let mut output = input.as_ref().to_owned();

        for cmd in &self.0 {
            output = cmd.run_with(&output).await?.0;
        }

        Ok(output.into())
    }
}

impl Deref for Pipeline {
    type Target = Vec<SingleCmd>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Pipeline {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Vec<String>> for Pipeline {
    fn from(cmd: Vec<String>) -> Self {
        Self(cmd.into_iter().map(Into::into).collect())
    }
}

impl From<Vec<&String>> for Pipeline {
    fn from(cmd: Vec<&String>) -> Self {
        Self(cmd.into_iter().map(Into::into).collect())
    }
}

impl From<Vec<&str>> for Pipeline {
    fn from(cmd: Vec<&str>) -> Self {
        Self(cmd.into_iter().map(Into::into).collect())
    }
}

impl From<&[String]> for Pipeline {
    fn from(cmd: &[String]) -> Self {
        Self(cmd.iter().map(Into::into).collect())
    }
}

impl From<&[&String]> for Pipeline {
    fn from(cmd: &[&String]) -> Self {
        Self(cmd.iter().map(|cmd| (*cmd).into()).collect())
    }
}

impl From<&[&str]> for Pipeline {
    fn from(cmd: &[&str]) -> Self {
        Self(cmd.iter().map(|cmd| (*cmd).into()).collect())
    }
}

impl ToString for Pipeline {
    fn to_string(&self) -> String {
        self.0.iter().fold(String::new(), |s, cmd| {
            if s.is_empty() {
                cmd.to_string()
            } else {
                s + "|" + &cmd.to_string()
            }
        })
    }
}

/// Wrapper around command output.
///
/// The only role of this struct is to provide convenient functions to
/// export command output as string.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CmdOutput(Vec<u8>);

impl CmdOutput {
    /// Reads the command output as string lossy.
    pub fn to_string_lossy(&self) -> String {
        String::from_utf8_lossy(self).to_string()
    }
}

impl Deref for CmdOutput {
    type Target = Vec<u8>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for CmdOutput {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Vec<u8>> for CmdOutput {
    fn from(output: Vec<u8>) -> Self {
        Self(output)
    }
}

impl Into<Vec<u8>> for CmdOutput {
    fn into(self) -> Vec<u8> {
        self.0
    }
}

impl TryInto<String> for CmdOutput {
    type Error = Error;

    fn try_into(self) -> Result<String> {
        String::from_utf8(self.0).map_err(Error::ParseOutputAsUtf8StringError)
    }
}