wei_run/
lib.rs

1#[macro_use]
2extern crate wei_log;
3
4#[cfg(target_os = "windows")]
5use std::os::windows::process::CommandExt;
6
7/// Run wei command, If the program does not exist/ Under the data/directory, search for the program's configuration file
8/// # Arguments
9/// * `cmd` - Command name
10/// * `param` - Command parameters
11pub fn run(cmd: &str, param: Vec<&str>) -> Result<String, Box<dyn std::error::Error>> {
12    let path = "./".to_owned() + cmd;
13
14    info!("run: {:?}, param: {:?}", path, param);
15
16    if let Ok(data) = command(&path, param.clone()) {
17        return Ok(data);
18    };
19
20    info!("{} dir: {:?}", cmd, wei_env::dir_bin());
21    let path = wei_env::read(&wei_env::dir_bin(),cmd)?;
22    command(path.as_str(), param)
23}
24
25/// Run command
26/// # Arguments
27/// * `cmd` - Command name
28/// * `param` - Command parameters
29pub fn command(cmd: &str, param: Vec<&str>) -> Result<String, Box<dyn std::error::Error>> {
30    info!("wei-run command: {:?}, param: {:?}", cmd, param);
31    #[cfg(target_os = "windows")]
32    let output = std::process::Command::new(cmd)
33    .args(param)
34    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW)
35    .output()?;
36
37    #[cfg(not(target_os = "windows"))]
38    let output = std::process::Command::new(cmd)
39    .args(param).output()?;
40
41    let data = format!("{}{}", 
42        std::str::from_utf8(&output.stdout)?, 
43        std::str::from_utf8(&output.stderr)?
44    );
45
46    Ok(data)
47}
48
49/// Run command_output
50/// # Arguments
51/// * `cmd` - Command name
52/// * `param` - Command parameters
53pub fn command_output(cmd: &str, param: Vec<&str>) -> Result<std::process::Output, Box<dyn std::error::Error>> {
54    #[cfg(target_os = "windows")]
55    let output = std::process::Command::new(cmd)
56    .args(param)
57    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW)
58    .output()?;
59
60    #[cfg(not(target_os = "windows"))]
61    let output = std::process::Command::new(cmd)
62    .args(param).output()?;
63
64    Ok(output)
65}
66
67/// Run wei command, If the program does not exist/ Under the data/directory, search for the program's configuration file
68/// # Arguments
69/// * `cmd` - Command name
70/// * `param` - Command parameters
71pub fn run_async(cmd: &str, param: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> {
72    let path = "./".to_owned() + cmd;
73
74    info!("run_async: {:?}, param: {:?}", path, param);
75
76    if let Ok(()) = command_async(&path, param.clone()) {
77        return Ok(());
78    };
79
80    info!("{} dir: {:?}", cmd, wei_env::dir_bin());
81    let path = wei_env::read(&wei_env::dir_bin(),cmd)?;
82    command_async(path.as_str(), param)
83}
84
85pub fn command_async(cmd: &str, param: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> {
86    #[cfg(target_os = "windows")]
87    std::process::Command::new(cmd)
88    .args(param)
89    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW)
90    .spawn()?;
91
92    #[cfg(not(target_os = "windows"))]
93    std::process::Command::new(cmd)
94    .args(param)
95    .spawn()?;
96
97    Ok(())
98}
99
100pub fn kill(name: &str) -> Result<(), Box<dyn std::error::Error>> {
101    #[cfg(target_os = "windows")]
102    {
103        // let mut cmd = Command::new("cmd");
104        // cmd.arg("/C").arg(format!("taskkill /IM {}.exe /F", name));
105        // cmd.output()?;
106        psrun("wei-close.ps1", name)?;
107    }
108
109    #[cfg(target_os = "linux")]
110    {
111        let output = std::process::Command::new("bash")
112            .arg("-c")
113            .arg(format!("pgrep -x {} | head -n 1", name))
114            .output()?;
115
116        let pid = String::from_utf8(output.stdout)?.trim().to_string();
117
118        if !pid.is_empty() {
119            let _ = std::process::Command::new("bash")
120                .arg("-c")
121                .arg(format!("kill {}", pid))
122                .output()?;
123        }
124    }
125    Ok(())
126}
127
128#[cfg(target_os = "windows")]
129pub fn psrun(name: &str, param: &str) -> Result<(), Box<dyn std::error::Error>> {
130    std::process::Command::new("powershell")
131    .arg("-ExecutionPolicy").arg("Bypass")
132    .arg("-File").arg(name).arg(param)
133    .creation_flags(winapi::um::winbase::CREATE_NO_WINDOW).output()?;
134
135    Ok(())
136}