Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip sha (sha1, sha256, sha512) checksum files #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [unreleased]
### Added
- add `UpdateBuilder::asset_match_fn` to enable custom logic for release asset matching
### Changed
### Removed

Expand Down
17 changes: 17 additions & 0 deletions src/backends/gitea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
update::{Release, ReleaseAsset, ReleaseUpdate},
DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE,
};
use crate::update::AssetMatchFn;

impl ReleaseAsset {
/// Parse a release-asset json object
Expand Down Expand Up @@ -230,6 +231,7 @@ pub struct UpdateBuilder {
repo_owner: Option<String>,
repo_name: Option<String>,
target: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<String>,
Expand Down Expand Up @@ -294,6 +296,14 @@ impl UpdateBuilder {
self
}

/// Set the function to match assets
///
/// If unspecified, the first asset matching the target will be chosen
pub fn asset_match_fn(&mut self, asset_match_fn: AssetMatchFn) -> &mut Self {
self.asset_match_fn = Some(asset_match_fn);
self
}

/// Set the exe's name. Also sets `bin_path_in_archive` if it hasn't already been set.
///
/// This method will append the platform specific executable file suffix
Expand Down Expand Up @@ -439,6 +449,7 @@ impl UpdateBuilder {
.as_ref()
.map(|t| t.to_owned())
.unwrap_or_else(|| get_target().to_owned()),
asset_match_fn: self.asset_match_fn.clone(),
bin_name: if let Some(ref name) = self.bin_name {
name.to_owned()
} else {
Expand Down Expand Up @@ -476,6 +487,7 @@ pub struct Update {
repo_name: String,
target: String,
current_version: String,
asset_match_fn: Option<AssetMatchFn>,
target_version: Option<String>,
bin_name: String,
bin_install_path: PathBuf,
Expand Down Expand Up @@ -553,6 +565,10 @@ impl ReleaseUpdate for Update {
self.target_version.clone()
}

fn asset_match_fn(&self) -> Option<AssetMatchFn> {
self.asset_match_fn.clone()
}

fn bin_name(&self) -> String {
self.bin_name.clone()
}
Expand Down Expand Up @@ -606,6 +622,7 @@ impl Default for UpdateBuilder {
repo_owner: None,
repo_name: None,
target: None,
asset_match_fn: None,
bin_name: None,
bin_install_path: None,
bin_path_in_archive: None,
Expand Down
17 changes: 17 additions & 0 deletions src/backends/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
update::{Release, ReleaseAsset, ReleaseUpdate},
DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE,
};
use crate::update::AssetMatchFn;

impl ReleaseAsset {
/// Parse a release-asset json object
Expand Down Expand Up @@ -232,6 +233,7 @@ pub struct UpdateBuilder {
repo_name: Option<String>,
target: Option<String>,
identifier: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<String>,
Expand Down Expand Up @@ -307,6 +309,14 @@ impl UpdateBuilder {
self
}

/// Set the function to match assets
///
/// If unspecified, the first asset matching the target will be chosen
pub fn asset_match_fn(&mut self, asset_match_fn: AssetMatchFn) -> &mut Self {
self.asset_match_fn = Some(asset_match_fn);
self
}

/// Set the exe's name. Also sets `bin_path_in_archive` if it hasn't already been set.
///
/// This method will append the platform specific executable file suffix
Expand Down Expand Up @@ -448,6 +458,7 @@ impl UpdateBuilder {
.map(|t| t.to_owned())
.unwrap_or_else(|| get_target().to_owned()),
identifier: self.identifier.clone(),
asset_match_fn: self.asset_match_fn.clone(),
bin_name: if let Some(ref name) = self.bin_name {
name.to_owned()
} else {
Expand Down Expand Up @@ -485,6 +496,7 @@ pub struct Update {
repo_name: String,
target: String,
identifier: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
current_version: String,
target_version: Option<String>,
bin_name: String,
Expand Down Expand Up @@ -577,6 +589,10 @@ impl ReleaseUpdate for Update {
self.identifier.clone()
}

fn asset_match_fn(&self) -> Option<AssetMatchFn> {
self.asset_match_fn.clone()
}

fn bin_name(&self) -> String {
self.bin_name.clone()
}
Expand Down Expand Up @@ -630,6 +646,7 @@ impl Default for UpdateBuilder {
repo_name: None,
target: None,
identifier: None,
asset_match_fn: None,
bin_name: None,
bin_install_path: None,
bin_path_in_archive: None,
Expand Down
17 changes: 17 additions & 0 deletions src/backends/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
update::{Release, ReleaseAsset, ReleaseUpdate},
DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE,
};
use crate::update::AssetMatchFn;

impl ReleaseAsset {
/// Parse a release-asset json object
Expand Down Expand Up @@ -227,6 +228,7 @@ pub struct UpdateBuilder {
repo_owner: Option<String>,
repo_name: Option<String>,
target: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<String>,
Expand Down Expand Up @@ -291,6 +293,14 @@ impl UpdateBuilder {
self
}

/// Set the function to match assets
///
/// If unspecified, the first asset matching the target will be chosen
pub fn asset_match_fn(&mut self, asset_match_fn: AssetMatchFn) -> &mut Self {
self.asset_match_fn = Some(asset_match_fn);
self
}

/// Set the exe's name. Also sets `bin_path_in_archive` if it hasn't already been set.
///
/// This method will append the platform specific executable file suffix
Expand Down Expand Up @@ -432,6 +442,7 @@ impl UpdateBuilder {
.as_ref()
.map(|t| t.to_owned())
.unwrap_or_else(|| get_target().to_owned()),
asset_match_fn: self.asset_match_fn.clone(),
bin_name: if let Some(ref name) = self.bin_name {
name.to_owned()
} else {
Expand Down Expand Up @@ -470,6 +481,7 @@ pub struct Update {
target: String,
current_version: String,
target_version: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
bin_name: String,
bin_install_path: PathBuf,
bin_path_in_archive: String,
Expand Down Expand Up @@ -551,6 +563,10 @@ impl ReleaseUpdate for Update {
self.target_version.clone()
}

fn asset_match_fn(&self) -> Option<AssetMatchFn> {
self.asset_match_fn.clone()
}

fn bin_name(&self) -> String {
self.bin_name.clone()
}
Expand Down Expand Up @@ -604,6 +620,7 @@ impl Default for UpdateBuilder {
repo_owner: None,
repo_name: None,
target: None,
asset_match_fn: None,
bin_name: None,
bin_install_path: None,
bin_path_in_archive: None,
Expand Down
9 changes: 9 additions & 0 deletions src/backends/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use regex::Regex;
use std::cmp::Ordering;
use std::env::{self, consts::EXE_SUFFIX};
use std::path::{Path, PathBuf};
use crate::update::AssetMatchFn;

/// Maximum number of items to retrieve from S3 API
const MAX_KEYS: u8 = 100;
Expand Down Expand Up @@ -142,6 +143,7 @@ pub struct UpdateBuilder {
asset_prefix: Option<String>,
target: Option<String>,
region: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<String>,
Expand All @@ -165,6 +167,7 @@ impl Default for UpdateBuilder {
asset_prefix: None,
target: None,
region: None,
asset_match_fn: None,
bin_name: None,
bin_install_path: None,
bin_path_in_archive: None,
Expand Down Expand Up @@ -367,6 +370,7 @@ impl UpdateBuilder {
.as_ref()
.map(|t| t.to_owned())
.unwrap_or_else(|| get_target().to_owned()),
asset_match_fn: self.asset_match_fn.clone(),
bin_name: if let Some(ref name) = self.bin_name {
name.to_owned()
} else {
Expand Down Expand Up @@ -406,6 +410,7 @@ pub struct Update {
region: Option<String>,
current_version: String,
target_version: Option<String>,
asset_match_fn: Option<AssetMatchFn>,
bin_name: String,
bin_install_path: PathBuf,
bin_path_in_archive: String,
Expand Down Expand Up @@ -486,6 +491,10 @@ impl ReleaseUpdate for Update {
self.target_version.clone()
}

fn asset_match_fn(&self) -> Option<AssetMatchFn> {
self.asset_match_fn.clone()
}

fn bin_name(&self) -> String {
self.bin_name.clone()
}
Expand Down
18 changes: 13 additions & 5 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ impl Release {
}
}

/// Function type for determining the asset among multiple matches
pub type AssetMatchFn = fn(&Release, &str, Option<&str>) -> Option<ReleaseAsset>;

/// Updates to a specified or latest release
pub trait ReleaseUpdate {
/// Fetch details of the latest release from the backend
Expand All @@ -98,6 +101,9 @@ pub trait ReleaseUpdate {
None
}

/// Optional function for determining the asset among multiple matches
fn asset_match_fn(&self) -> Option<AssetMatchFn>;

/// Name of the binary being updated
fn bin_name(&self) -> String;

Expand Down Expand Up @@ -206,11 +212,13 @@ pub trait ReleaseUpdate {
}
};

let target_asset = release
.asset_for(&target, self.identifier().as_deref())
.ok_or_else(|| {
format_err!(Error::Release, "No asset found for target: `{}`", target)
})?;
let target_asset = if let Some(asset_match_fn) = self.asset_match_fn() {
asset_match_fn(&release, &target, self.identifier().as_deref())
} else {
release.asset_for(&target, self.identifier().as_deref())
}.ok_or_else(|| {
format_err!(Error::Release, "No asset found for target: `{}`", target)
})?;

let prompt_confirmation = !self.no_confirm();
if self.show_output() || prompt_confirmation {
Expand Down