Skip to content

Commit a0a4b5a

Browse files
committed
Use GitHub auth token in self-update to avoid API rate limits
1 parent 2ad9f7c commit a0a4b5a

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

cas-cli/src/cli/update.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,16 @@ fn check_for_updates(
612612
use self_update::backends::github::Update;
613613

614614
// Check binary updates
615-
let updater = Update::configure()
615+
let mut builder = Update::configure();
616+
builder
616617
.repo_owner(REPO_OWNER)
617618
.repo_name(REPO_NAME)
618619
.bin_name(BIN_NAME)
619-
.current_version(current_version)
620-
.build()?;
620+
.current_version(current_version);
621+
if let Some(token) = github_auth_token() {
622+
builder.auth_token(&token);
623+
}
624+
let updater = builder.build()?;
621625

622626
let latest = updater.get_latest_release()?;
623627
let latest_version = latest.version.trim_start_matches('v');
@@ -722,6 +726,9 @@ fn perform_update(args: &UpdateArgs, current_version: &str, cli: &Cli) -> anyhow
722726
.current_version(current_version)
723727
.show_download_progress(true)
724728
.no_confirm(args.yes);
729+
if let Some(token) = github_auth_token() {
730+
updater.auth_token(&token);
731+
}
725732

726733
// If a specific version is requested, set it
727734
if let Some(ref version) = args.version {
@@ -810,6 +817,31 @@ fn perform_update(args: &UpdateArgs, current_version: &str, cli: &Cli) -> anyhow
810817
Ok(())
811818
}
812819

820+
/// Try to get a GitHub auth token from `gh auth token` or GITHUB_TOKEN env var.
821+
fn github_auth_token() -> Option<String> {
822+
// Try GITHUB_TOKEN env var first
823+
if let Ok(token) = std::env::var("GITHUB_TOKEN") {
824+
if !token.is_empty() {
825+
return Some(token);
826+
}
827+
}
828+
// Fall back to `gh auth token`
829+
std::process::Command::new("gh")
830+
.args(["auth", "token"])
831+
.output()
832+
.ok()
833+
.and_then(|o| {
834+
if o.status.success() {
835+
String::from_utf8(o.stdout)
836+
.ok()
837+
.map(|s| s.trim().to_string())
838+
.filter(|s| !s.is_empty())
839+
} else {
840+
None
841+
}
842+
})
843+
}
844+
813845
/// Compare semantic versions to check if `new` is newer than `current`
814846
fn is_newer(new: &str, current: &str) -> bool {
815847
let parse = |v: &str| -> Option<(u32, u32, u32)> {

0 commit comments

Comments
 (0)