From f7643e1da10a50b53bacc6a902b6b74a02a9ce95 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Mon, 22 Jul 2024 23:54:50 +1000 Subject: [PATCH] Add support to disable strategies via crate `Cargo.toml` Signed-off-by: Jiahao XU --- crates/bin/src/entry.rs | 1 + crates/binstalk-fetchers/src/gh_crate_meta.rs | 5 +++++ crates/binstalk-fetchers/src/lib.rs | 5 ++++- crates/binstalk-fetchers/src/quickinstall.rs | 6 +++++- crates/binstalk-types/src/cargo_toml_binstall.rs | 12 ++++++++++++ crates/binstalk/src/ops/resolve.rs | 9 +++++++++ 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/bin/src/entry.rs b/crates/bin/src/entry.rs index 3ae83141..770c72fb 100644 --- a/crates/bin/src/entry.rs +++ b/crates/bin/src/entry.rs @@ -85,6 +85,7 @@ pub fn install_crates( pkg_url: args.pkg_url, pkg_fmt: args.pkg_fmt, bin_dir: args.bin_dir, + disabled_strategies: None, signing: None, }; diff --git a/crates/binstalk-fetchers/src/gh_crate_meta.rs b/crates/binstalk-fetchers/src/gh_crate_meta.rs index 51cd012c..d1df0637 100644 --- a/crates/binstalk-fetchers/src/gh_crate_meta.rs +++ b/crates/binstalk-fetchers/src/gh_crate_meta.rs @@ -1,6 +1,7 @@ use std::{borrow::Cow, fmt, iter, path::Path, sync::Arc}; use binstalk_git_repo_api::gh_api_client::{GhApiError, GhReleaseArtifact, GhReleaseArtifactUrl}; +use binstalk_types::cargo_toml_binstall::Strategy; use compact_str::{CompactString, ToCompactString}; use either::Either; use leon::Template; @@ -396,6 +397,10 @@ impl super::Fetcher for GhCrateMeta { FETCHER_GH_CRATE_META } + fn strategy(&self) -> Strategy { + Strategy::CrateMetaData + } + fn is_third_party(&self) -> bool { false } diff --git a/crates/binstalk-fetchers/src/lib.rs b/crates/binstalk-fetchers/src/lib.rs index 5d4f692a..49a86894 100644 --- a/crates/binstalk-fetchers/src/lib.rs +++ b/crates/binstalk-fetchers/src/lib.rs @@ -4,7 +4,7 @@ use std::{path::Path, sync::Arc, time::Duration}; use binstalk_downloader::{download::DownloadError, remote::Error as RemoteError}; use binstalk_git_repo_api::gh_api_client::{GhApiError, GhRepo, RepoInfo as GhRepoInfo}; -use binstalk_types::cargo_toml_binstall::SigningAlgorithm; +use binstalk_types::cargo_toml_binstall::{SigningAlgorithm, Strategy}; use thiserror::Error as ThisError; use tokio::{sync::OnceCell, task::JoinError, time::sleep}; pub use url::ParseError as UrlParseError; @@ -134,6 +134,9 @@ pub trait Fetcher: Send + Sync { /// [`Fetcher::fetch_and_extract`]. fn fetcher_name(&self) -> &'static str; + /// The strategy used by this fetcher + fn strategy(&self) -> Strategy; + /// Should return true if the remote is from a third-party source fn is_third_party(&self) -> bool; diff --git a/crates/binstalk-fetchers/src/quickinstall.rs b/crates/binstalk-fetchers/src/quickinstall.rs index 2862fb89..ad5d2f7d 100644 --- a/crates/binstalk-fetchers/src/quickinstall.rs +++ b/crates/binstalk-fetchers/src/quickinstall.rs @@ -5,7 +5,7 @@ use std::{ }; use binstalk_downloader::remote::Method; -use binstalk_types::cargo_toml_binstall::{PkgFmt, PkgMeta, PkgSigning}; +use binstalk_types::cargo_toml_binstall::{PkgFmt, PkgMeta, PkgSigning, Strategy}; use tokio::sync::OnceCell; use tracing::{error, info, trace}; use url::Url; @@ -252,6 +252,10 @@ by rust officially."#, "QuickInstall" } + fn strategy(&self) -> Strategy { + Strategy::QuickInstall + } + fn is_third_party(&self) -> bool { true } diff --git a/crates/binstalk-types/src/cargo_toml_binstall.rs b/crates/binstalk-types/src/cargo_toml_binstall.rs index 1684ac60..e1ab03c6 100644 --- a/crates/binstalk-types/src/cargo_toml_binstall.rs +++ b/crates/binstalk-types/src/cargo_toml_binstall.rs @@ -73,6 +73,9 @@ pub struct PkgMeta { /// Package signing configuration pub signing: Option, + /// Stratgies to disable + pub disabled_strategies: Option>, + /// Target specific overrides pub overrides: BTreeMap, } @@ -118,10 +121,16 @@ impl PkgMeta { .or_else(|| self.bin_dir.clone()), signing: pkg_overrides + .clone() .into_iter() .find_map(|pkg_override| pkg_override.signing.clone()) .or_else(|| self.signing.clone()), + disabled_strategies: pkg_overrides + .into_iter() + .find_map(|pkg_override| pkg_override.disabled_strategies.clone()) + .or_else(|| self.disabled_strategies.clone()), + overrides: Default::default(), } } @@ -142,6 +151,9 @@ pub struct PkgOverride { /// Path template override for binary files in packages pub bin_dir: Option, + /// Stratgies to disable + pub disabled_strategies: Option>, + /// Package signing configuration pub signing: Option, } diff --git a/crates/binstalk/src/ops/resolve.rs b/crates/binstalk/src/ops/resolve.rs index 5c6379d9..36be67e3 100644 --- a/crates/binstalk/src/ops/resolve.rs +++ b/crates/binstalk/src/ops/resolve.rs @@ -134,6 +134,8 @@ async fn resolve_inner( }, )) .filter_map(|(f, target_data)| { + let disabled_strategies = target_data.meta.disabled_strategies.clone(); + let fetcher = f( opts.client.clone(), gh_api_client.clone(), @@ -141,6 +143,13 @@ async fn resolve_inner( target_data, opts.signature_policy, ); + + if let Some(disabled_strategies) = disabled_strategies.as_deref() { + if disabled_strategies.contains(&fetcher.strategy()) { + return None; + } + } + filter_fetcher_by_name_predicate(fetcher.fetcher_name()).then_some(fetcher) }), )