1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#[cfg(windows)]
const BIN_NAME: &str = "unc.exe";
#[cfg(not(windows))]
const BIN_NAME: &str = "unc";

use color_eyre::{eyre::WrapErr, owo_colors::OwoColorize};

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = crate::GlobalContext)]
#[interactive_clap(output_context = SelfUpdateCommandContext)]
pub struct SelfUpdateCommand;

#[derive(Debug, Clone)]
pub struct SelfUpdateCommandContext;

impl SelfUpdateCommandContext {
    pub fn from_previous_context(
        _previous_context: crate::GlobalContext,
        _scope: &<SelfUpdateCommand as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
    ) -> color_eyre::eyre::Result<Self> {
        let status = self_update::backends::github::Update::configure()
            .repo_owner("unc")
            .repo_name("unc-cli-rs")
            .bin_path_in_archive(
                format!("unc-cli-rs-{}/{}", self_update::get_target(), BIN_NAME).as_str(),
            )
            .bin_name(BIN_NAME)
            .show_download_progress(true)
            .current_version(self_update::cargo_crate_version!())
            .build()
            .wrap_err("Failed to build self_update")?
            .update()
            .wrap_err("Failed to update unc CLI")?;
        if let self_update::Status::Updated(release) = status {
            println!(
                "\n{}{}{}\n",
                "Welcome to `unc` CLI v".green().bold(),
                release.green().bold(),
                "!".green().bold()
            );
            println!("Report any bugs:\n");
            println!("\thttps://github.com/utility/utility-cli-rs/issues\n");
            println!("What's new:\n");
            println!(
                "\t{}{}\n",
                "https://github.com/utility/utility-cli-rs/releases/tag/v".truecolor(0, 160, 150),
                release.truecolor(0, 160, 150)
            );
        }

        Ok(Self)
    }
}

pub fn get_latest_version() -> color_eyre::eyre::Result<String> {
    Ok(self_update::backends::github::Update::configure()
        .repo_owner("unc")
        .repo_name("unc-cli-rs")
        .bin_name("unc")
        .current_version(self_update::cargo_crate_version!())
        .build()
        .wrap_err("Failed to build self_update")?
        .get_latest_release()
        .wrap_err("Failed to get latest release")?
        .version)
}