Fix GitHub token auto discovery (#1335)

* Fix GitHub token auto discovery

Fixed #1333

 - Rm dep `gh-token` since it is broken and we can simply run
   `gh auth token` in `cargo-binstall` instead.
 - binstalk-downloader: Make sure GitHub token is at least 40B long
   and other than the `_`, composes of only alphanumeric characters.
 - Warn on failure to read `git/credential` files
 - Optimize `try_from_home` to avoid heap allocation of `PathBuf`

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

* Fix typo and clippy

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

* Simplify `is_valid_gh_token` & `is_ascii_alphanumeric` impl

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

* Improve err msg in `get_inner`

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

* Improve err msg of `cargo_binstall::gh_token::get`

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

---------

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-09-01 11:14:59 +10:00 committed by GitHub
parent 0ca38ab0e3
commit 8a08cdda6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 53 deletions

View file

@ -33,7 +33,7 @@ use tracing::{debug, error, info, warn};
use crate::{
args::{Args, Strategy},
install_path,
gh_token, git_credentials, install_path,
ui::confirm,
};
@ -107,7 +107,16 @@ pub fn install_crates(
)
.map_err(BinstallError::from)?;
let gh_api_client = GhApiClient::new(client.clone(), args.github_token);
let gh_api_client = GhApiClient::new(
client.clone(),
args.github_token.or_else(|| {
if args.no_discover_github_token {
None
} else {
git_credentials::try_from_home().or_else(gh_token::get)
}
}),
);
// Create binstall_opts
let binstall_opts = Arc::new(Options {