Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for windows #3

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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").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())
}
}

Expand Down
Loading