Skip to content

Commit

Permalink
feat: Add support for windows
Browse files Browse the repository at this point in the history
Also look for _netrc file in USERPROFILE
  • Loading branch information
bschoenmaeckers committed Jun 4, 2024
1 parent e572bbf commit 3514677
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub use netrc::{Authenticator, Netrc};
use std::fs;
use std::io;
use std::io::ErrorKind;
use std::iter::repeat;
use std::path::{Path, PathBuf};
use std::result;

Expand Down Expand Up @@ -85,11 +86,15 @@ impl Netrc {
/// Look up the `NETRC` environment variable if it is defined else that the
/// default `$HOME/.netrc` file.
pub fn get_file() -> Option<PathBuf> {
std::env::var("NETRC")
.map(|x| PathBuf::from(&x))
.or(std::env::var("HOME").map(|h| Path::new(&h).join(".netrc")))
.ok()
.and_then(|f| if f.exists() { Some(f) } else { None })
let env_var = std::env::var("NETRC").ok().map(|file| PathBuf::from(file));

let user_home = ["HOME", "USERPROFILE"]
.iter()
.filter_map(|x| std::env::var(x).ok())
.flat_map(|home| repeat(home).zip([".netrc", "_netrc"]))
.map(|(home, file)| PathBuf::from(home).join(file));

env_var.into_iter().chain(user_home).find(|f| f.exists())
}
}

Expand Down

0 comments on commit 3514677

Please sign in to comment.