return a PathBuf instead of String

This commit is contained in:
Craig Disselkoen 2020-05-06 12:39:11 -07:00
parent 1b76b4281e
commit 44b01ccff3
2 changed files with 9 additions and 8 deletions

View file

@ -4,15 +4,15 @@
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use std::env; use std::env;
use std::path::Path; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
/// Return a `String` to use for the given executable. /// Return a `PathBuf` to use for the given executable.
/// ///
/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that /// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
/// gives a valid Cargo executable; or it may return a full path to a valid /// gives a valid Cargo executable; or it may return a full path to a valid
/// Cargo. /// Cargo.
pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<String> { pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<PathBuf> {
// The current implementation checks three places for an executable to use: // The current implementation checks three places for an executable to use:
// 1) Appropriate environment variable (erroring if this is set but not a usable executable) // 1) Appropriate environment variable (erroring if this is set but not a usable executable)
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
@ -25,7 +25,7 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
let env_var = executable_name.to_ascii_uppercase(); let env_var = executable_name.to_ascii_uppercase();
if let Ok(path) = env::var(&env_var) { if let Ok(path) = env::var(&env_var) {
if is_valid_executable(&path) { if is_valid_executable(&path) {
Ok(path) Ok(path.into())
} else { } else {
Err(Error::msg(format!( Err(Error::msg(format!(
"`{}` environment variable points to something that's not a valid executable", "`{}` environment variable points to something that's not a valid executable",
@ -34,14 +34,14 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
} }
} else { } else {
if is_valid_executable(executable_name) { if is_valid_executable(executable_name) {
return Ok(executable_name.to_owned()); return Ok(executable_name.into());
} }
if let Some(mut path) = dirs::home_dir() { if let Some(mut path) = dirs::home_dir() {
path.push(".cargo"); path.push(".cargo");
path.push("bin"); path.push("bin");
path.push(executable_name); path.push(executable_name);
if is_valid_executable(&path) { if is_valid_executable(&path) {
return Ok(path.into_os_string().into_string().expect("Invalid Unicode in path")); return Ok(path);
} }
} }
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly // This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly

View file

@ -89,9 +89,10 @@ fn create_command_text(program: &str, args: &[&str]) -> String {
format!("{} {}", program, args.join(" ")) format!("{} {}", program, args.join(" "))
} }
fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> { fn run_command_in_cargo_dir(cargo_toml: impl AsRef<Path>, program: impl AsRef<Path>, args: &[&str]) -> Result<Output> {
let program = program.as_ref().as_os_str().to_str().expect("Invalid Unicode in path");
let output = Command::new(program) let output = Command::new(program)
.current_dir(cargo_toml.parent().unwrap()) .current_dir(cargo_toml.as_ref().parent().unwrap())
.args(args) .args(args)
.output() .output()
.context(format!("{} failed", create_command_text(program, args)))?; .context(format!("{} failed", create_command_text(program, args)))?;