From 759a15b8aeb3a23ec31c7a87db919bdd33b13739 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 18 Jul 2017 17:10:07 -0700 Subject: [PATCH] Add cwd-accepting versions of cargo_home and rustup_home --- Cargo.toml | 2 +- src/lib.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aa5a4b7a22d..d2eed3e1de4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "home" -version = "0.1.0" +version = "0.2.0" authors = [ "Brian Anderson " ] description = "Shared definitions of home directories" documentation = "https://docs.rs/home" diff --git a/src/lib.rs b/src/lib.rs index 509577b56e0..49906084a5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ extern crate userenv; #[cfg(windows)] use winapi::DWORD; -use std::path::PathBuf; +use std::path::{PathBuf, Path}; use std::io; use std::env; @@ -177,6 +177,11 @@ fn home_dir_() -> Option { /// This function fails if it fails to retrieve the current directory, /// or if the home directory cannot be determined. pub fn cargo_home() -> io::Result { + let cwd = env::current_dir()?; + cargo_home_with_cwd(&cwd) +} + +pub fn cargo_home_with_cwd(cwd: &Path) -> io::Result { let env_var = env::var_os("CARGO_HOME"); // NB: During the multirust-rs -> rustup transition the install @@ -198,7 +203,6 @@ pub fn cargo_home() -> io::Result { None }; - let cwd = env::current_dir()?; let env_cargo_home = env_var.map(|home| cwd.join(home)); let home_dir = home_dir() .ok_or(io::Error::new(io::ErrorKind::Other, "couldn't find home dir")); @@ -235,8 +239,12 @@ pub fn cargo_home() -> io::Result { /// This function fails if it fails to retrieve the current directory, /// or if the home directory cannot be determined. pub fn rustup_home() -> io::Result { - let env_var = env::var_os("RUSTUP_HOME"); let cwd = env::current_dir()?; + rustup_home_with_cwd(&cwd) +} + +pub fn rustup_home_with_cwd(cwd: &Path) -> io::Result { + let env_var = env::var_os("RUSTUP_HOME"); let env_rustup_home = env_var.map(|home| cwd.join(home)); let home_dir = home_dir() .ok_or(io::Error::new(io::ErrorKind::Other, "couldn't find home dir"));