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
#[macro_use]
extern crate wei_log;

#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;

/// Run wei command, If the program does not exist/ Under the data/directory, search for the program's configuration file
/// # Arguments
/// * `cmd` - Command name
/// * `param` - Command parameters
pub fn run(cmd: &str, param: Vec<&str>) -> Result<String, Box<dyn std::error::Error>> {
    let path = "./".to_owned() + cmd;

    info!("path: {:?}", path);

    if let Ok(data) = command(&path, param.clone()) {
        return Ok(data);
    };

    info!("{} dir: {:?}", cmd, wei_env::dir_bin());
    let path = wei_env::read(&wei_env::dir_bin(),cmd)?;
    command(path.as_str(), param)
}

/// Run command
/// # Arguments
/// * `cmd` - Command name
/// * `param` - Command parameters
pub fn command(cmd: &str, param: Vec<&str>) -> Result<String, Box<dyn std::error::Error>> {
    #[cfg(target_os = "windows")]
    let output = std::process::Command::new(cmd)
    .args(param)
    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW)
    .output()?;

    #[cfg(not(target_os = "windows"))]
    let output = std::process::Command::new(cmd)
    .args(param).output()?;

    let data = format!("{}{}", 
        std::str::from_utf8(&output.stdout)?, 
        std::str::from_utf8(&output.stderr)?
    );

    Ok(data)
}

/// Run wei command, If the program does not exist/ Under the data/directory, search for the program's configuration file
/// # Arguments
/// * `cmd` - Command name
/// * `param` - Command parameters
pub fn run_async(cmd: &str, param: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> {
    let path = "./".to_owned() + cmd;

    info!("path: {:?}", path);

    if let Ok(()) = command_async(&path, param.clone()) {
        return Ok(());
    };

    info!("{} dir: {:?}", cmd, wei_env::dir_bin());
    let path = wei_env::read(&wei_env::dir_bin(),cmd)?;
    command_async(path.as_str(), param)
}

pub fn command_async(cmd: &str, param: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> {
    #[cfg(target_os = "windows")]
    std::process::Command::new(cmd)
    .args(param)
    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW)
    .spawn()?;

    #[cfg(not(target_os = "windows"))]
    std::process::Command::new(cmd)
    .args(param)
    .spawn()?;

    Ok(())
}

pub fn kill(name: &str) -> Result<(), Box<dyn std::error::Error>> {
    #[cfg(target_os = "windows")]
    {
        // let mut cmd = Command::new("cmd");
        // cmd.arg("/C").arg(format!("taskkill /IM {}.exe /F", name));
        // cmd.output()?;
        psrun("wei-close.ps1", name)?;
    }

    #[cfg(target_os = "linux")]
    {
        let mut cmd = Command::new("bash");
        cmd.arg("-c").arg(format!("pkill {}", name));
        cmd.output()?;
    }
    Ok(())
}

pub fn psrun(name: &str, param: &str) -> Result<(), Box<dyn std::error::Error>> {
    std::process::Command::new("powershell")
    .arg("-ExecutionPolicy").arg("Bypass")
    .arg("-File").arg(name).arg(param)
    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW).output()?;

    Ok(())
}