Skip to content

Commit 6e43f96

Browse files
feat: Add support for windows
Also look for _netrc file in USERPROFILE
1 parent e572bbf commit 6e43f96

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/lib.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub use netrc::{Authenticator, Netrc};
3333
use std::fs;
3434
use std::io;
3535
use std::io::ErrorKind;
36+
#[cfg(windows)]
37+
use std::iter::repeat;
3638
use std::path::{Path, PathBuf};
3739
use std::result;
3840

@@ -82,14 +84,21 @@ impl Netrc {
8284

8385
/// Search a netrc file.
8486
///
85-
/// Look up the `NETRC` environment variable if it is defined else that the
86-
/// default `$HOME/.netrc` file.
87+
/// Look up the `NETRC` environment variable if it is defined else use the .netrc (or _netrc
88+
/// file on windows) in the user's home directory.
8789
pub fn get_file() -> Option<PathBuf> {
88-
std::env::var("NETRC")
89-
.map(|x| PathBuf::from(&x))
90-
.or(std::env::var("HOME").map(|h| Path::new(&h).join(".netrc")))
91-
.ok()
92-
.and_then(|f| if f.exists() { Some(f) } else { None })
90+
let env_var = std::env::var("NETRC").map(PathBuf::from);
91+
92+
#[cfg(windows)]
93+
let default = std::env::var("USERPROFILE")
94+
.into_iter()
95+
.flat_map(|home| repeat(home).zip([".netrc", "_netrc"]))
96+
.map(|(home, file)| PathBuf::from(home).join(file));
97+
98+
#[cfg(not(windows))]
99+
let default = std::env::var("HOME").map(|home| PathBuf::from(home).join(".netrc"));
100+
101+
env_var.into_iter().chain(default).find(|f| f.exists())
93102
}
94103
}
95104

0 commit comments

Comments
 (0)