diff --git a/src/cmds/install.rs b/src/cmds/install.rs index 0128904..574e819 100644 --- a/src/cmds/install.rs +++ b/src/cmds/install.rs @@ -1,7 +1,6 @@ use anyhow::{Context, Result}; use clap::Parser; use flate2::read::GzDecoder; -use octocrab::Octocrab; use octocrab::models::repos::Asset; use octocrab::models::repos::Release; use reqwest::Client; @@ -180,8 +179,9 @@ pub async fn download_tool_from_asset(tool: &Tool, asset: &Asset, config: &Confi async fn find_matching_release( tool: &Tool, requested: &VersionReq, + config: &Config, ) -> anyhow::Result> { - let octocrab = Octocrab::builder().build()?; + let octocrab = crate::manifest::build_octocrab(config)?; let owner = tool.repo_owner.clone(); let repo = tool.repo_name.clone(); let repo = octocrab.repos(owner, repo); @@ -219,7 +219,7 @@ async fn run_github_release_installer( ) -> anyhow::Result<()> { println!("\n> Installing {} at version {}", tool.name, requested); - let Some((version, release)) = find_matching_release(tool, requested).await? else { + let Some((version, release)) = find_matching_release(tool, requested, config).await? else { return Err(anyhow::anyhow!("No release found for {}", tool.name)); }; diff --git a/src/main.rs b/src/main.rs index a5658bd..1c8f3c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,9 @@ struct Cli { #[arg(global = true, short, long, env = "TX3_CHANNEL")] channel: Option, + #[arg(global = true, long, env = "GITHUB_TOKEN", hide = true)] + github_token: Option, + #[command(subcommand)] command: Option, } @@ -57,9 +60,14 @@ impl Commands { pub struct Config { root_dir: Option, channel: Option, + github_token: Option, } impl Config { + pub fn github_token(&self) -> Option<&str> { + self.github_token.as_deref().filter(|t| !t.is_empty()) + } + pub fn default_root_dir() -> Result { let mut path = if cfg!(target_os = "windows") { dirs::data_local_dir() @@ -164,6 +172,7 @@ async fn main() -> anyhow::Result<()> { let config = Config { root_dir: cli.root_dir, channel: cli.channel, + github_token: cli.github_token, }; let skip_banner = cli.command.as_ref().map_or(false, |c| c.skip_banner()); diff --git a/src/manifest.rs b/src/manifest.rs index cd1d807..1246702 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -10,6 +10,16 @@ use tokio::fs; use crate::Config; +pub fn build_octocrab(config: &Config) -> anyhow::Result { + let mut builder = Octocrab::builder(); + + if let Some(token) = config.github_token() { + builder = builder.personal_token(token.to_string()); + } + + builder.build().context("building octocrab client") +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Installer { GithubRelease, @@ -103,9 +113,7 @@ pub async fn download_remote_manifest( config: &Config, explicit_tag: Option<&str>, ) -> anyhow::Result<()> { - let octocrab = Octocrab::builder() - .build() - .context("building octocrab client")?; + let octocrab = build_octocrab(config)?; let repo = octocrab.repos("tx3-lang", "toolchain");