< 返回我的博客

Borber 发表于 2021-12-16 17:52

Tags:小工具

use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::Result;
use crate::errors::{DaMieError, rs_error};

pub fn get_exe_path() -> Result<PathBuf> {
    let current = env::current_exe()?;
    match current.parent() {
        None => {
            Ok(current)
        }
        Some(path) => {
            Ok(path.to_path_buf())
        }
    }
}

pub fn get_path() -> Result<PathBuf> {
    Ok(env::current_dir()?)
}

pub fn run_command(program: &str, args: &Vec<&str>) -> Result<String, DaMieError> {
    let output = Command::new(program)
        .args(args)
        .output()?;
    if !output.status.success() {
        return Err(rs_error(format!("{}{}",program, args.join(" "))));
    };
    Ok(String::from_utf8_lossy(&*output.stdout).to_string())
}

评论区

写评论

还没有评论

1 共 0 条评论, 1 页