cargo-binstall/crates/bin/src/gh_token.rs
Jiahao XU d76a40bf7d
Rm unnecessary warning msg "Failed to read git credential file" (#1479)
* Rm unnecessary warning msg "Failed to read git credential file"

Fixed #1476

If `gh auth token` executed successfully and binstall obtained a gh
token from it, then there's no reason to issue any warning msg.

Only when binstall cannot read from `.git-credential` and
`gh auth token` failed does binstall need to issue warning.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix clippy warning

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

---------

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-11-08 22:06:32 +00:00

27 lines
833 B
Rust

use std::{io, process};
use compact_str::CompactString;
pub(super) fn get() -> 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())
}