Skip to content

Commit

Permalink
feat: 完善安装和更新前检查
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed May 4, 2024
1 parent e89764c commit 35fb014
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/entrances/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
},
};
use crate::{
entrances::{info, update_using_package},
entrances::{info, update_using_package, update_using_package_matcher},
utils::{
download::{download, fill_url_template},
get_path_temp,
Expand Down Expand Up @@ -147,6 +147,21 @@ pub fn install_using_package_matcher(
matcher: PackageMatcher,
verify_signature: bool,
) -> Result<()> {
// 查找 scope
let scope = if let Some(s) = matcher.scope.clone() {
s
} else {
find_scope_with_name_locally(&matcher.name)?
};
// 检查对应包名有没有被安装过
if let Ok((_, diff)) = info_local(&scope, &matcher.name) {
log!(
"Warning:Package '{name}' has been installed({ver}), switch to update entrance",
name = matcher.name,
ver = diff.version,
);
return update_using_package_matcher(matcher, verify_signature);
}
// 解析 url
let url = get_url_with_version_req(matcher)?;
// 执行安装
Expand Down
30 changes: 26 additions & 4 deletions src/entrances/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ use crate::{
download::{download, download_nep},
fs::move_or_copy,
get_path_apps, get_path_temp,
mirror::get_url_with_version_req,
mirror::{filter_release, get_url_with_version_req},
path::find_scope_with_name_locally,
term::ask_yn,
},
};
use crate::{log, log_ok_last};

use super::{
info_local, install_using_package, uninstall,
utils::package::{clean_temp, unpack_nep},
utils::validator::installed_validator,
info_local, info_online, install_using_package, uninstall,
utils::{
package::{clean_temp, unpack_nep},
validator::installed_validator,
},
};

fn same_authors(a: &Vec<String>, b: &Vec<String>) -> bool {
Expand Down Expand Up @@ -145,6 +148,25 @@ pub fn update_using_url(url: &String, verify_signature: bool) -> Result<()> {
}

pub fn update_using_package_matcher(matcher: PackageMatcher, verify_signature: bool) -> Result<()> {
// 查找 scope
let scope = if let Some(s) = matcher.scope.clone() {
s
} else {
find_scope_with_name_locally(&matcher.name)?
};
// 检查对应包名有没有被安装过
let (_global, local_diff) = info_local(&scope, &matcher.name).map_err(|_| {
anyhow!(
"Error:Package '{name}' hasn't been installed, use 'ept install \"{name}\"' instead",
name = &matcher.name,
)
})?;
// 检查包的版本号是否允许升级
let (online_item, _url_template) = info_online(&scope, &matcher.name, matcher.mirror.clone())?;
let selected_release = filter_release(online_item.releases, matcher.version_req.clone())?;
if selected_release.version <= ExSemVer::parse(&local_diff.version)? {
return Err(anyhow!("Error:Package '{name}' has been up to date ({local_version}), can't update to the version of given package ({fresh_version})",name=matcher.name,local_version=&local_diff.version,fresh_version=&selected_release.version));
}
// 解析 url
let url = get_url_with_version_req(matcher)?;
// 执行安装
Expand Down
5 changes: 4 additions & 1 deletion src/utils/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ pub fn download(url: &String, at: &PathBuf) -> Result<()> {
let client = Client::new();

// 发送 GET 请求
let mut response = client.get(url).send()?;
let mut response = client
.get(url)
.send()
.map_err(|e| anyhow!("Error:Failed to request url '{url}' : {e}"))?;

// 尝试获取内容长度
let content_length = response.content_length().unwrap_or(0);
Expand Down

0 comments on commit 35fb014

Please sign in to comment.