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

✨ self-update supports for upgrading to specific version #381

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
},

/// Manage the Smart-DNS service (install, uninstall, start, stop, restart).
Expand Down
2 changes: 1 addition & 1 deletion src/dns_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl DnsClient {
}

pub async fn get_server_group(&self, name: &str) -> Option<Arc<NameServerGroup>> {
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()
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/update.rs → src/updater.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#[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)
.bin_name(bin)
.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(())
Expand Down