cargo-binstall/crates/bin/src/gh_token.rs
Jiahao XU fff6aa8122
Improve use of github token (#1769)
* Add new dep zeroize

* Use Zeroizing to avoid leaking the token

* Optimize gh-auth-token

Spawn it as a task, and only await it
when using GhApiClient

* Fix binstalk-git-repo-api unit tests
2024-06-15 05:42:09 +00:00

36 lines
851 B
Rust

use std::{
io,
process::{Output, Stdio},
str,
};
use tokio::process::Command;
use zeroize::Zeroizing;
pub(super) async fn get() -> io::Result<Zeroizing<Box<str>>> {
let Output { status, stdout, .. } = Command::new("gh")
.args(["auth", "token"])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.output()
.await?;
let stdout = Zeroizing::new(stdout);
if !status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("process exited with `{status}`"),
));
}
let s = str::from_utf8(&stdout).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Invalid output, expected utf8: {err}"),
)
})?;
Ok(Zeroizing::new(s.trim().into()))
}