mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-16 08:50:02 +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
crates/bin/src
38
crates/bin/src/gh_token.rs
Normal file
38
crates/bin/src/gh_token.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use std::{io, process};
|
||||
|
||||
use compact_str::CompactString;
|
||||
use tracing::warn;
|
||||
|
||||
fn get_inner() -> io::Result<CompactString> {
|
||||
let process::Output { status, stdout, .. } = process::Command::new("gh")
|
||||
.args(["auth", "token"])
|
||||
.stdin(process::Stdio::null())
|
||||
.stdout(process::Stdio::piped())
|
||||
.stderr(process::Stdio::null())
|
||||
.output()?;
|
||||
|
||||
if !status.success() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("process exited with `{status}`"),
|
||||
));
|
||||
}
|
||||
|
||||
// Use String here instead of CompactString here since
|
||||
// `CompactString::from_utf8` allocates if it's longer than 24B.
|
||||
let s = String::from_utf8(stdout).map_err(|_err| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, "Invalid output, expected utf8")
|
||||
})?;
|
||||
|
||||
Ok(s.trim().into())
|
||||
}
|
||||
|
||||
pub(super) fn get() -> Option<CompactString> {
|
||||
match get_inner() {
|
||||
Ok(token) => Some(token),
|
||||
Err(err) => {
|
||||
warn!(?err, "Failed to retrieve token from `gh auth token`");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue