easy_install/
gh_token.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::{
    io,
    process::{Output, Stdio},
    str,
};

use tokio::process::Command;
use zeroize::Zeroizing;

pub 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()))
}