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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
mod parse;
pub mod types;
pub mod util;
use std::ffi::OsString;
use std::path::PathBuf;
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::commands::update::util::HOP_CLI_DOWNLOAD_URL;
use crate::config::{ARCH, VERSION};
use crate::state::http::HttpClient;
use crate::state::State;
use crate::utils::capitalize;
#[derive(Debug, Parser)]
#[clap(about = "Update Hop to the latest version")]
pub struct Options {
#[clap(short, long, help = "Force update")]
pub force: bool,
#[clap(short, long, help = "Update to beta version (if available)")]
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 platform = capitalize(&sys_info::os_type().unwrap_or_else(|_| "Unknown".to_string()));
let packed_temp = download(
&http,
HOP_CLI_DOWNLOAD_URL,
&format!("v{version}"),
&format!("hop-{ARCH}-{platform}"),
)
.await?;
let unpacked = unpack(&packed_temp, "hop").await?;
fs::remove_file(packed_temp).await?;
let mut non_elevated_args: Vec<OsString> = vec![];
let mut elevated_args: Vec<OsString> = vec![];
let mut current = std::env::current_exe()?
.canonicalize()?
.to_string_lossy()
.to_string();
if current.starts_with(r"\\\\?\\") {
current = current[7..].to_string();
} else if current.starts_with(r"\\?\") {
current = current[4..].to_string();
}
let current = PathBuf::from(current);
log::debug!("Current executable: {current:?}");
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(())
}