Skip to content

Commit

Permalink
refactor: revert cmd_name logic & print more error info
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Sep 28, 2024
1 parent 96d7fa4 commit 53cb637
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/ops/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@ fn do_call(
return Ok(true);
}
let mut iter = command.iter();
let origin_cmd_name: &String = iter.next().unwrap();

// convert to absolute path if path is existed as first and avoid to use Command::current_dir
let cmd_name = path.map_or_else(
|| origin_cmd_name.clone(),
|p| {
dunce::canonicalize(p.join(origin_cmd_name))
.map(|path| path.to_string_lossy().into_owned())
.unwrap_or_else(|_| p.join(origin_cmd_name).to_string_lossy().into_owned())
},
);
let cmd_name = iter.next().unwrap();

let mut cmd = Command::new(&cmd_name);

if let Some(p) = path {
cmd.current_dir(p);
}

if let Some(e) = envs {
cmd.envs(e.iter());
}
Expand All @@ -44,12 +38,14 @@ fn do_call(
}
}

let ctx_dir = path.map_or(String::new(), |p| format!(" within {}", p.display()));

let mut child = cmd
.spawn()
.map_err(|e| anyhow::format_err!("failed to launch `{cmd_name}`: {e}"))?;
.map_err(|e| anyhow::format_err!("failed to launch `{cmd_name}{ctx_dir}`: {e}"))?;
let result = child
.wait()
.map_err(|e| anyhow::format_err!("failed to launch `{cmd_name}`: {e}"))?;
.map_err(|e| anyhow::format_err!("failed to launch `{cmd_name}{ctx_dir}`: {e}"))?;

Ok(result.success())
}
Expand Down

0 comments on commit 53cb637

Please sign in to comment.