From d76a40bf7d41edd6e1b4a03f9f415d21bf6039e8 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 9 Nov 2023 08:06:32 +1000 Subject: [PATCH] Rm unnecessary warning msg "Failed to read git credential file" (#1479) * Rm unnecessary warning msg "Failed to read git credential file" Fixed #1476 If `gh auth token` executed successfully and binstall obtained a gh token from it, then there's no reason to issue any warning msg. Only when binstall cannot read from `.git-credential` and `gh auth token` failed does binstall need to issue warning. Signed-off-by: Jiahao XU * Fix clippy warning Signed-off-by: Jiahao XU --------- Signed-off-by: Jiahao XU --- crates/bin/src/entry.rs | 9 ++++++++- crates/bin/src/gh_token.rs | 13 +------------ crates/bin/src/git_credentials.rs | 18 ++---------------- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/crates/bin/src/entry.rs b/crates/bin/src/entry.rs index d38ec5b0f..304bab659 100644 --- a/crates/bin/src/entry.rs +++ b/crates/bin/src/entry.rs @@ -114,7 +114,14 @@ pub fn install_crates( if args.no_discover_github_token { None } else { - git_credentials::try_from_home().or_else(gh_token::get) + git_credentials::try_from_home().or_else(|| match gh_token::get() { + Ok(token) => Some(token), + Err(err) => { + warn!(?err, "Failed to retrieve token from `gh auth token`"); + warn!("Failed to read git credential file"); + None + } + }) } }), ); diff --git a/crates/bin/src/gh_token.rs b/crates/bin/src/gh_token.rs index 3cfb3563b..172070a64 100644 --- a/crates/bin/src/gh_token.rs +++ b/crates/bin/src/gh_token.rs @@ -1,9 +1,8 @@ use std::{io, process}; use compact_str::CompactString; -use tracing::warn; -fn get_inner() -> io::Result { +pub(super) fn get() -> io::Result { let process::Output { status, stdout, .. } = process::Command::new("gh") .args(["auth", "token"]) .stdin(process::Stdio::null()) @@ -26,13 +25,3 @@ fn get_inner() -> io::Result { Ok(s.trim().into()) } - -pub(super) fn get() -> Option { - match get_inner() { - Ok(token) => Some(token), - Err(err) => { - warn!(?err, "Failed to retrieve token from `gh auth token`"); - None - } - } -} diff --git a/crates/bin/src/git_credentials.rs b/crates/bin/src/git_credentials.rs index 613b3732d..d2e420f7a 100644 --- a/crates/bin/src/git_credentials.rs +++ b/crates/bin/src/git_credentials.rs @@ -2,7 +2,6 @@ use std::{env, fs, path::PathBuf}; use compact_str::CompactString; use dirs::home_dir; -use tracing::warn; pub fn try_from_home() -> Option { if let Some(mut home) = home_dir() { @@ -25,7 +24,8 @@ pub fn try_from_home() -> Option { } fn from_file(path: PathBuf) -> Option { - read_cred_file(path)? + fs::read_to_string(path) + .ok()? .lines() .find_map(from_line) .map(CompactString::from) @@ -40,20 +40,6 @@ fn from_line(line: &str) -> Option<&str> { Some(cred.split_once(':')?.1) } -fn read_cred_file(path: PathBuf) -> Option { - 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::*;