From 7d7809e566a409464609aced7f0e979941084219 Mon Sep 17 00:00:00 2001 From: YISH Date: Tue, 17 Sep 2024 09:22:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20self-update=20supports=20for=20?= =?UTF-8?q?upgrading=20to=20specific=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.rs | 3 +++ src/main.rs | 7 +++---- src/{update.rs => updater.rs} | 16 +++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) rename src/{update.rs => updater.rs} (65%) diff --git a/src/cli.rs b/src/cli.rs index cb794158..711870ea 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -79,6 +79,9 @@ pub enum Commands { /// Automatic yes to prompts #[arg(short = 'y', long)] yes: bool, + + /// The target version to update to + version: Option, }, /// Manage the Smart-DNS service (install, uninstall, start, stop, restart). diff --git a/src/main.rs b/src/main.rs index 07af26cd..0355b6cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ mod server; mod service; mod third_ext; #[cfg(feature = "self-update")] -mod update; +mod updater; use error::Error; use infra::middleware; @@ -169,9 +169,8 @@ impl Cli { RuntimeConfig::load(conf); } #[cfg(feature = "self-update")] - Commands::Update { yes } => { - use update::update; - update(yes).unwrap(); + Commands::Update { yes, version } => { + updater::update(yes, version.as_deref()).unwrap(); } } } diff --git a/src/update.rs b/src/updater.rs similarity index 65% rename from src/update.rs rename to src/updater.rs index f17791c6..246d6ea5 100644 --- a/src/update.rs +++ b/src/updater.rs @@ -1,10 +1,12 @@ #[cfg(feature = "self-update")] -pub fn update(assume_yes: bool) -> anyhow::Result<()> { +pub fn update(assume_yes: bool, ver: Option<&str>) -> anyhow::Result<()> { let bin = env!("CARGO_BIN_NAME"); let target = env!("CARGO_BUILD_TARGET"); let target_dir = format!("{bin}-{target}"); - let status = self_update::backends::github::Update::configure() + let mut builder = self_update::backends::github::Update::configure(); + + builder .repo_owner("mokeyish") .repo_name(&[bin, "-rs"].concat()) .identifier(target) @@ -12,9 +14,13 @@ pub fn update(assume_yes: bool) -> anyhow::Result<()> { .bin_path_in_archive(&format!("{}/{}", target_dir, "{{bin}}")) .show_download_progress(true) .no_confirm(assume_yes) - .current_version(self_update::cargo_crate_version!()) - .build()? - .update()?; + .current_version(self_update::cargo_crate_version!()); + + if let Some(ver) = ver { + builder.target_version_tag(ver); + } + + let status = builder.build()?.update()?; println!("Update status: `{}`!", status.version()); Ok(()) From e7e9ac5539e7287b6026d7499bdf63327c34604f Mon Sep 17 00:00:00 2001 From: YISH Date: Tue, 17 Sep 2024 10:08:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20clippy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dns_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dns_client.rs b/src/dns_client.rs index a595b7b7..c0e0d533 100644 --- a/src/dns_client.rs +++ b/src/dns_client.rs @@ -317,7 +317,7 @@ impl DnsClient { } pub async fn get_server_group(&self, name: &str) -> Option> { - if name == "" || name.eq_ignore_ascii_case("default") { + if name.is_empty() || name.eq_ignore_ascii_case("default") { return Some(self.default.clone()); } self.servers.get(name).cloned()