Extract GitHub token from file if --github-token or env variable GITHUB_TOKEN is not present (#849)

- Add option `--no-discover-github-token` for disabling this behavior
 - Add new dep gh-token v0.1.0 to crates/bin
 - Extract github-token from git-credentials or gh config if `--github-token` or
    environment variable `GITHUB_TOKEN` is not present.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-03-03 15:32:50 +11:00 committed by GitHub
parent 75289cc2b4
commit 44ac63ce0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 1 deletions

View file

@ -1,7 +1,7 @@
use std::{
env,
ffi::OsString,
fmt,
fmt, fs, iter,
num::{NonZeroU64, ParseIntError},
path::PathBuf,
str::FromStr,
@ -14,6 +14,7 @@ use binstalk::{
};
use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum};
use compact_str::CompactString;
use dirs::home_dir;
use log::LevelFilter;
use semver::VersionReq;
use strum::EnumCount;
@ -145,6 +146,14 @@ pub struct Args {
#[clap(help_heading = "Overrides", long, value_delimiter(','))]
pub disable_strategies: Vec<Strategy>,
/// If `--github-token` or environment variable `GITHUB_TOKEN` is not
/// specified, then cargo-binstall will try to extract github token from
/// `$HOME/.git-credentials` or `$HOME/.config/gh/hosts.yml` by default.
///
/// This option can be used to disable that behavior.
#[clap(help_heading = "Overrides", long)]
pub no_discover_github_token: bool,
/// Disable symlinking / versioned updates.
///
/// By default, Binstall will install a binary named `<name>-<version>` in the install path, and
@ -452,9 +461,52 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
.exit()
}
if opts.github_token.is_none() && !opts.no_discover_github_token {
if let Some(github_token) = try_extract_from_git_credentials() {
opts.github_token = Some(github_token);
} else if let Ok(github_token) = gh_token::get() {
opts.github_token = Some(github_token.into());
}
}
opts
}
fn try_extract_from_git_credentials() -> Option<CompactString> {
home_dir()
.map(|mut home| {
home.push(".git-credentials");
home
})
.into_iter()
.chain(iter::from_fn(|| {
let home = env::var_os("XDG_CONFIG_HOME")?;
(!home.is_empty()).then(|| {
let mut path = PathBuf::from(home);
path.push("git/credentials");
path
})
}))
.find_map(try_extract_from_git_credentials_from)
}
fn try_extract_from_git_credentials_from(path: PathBuf) -> Option<CompactString> {
fs::read_to_string(path)
.ok()?
.lines()
.find_map(extract_github_token_from_git_credentials_line)
.map(CompactString::from)
}
fn extract_github_token_from_git_credentials_line(line: &str) -> Option<&str> {
let cred = line
.trim()
.strip_prefix("https://")?
.strip_suffix("@github.com")?;
Some(cred.split_once(':')?.1)
}
#[cfg(test)]
mod test {
use super::*;
@ -463,4 +515,24 @@ mod test {
fn verify_cli() {
Args::command().debug_assert()
}
const GIT_CREDENTIALS_TEST_CASES: &[(&str, Option<&str>)] = &[
// Success
("https://NobodyXu:gho_asdc@github.com", Some("gho_asdc")),
(
"https://NobodyXu:gho_asdc12dz@github.com",
Some("gho_asdc12dz"),
),
// Failure
("http://NobodyXu:gho_asdc@github.com", None),
("https://NobodyXu:gho_asdc@gitlab.com", None),
("https://NobodyXugho_asdc@github.com", None),
];
#[test]
fn test_extract_github_token_from_git_credentials_line() {
GIT_CREDENTIALS_TEST_CASES.iter().for_each(|(line, res)| {
assert_eq!(extract_github_token_from_git_credentials_line(line), *res);
})
}
}