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:
Jiahao XU 2024-06-13 20:50:47 +10:00 committed by GitHub
parent 22217acc51
commit cd85622b13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 24 deletions

View file

@ -1,14 +1,19 @@
use std::{io, process};
use std::{
io,
process::{Output, Stdio},
};
use compact_str::CompactString;
use tokio::process::Command;
pub(super) fn get() -> io::Result<CompactString> {
let process::Output { status, stdout, .. } = process::Command::new("gh")
pub(super) async fn get() -> io::Result<CompactString> {
let Output { status, stdout, .. } = Command::new("gh")
.args(["auth", "token"])
.stdin(process::Stdio::null())
.stdout(process::Stdio::piped())
.stderr(process::Stdio::null())
.output()?;
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.output()
.await?;
if !status.success() {
return Err(io::Error::new(
@ -19,8 +24,11 @@ pub(super) fn get() -> io::Result<CompactString> {
// 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")
let s = String::from_utf8(stdout).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Invalid output, expected utf8: {err}"),
)
})?;
Ok(s.trim().into())