Skip to content

Commit

Permalink
Add cwd-accepting versions of cargo_home and rustup_home
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Jul 19, 2017
1 parent cf5dd41 commit 759a15b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "home"
version = "0.1.0"
version = "0.2.0"
authors = [ "Brian Anderson <andersrb@gmail.com>" ]
description = "Shared definitions of home directories"
documentation = "https://docs.rs/home"
Expand Down
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -177,6 +177,11 @@ fn home_dir_() -> Option<PathBuf> {
/// 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<PathBuf> {
let cwd = env::current_dir()?;
cargo_home_with_cwd(&cwd)
}

pub fn cargo_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
let env_var = env::var_os("CARGO_HOME");

// NB: During the multirust-rs -> rustup transition the install
Expand All @@ -198,7 +203,6 @@ pub fn cargo_home() -> io::Result<PathBuf> {
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"));
Expand Down Expand Up @@ -235,8 +239,12 @@ pub fn cargo_home() -> io::Result<PathBuf> {
/// 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<PathBuf> {
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<PathBuf> {
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"));
Expand Down

0 comments on commit 759a15b

Please sign in to comment.