mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Improve gh token auto scraping (#1746)
* Log the gh-auth token retrieval failure on debug level Fixed #1733 * Make gh_token::get an async function * Improve returned err msg in gh_token::get * Update use of gh_token::get() in entry.rs * Fix typos * Fix unclosed () * Fix unclosed () * Fix gh_token.rs * Fix entry.rs * Fix fmt in entry.rs * Fix fmt in gh_token.rs * Fix fmt in gh_token.rs * Fix fmt in gh_token.rs * Fix fmt in entry.rs
This commit is contained in:
parent
22217acc51
commit
cd85622b13
2 changed files with 45 additions and 24 deletions
|
@ -27,7 +27,7 @@ use file_format::FileFormat;
|
||||||
use home::cargo_home;
|
use home::cargo_home;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use miette::{miette, Report, Result, WrapErr};
|
use miette::{miette, Report, Result, WrapErr};
|
||||||
use tokio::task::block_in_place;
|
use tokio::{runtime::Handle, task::block_in_place};
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -82,6 +82,28 @@ pub fn install_crates(
|
||||||
// Launch target detection
|
// Launch target detection
|
||||||
let desired_targets = get_desired_targets(args.targets);
|
let desired_targets = get_desired_targets(args.targets);
|
||||||
|
|
||||||
|
// Launch scraping of gh token
|
||||||
|
let no_discover_github_token = args.no_discover_github_token;
|
||||||
|
let github_token = args.github_token.or_else(|| {
|
||||||
|
if args.no_discover_github_token {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
git_credentials::try_from_home()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let get_gh_token_task = (github_token.is_none() && !no_discover_github_token).then(|| {
|
||||||
|
AutoAbortJoinHandle::spawn(async move {
|
||||||
|
match gh_token::get().await {
|
||||||
|
Ok(token) => Some(token),
|
||||||
|
Err(err) => {
|
||||||
|
debug!(?err, "Failed to retrieve token from `gh auth token`");
|
||||||
|
debug!("Failed to read git credential file");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
// Computer cli_overrides
|
// Computer cli_overrides
|
||||||
let cli_overrides = PkgOverride {
|
let cli_overrides = PkgOverride {
|
||||||
pkg_url: args.pkg_url,
|
pkg_url: args.pkg_url,
|
||||||
|
@ -109,20 +131,11 @@ pub fn install_crates(
|
||||||
|
|
||||||
let gh_api_client = GhApiClient::new(
|
let gh_api_client = GhApiClient::new(
|
||||||
client.clone(),
|
client.clone(),
|
||||||
args.github_token.or_else(|| {
|
if let Some(task) = get_gh_token_task {
|
||||||
if args.no_discover_github_token {
|
Handle::current().block_on(task)?
|
||||||
None
|
|
||||||
} else {
|
} else {
|
||||||
git_credentials::try_from_home().or_else(|| match gh_token::get() {
|
github_token
|
||||||
Ok(token) => Some(token),
|
},
|
||||||
Err(err) => {
|
|
||||||
warn!(?err, "Failed to retrieve token from `gh auth token`");
|
|
||||||
warn!("Failed to read git credential file");
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create binstall_opts
|
// Create binstall_opts
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
use std::{io, process};
|
use std::{
|
||||||
|
io,
|
||||||
|
process::{Output, Stdio},
|
||||||
|
};
|
||||||
|
|
||||||
use compact_str::CompactString;
|
use compact_str::CompactString;
|
||||||
|
use tokio::process::Command;
|
||||||
|
|
||||||
pub(super) fn get() -> io::Result<CompactString> {
|
pub(super) async fn get() -> io::Result<CompactString> {
|
||||||
let process::Output { status, stdout, .. } = process::Command::new("gh")
|
let Output { status, stdout, .. } = Command::new("gh")
|
||||||
.args(["auth", "token"])
|
.args(["auth", "token"])
|
||||||
.stdin(process::Stdio::null())
|
.stdin(Stdio::null())
|
||||||
.stdout(process::Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(process::Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.output()?;
|
.output()
|
||||||
|
.await?;
|
||||||
|
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
|
@ -19,8 +24,11 @@ pub(super) fn get() -> io::Result<CompactString> {
|
||||||
|
|
||||||
// Use String here instead of CompactString here since
|
// Use String here instead of CompactString here since
|
||||||
// `CompactString::from_utf8` allocates if it's longer than 24B.
|
// `CompactString::from_utf8` allocates if it's longer than 24B.
|
||||||
let s = String::from_utf8(stdout).map_err(|_err| {
|
let s = String::from_utf8(stdout).map_err(|err| {
|
||||||
io::Error::new(io::ErrorKind::InvalidData, "Invalid output, expected utf8")
|
io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
format!("Invalid output, expected utf8: {err}"),
|
||||||
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(s.trim().into())
|
Ok(s.trim().into())
|
||||||
|
|
Loading…
Add table
Reference in a new issue