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
67
68
69
70
71
72
73
74
75
76
77
mod parse;
pub mod types;
pub mod util;
use anyhow::Result;
use clap::Parser;
use tokio::fs;
use self::types::Version;
use self::util::{
check_version, create_completions_commands, download, execute_commands, now_secs,
swap_exe_command, unpack,
};
use crate::config::VERSION;
use crate::state::http::HttpClient;
use crate::state::State;
#[derive(Debug, Parser)]
#[clap(about = "Update Hop to the latest version")]
pub struct Options {
#[clap(short = 'f', long = "force", help = "Force update")]
pub force: bool,
#[clap(short = 'b', long = "beta", help = "Update to beta version")]
pub beta: bool,
}
pub async fn handle(options: Options, mut state: State) -> Result<()> {
let http = HttpClient::new(None, None);
let (update, version) = check_version(&Version::from_string(VERSION)?, options.beta).await?;
if !update && !options.force {
log::info!("CLI is up to date");
return Ok(());
}
log::info!("Found new version {version} (current: {VERSION})");
let packed_temp = download(http, version.to_string())
.await
.expect("Failed to download");
let unpacked = unpack(packed_temp.clone()).await?;
fs::remove_file(packed_temp).await?;
let mut non_elevated_args: Vec<String> = vec![];
let mut elevated_args: Vec<String> = vec![];
let current = std::env::current_exe()?;
swap_exe_command(
&mut non_elevated_args,
&mut elevated_args,
current.clone(),
unpacked,
)
.await;
create_completions_commands(&mut non_elevated_args, &mut elevated_args, current).await;
execute_commands(&non_elevated_args, &elevated_args).await?;
state.ctx.last_version_check = Some((now_secs().to_string(), version.to_string()));
state.ctx.save().await?;
log::info!("Updated to {version}");
Ok(())
}