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
use std::{
    env,
    io::Write,
    process::{self, Command, Stdio},
};

use crate::{error::PsError, output::Output, Result, POWERSHELL_NAME};

const PATH_SPLITTER: char = ':';

pub struct PsScript {
    pub(crate) args: Vec<&'static str>,
    pub(crate) hidden: bool,
    pub(crate) print_commands: bool,
}

impl PsScript {
    pub fn run(&self, script: &str) -> Result<Output> {
        let proc_output = self.run_raw(script)?;

        let output = Output::from(proc_output);
        if output.success {
            Ok(output)
        } else {
            Err(PsError::Powershell(output))
        }
    }

    fn run_raw(&self, script: &str) -> Result<process::Output> {
        let mut cmd = Command::new(get_powershell_path()?);

        cmd.stdin(Stdio::piped());
        cmd.stdout(Stdio::piped());
        cmd.stderr(Stdio::piped());

        cmd.args(&self.args);

        if self.hidden {
            // TODO: Check if this is a problem in PS Core on Unix platforms
            // See: https://github.com/cfsamson/powershell-script/pull/9
        }

        let mut process = cmd.spawn()?;
        let stdin = process.stdin.as_mut().ok_or(PsError::ChildStdinNotFound)?;

        for line in script.lines() {
            if self.print_commands {
                println!("{}", line)
            };
            writeln!(stdin, "{}", line)?;
        }

        let output = process.wait_with_output()?;
        Ok(output)
    }
}

/// Check whether there is a program called "program name" on the system path
fn is_program_on_path(program_name: &str) -> Option<bool> {
    let system_path = match env::var("PATH") {
        Ok(x) => x,
        Err(_e) => return None,
    };

    for path_dir in system_path.split(PATH_SPLITTER) {
        let path = std::path::Path::new(path_dir).join(&program_name);
        if path.exists() {
            return Some(true);
        }
    }
    return Some(false);
}

fn get_powershell_path() -> Result<String> {
    if is_program_on_path(POWERSHELL_NAME).unwrap() {
        Ok(POWERSHELL_NAME.to_string())
    } else {
        Err(PsError::PowershellNotFound)
    }
}