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
use crate::error::{Error, NeverError};
use crate::utils::{spawn, PANIC_MSG};
use std::ffi::OsStr;
use std::process::{Child, Output};

/// Executes command with args and environment variables, ignores output
/// * On invalid command: do nothing
/// * On error exit code: do nothing
/// * On parsing failure: N/A
/// * Possible errors: N/A
///
/// Designed for
/// ```rust
/// use shellfn::shell;
///
/// #[shell(no_panic)]
/// fn command() {
///     "echo Hello, world"
/// }
///
/// command()
/// ```
pub fn execute_void_nopanic<TArg, TEnvKey, TEnvVal>(
    cmd: impl AsRef<OsStr>,
    args: impl IntoIterator<Item = TArg>,
    envs: impl IntoIterator<Item = (TEnvKey, TEnvVal)>,
) where
    TArg: AsRef<OsStr>,
    TEnvKey: AsRef<OsStr>,
    TEnvVal: AsRef<OsStr>,
{
    let _ = spawn(cmd, args, envs).and_then(Child::wait_with_output);
}

/// Executes command with args and environment variables, ignores output
/// * On invalid command: panic
/// * On error exit code: panic
/// * On parsing failure: N/A
/// * Possible errors: N/A
///
/// Designed for
/// ```rust
/// use shellfn::shell;
///
/// #[shell]
/// fn command() {
///     "echo Hello, world"
/// }
///
/// command()
/// ```
pub fn execute_void_panic<TArg, TEnvKey, TEnvVal>(
    cmd: impl AsRef<OsStr>,
    args: impl IntoIterator<Item = TArg>,
    envs: impl IntoIterator<Item = (TEnvKey, TEnvVal)>,
) where
    TArg: AsRef<OsStr>,
    TEnvKey: AsRef<OsStr>,
    TEnvVal: AsRef<OsStr>,
{
    let output = spawn(cmd, args, envs)
        .and_then(Child::wait_with_output)
        .expect(PANIC_MSG);

    if !output.status.success() {
        panic!(PANIC_MSG)
    }
}

/// Executes command with args and environment variables, ignores output
/// * On invalid command: return error
/// * On error exit code: return error
/// * On parsing failure: N/A
/// * Possible errors: ProcessNotSpawned, WaitFailed, ProcessFailed (stdout and stderr always empty)
///
/// Designed for
/// ```rust
/// use shellfn::shell;
///
/// #[shell]
/// fn command() -> Result<(), Box<Error>> {
///     sleep 5
/// }
///
/// command()
/// ```
pub fn execute_void_result<TArg, TEnvKey, TEnvVal, TError>(
    cmd: impl AsRef<OsStr>,
    args: impl IntoIterator<Item = TArg>,
    envs: impl IntoIterator<Item = (TEnvKey, TEnvVal)>,
) -> Result<(), TError>
where
    TArg: AsRef<OsStr>,
    TEnvKey: AsRef<OsStr>,
    TEnvVal: AsRef<OsStr>,
    TError: From<Error<NeverError>>,
{
    let mut process = spawn(cmd, args, envs).map_err(Error::ProcessNotSpawned)?;
    let status = process.wait().map_err(Error::WaitFailed)?;

    if !status.success() {
        return Err(Error::ProcessFailed(Output {
            status,
            stdout: Vec::new(),
            stderr: Vec::new(),
        }))?;
    }

    Ok(())
}