From 6e43f96294595a39e0ec7e67e4af4796d626df01 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Tue, 4 Jun 2024 19:31:20 +0200 Subject: [PATCH] feat: Add support for windows Also look for _netrc file in USERPROFILE --- src/lib.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 20ef2f0..566fbc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,8 @@ pub use netrc::{Authenticator, Netrc}; use std::fs; use std::io; use std::io::ErrorKind; +#[cfg(windows)] +use std::iter::repeat; use std::path::{Path, PathBuf}; use std::result; @@ -82,14 +84,21 @@ impl Netrc { /// Search a netrc file. /// - /// Look up the `NETRC` environment variable if it is defined else that the - /// default `$HOME/.netrc` file. + /// Look up the `NETRC` environment variable if it is defined else use the .netrc (or _netrc + /// file on windows) in the user's home directory. pub fn get_file() -> Option { - 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").map(PathBuf::from); + + #[cfg(windows)] + let default = std::env::var("USERPROFILE") + .into_iter() + .flat_map(|home| repeat(home).zip([".netrc", "_netrc"])) + .map(|(home, file)| PathBuf::from(home).join(file)); + + #[cfg(not(windows))] + let default = std::env::var("HOME").map(|home| PathBuf::from(home).join(".netrc")); + + env_var.into_iter().chain(default).find(|f| f.exists()) } }