mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-01 17:50:03 +00:00
Fix GitHub token auto discovery (#1335)
* Fix GitHub token auto discovery Fixed #1333 - Rm dep `gh-token` since it is broken and we can simply run `gh auth token` in `cargo-binstall` instead. - binstalk-downloader: Make sure GitHub token is at least 40B long and other than the `_`, composes of only alphanumeric characters. - Warn on failure to read `git/credential` files - Optimize `try_from_home` to avoid heap allocation of `PathBuf` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix typo and clippy Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Simplify `is_valid_gh_token` & `is_ascii_alphanumeric` impl Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Improve err msg in `get_inner` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Improve err msg of `cargo_binstall::gh_token::get` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> --------- Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
0ca38ab0e3
commit
8a08cdda6f
8 changed files with 85 additions and 53 deletions
|
@ -1,10 +1,8 @@
|
|||
use std::{
|
||||
env, fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{env, fs, path::PathBuf};
|
||||
|
||||
use compact_str::CompactString;
|
||||
use dirs::home_dir;
|
||||
use tracing::warn;
|
||||
|
||||
pub fn try_from_home() -> Option<CompactString> {
|
||||
if let Some(mut home) = home_dir() {
|
||||
|
@ -15,7 +13,9 @@ pub fn try_from_home() -> Option<CompactString> {
|
|||
}
|
||||
|
||||
if let Some(home) = env::var_os("XDG_CONFIG_HOME") {
|
||||
let home = Path::new(&home).join("git/credentials");
|
||||
let mut home = PathBuf::from(home);
|
||||
home.push("git/credentials");
|
||||
|
||||
if let Some(cred) = from_file(home) {
|
||||
return Some(cred);
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ pub fn try_from_home() -> Option<CompactString> {
|
|||
}
|
||||
|
||||
fn from_file(path: PathBuf) -> Option<CompactString> {
|
||||
fs::read_to_string(path)
|
||||
.ok()?
|
||||
read_cred_file(path)?
|
||||
.lines()
|
||||
.find_map(from_line)
|
||||
.map(CompactString::from)
|
||||
|
@ -41,6 +40,20 @@ fn from_line(line: &str) -> Option<&str> {
|
|||
Some(cred.split_once(':')?.1)
|
||||
}
|
||||
|
||||
fn read_cred_file(path: PathBuf) -> Option<String> {
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(s) => Some(s),
|
||||
Err(err) => {
|
||||
warn!(
|
||||
?err,
|
||||
"Failed to read git credential file {}",
|
||||
path.display()
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue