rustic_rs/commands/
self_update.rs

1//! `self-update` subcommand
2
3use crate::{Application, RUSTIC_APP};
4
5use abscissa_core::{Command, Runnable, Shutdown, status_err};
6
7use anyhow::Result;
8
9/// `self-update` subcommand
10#[derive(clap::Parser, Command, Debug)]
11pub(crate) struct SelfUpdateCmd {
12    /// Do not ask before processing the self-update
13    #[clap(long, conflicts_with = "dry_run")]
14    force: bool,
15}
16
17impl Runnable for SelfUpdateCmd {
18    fn run(&self) {
19        if let Err(err) = self.inner_run() {
20            status_err!("{}", err);
21            RUSTIC_APP.shutdown(Shutdown::Crash);
22        };
23    }
24}
25
26impl SelfUpdateCmd {
27    #[cfg(feature = "self-update")]
28    fn inner_run(&self) -> Result<()> {
29        let current_version = semver::Version::parse(self_update::cargo_crate_version!())?;
30
31        let release = self_update::backends::github::Update::configure()
32            .repo_owner("rustic-rs")
33            .repo_name("rustic")
34            .bin_name("rustic")
35            .show_download_progress(true)
36            .current_version(current_version.to_string().as_str())
37            .no_confirm(self.force)
38            .build()?;
39
40        let latest_release = release.get_latest_release()?;
41
42        let upstream_version = semver::Version::parse(&latest_release.version)?;
43
44        match current_version.cmp(&upstream_version) {
45            std::cmp::Ordering::Greater => {
46                println!(
47                    "Your rustic version {current_version} is newer than the stable version {upstream_version} on upstream!"
48                );
49            }
50            std::cmp::Ordering::Equal => {
51                println!("rustic version {current_version} is up-to-date!");
52            }
53            std::cmp::Ordering::Less => {
54                let status = release.update()?;
55
56                if let self_update::Status::Updated(str) = status {
57                    println!("rustic version has been updated to: {str}");
58                }
59            }
60        }
61
62        Ok(())
63    }
64    #[cfg(not(feature = "self-update"))]
65    fn inner_run(&self) -> Result<()> {
66        anyhow::bail!(
67            "This version of rustic was built without the \"self-update\" feature. Please use your system package manager to update it."
68        );
69    }
70}