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
use std::process::Command;

/// Shell to use to run commands.
///
/// `Cmd` and `Powershell` are special-cased because they have different calling
/// conventions. Also `Cmd` is only available in Windows, while `Powershell` is
/// also available on unices (provided the end-user has it installed, of course).
///
/// See [`Config.cmd`][crate::config::Config] for the semantics of `None` vs the
/// other options.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Shell {
    /// Use no shell, and execute the command directly.
    None,

    /// Use the given string as a unix shell invocation.
    ///
    /// This means two things:
    /// - the program is invoked with `-c` followed by the command, and
    /// - the string will be split on space, and the resulting vec used as
    ///   execvp(3) arguments: first is the shell program, rest are additional
    ///   arguments (which come before the `-c` mentioned above). This is a very
    ///   simplistic approach deliberately: it will not support quoted
    ///   arguments, for example. Use [`Shell::None`] with a custom command vec
    ///   if you want that.
    Unix(String),

    /// Use the Windows CMD.EXE shell.
    ///
    /// This is invoked with `/C` followed by the command.
    #[cfg(windows)]
    Cmd,

    /// Use Powershell, on Windows or elsewhere.
    ///
    /// This is invoked with `-Command` followed by the command.
    ///
    /// This is preferred over `Unix("pwsh")`, though that will also work
    /// on unices due to Powershell supporting the `-c` short option.
    Powershell,
}

impl Default for Shell {
    #[cfg(windows)]
    fn default() -> Self {
        Self::Powershell
    }

    #[cfg(not(windows))]
    fn default() -> Self {
        Self::Unix("sh".into())
    }
}

impl Shell {
    /// Obtain a [`Command`] given the cmd vec from [`Config`][crate::config::Config].
    ///
    /// Behaves as described in the enum documentation.
    ///
    /// # Panics
    ///
    /// - Panics if `cmd` is empty.
    /// - Panics if the string in the `Unix` variant is empty or only whitespace.
    pub fn to_command(&self, cmd: &[String]) -> Command {
        assert!(!cmd.is_empty(), "cmd was empty");

        match self {
            Shell::None => {
                // UNWRAP: checked by assert
                #[allow(clippy::unwrap_used)]
                let (first, rest) = cmd.split_first().unwrap();
                let mut c = Command::new(first);
                c.args(rest);
                c
            }

            #[cfg(windows)]
            Shell::Cmd => {
                let mut c = Command::new("cmd.exe");
                c.arg("/C").arg(cmd.join(" "));
                c
            }

            Shell::Powershell if cfg!(windows) => {
                let mut c = Command::new("powershell.exe");
                c.arg("-Command").arg(cmd.join(" "));
                c
            }

            Shell::Powershell => {
                let mut c = Command::new("pwsh");
                c.arg("-Command").arg(cmd.join(" "));
                c
            }

            Shell::Unix(name) => {
                assert!(!name.is_empty(), "shell program was empty");
                let sh = name.split_ascii_whitespace().collect::<Vec<_>>();

                // UNWRAP: checked by assert
                #[allow(clippy::unwrap_used)]
                let (shprog, shopts) = sh.split_first().unwrap();

                let mut c = Command::new(shprog);
                c.args(shopts);
                c.arg("-c").arg(cmd.join(" "));
                c
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::Shell;
    use command_group::CommandGroup;

    #[test]
    #[cfg(unix)]
    fn unix_shell_default() -> Result<(), std::io::Error> {
        assert!(Shell::default()
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(unix)]
    fn unix_shell_none() -> Result<(), std::io::Error> {
        assert!(Shell::None
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(unix)]
    fn unix_shell_alternate() -> Result<(), std::io::Error> {
        assert!(Shell::Unix("bash".into())
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(unix)]
    fn unix_shell_alternate_shopts() -> Result<(), std::io::Error> {
        assert!(Shell::Unix("bash -o errexit".into())
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(windows)]
    fn windows_shell_default() -> Result<(), std::io::Error> {
        assert!(Shell::default()
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(windows)]
    fn windows_shell_cmd() -> Result<(), std::io::Error> {
        assert!(Shell::Cmd
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(windows)]
    fn windows_shell_powershell() -> Result<(), std::io::Error> {
        assert!(Shell::Powershell
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }

    #[test]
    #[cfg(windows)]
    fn windows_shell_unix_style_powershell() -> Result<(), std::io::Error> {
        assert!(Shell::Unix("powershell.exe".into())
            .to_command(&["echo".into(), "hi".into()])
            .group_status()?
            .success());
        Ok(())
    }
}