mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 12:38:43 +00:00

* 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
36 lines
851 B
Rust
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()))
|
|
}
|