mod.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #[cfg(target_os = "dragonos")]
  2. use drstd as std;
  3. use std::{process::Command, string::String};
  4. use crate::error::runtime_error::{RuntimeError, RuntimeErrorType};
  5. #[derive(Debug, Clone, Default)]
  6. pub struct CmdTask {
  7. pub path: String,
  8. pub cmd: Vec<String>,
  9. pub ignore: bool, //表示忽略这个命令的错误,即使它运行失败也不影响unit正常运作
  10. }
  11. impl CmdTask {
  12. pub fn exec(&self) -> Result<(), RuntimeError> {
  13. let result = Command::new(&self.path).args(&self.cmd).output();
  14. match result {
  15. Ok(output) => {
  16. if !output.status.success() && !self.ignore {
  17. let stderr = String::from_utf8_lossy(&output.stderr);
  18. eprintln!("{}: Command failed: {}", self.path, stderr);
  19. return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
  20. }
  21. }
  22. Err(err) => {
  23. if !self.ignore {
  24. eprintln!("{}: Command failed: {}", self.path, err);
  25. return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
  26. }
  27. }
  28. }
  29. Ok(())
  30. }
  31. }