1use std::sync::Arc;
7
8use indicatif::ProgressBar;
9
10use crate::{
11 constants::PRODUCT_NAME_LONG,
12 self_update::SelfUpdate,
13 update_service::UpdateService,
14 util::{errors::AnyError, http::ReqwestSimpleHttp, input::ProgressBarReporter},
15};
16
17use super::{args::StandaloneUpdateArgs, CommandContext};
18
19pub async fn update(ctx: CommandContext, args: StandaloneUpdateArgs) -> Result<i32, AnyError> {
20 let update_service = UpdateService::new(
21 ctx.log.clone(),
22 Arc::new(ReqwestSimpleHttp::with_client(ctx.http.clone())),
23 );
24 let update_service = SelfUpdate::new(&update_service)?;
25
26 let _ = update_service.cleanup_old_update();
27
28 let current_version = update_service.get_current_release().await?;
29 if update_service.is_up_to_date_with(¤t_version) {
30 ctx.log.result(format!(
31 "{} is already to to date ({})",
32 PRODUCT_NAME_LONG, current_version.commit
33 ));
34 return Ok(1);
35 }
36
37 if args.check {
38 ctx.log
39 .result(format!("Update to {} is available", current_version));
40 return Ok(0);
41 }
42
43 let pb = ProgressBar::new(1);
44 pb.set_message("Downloading...");
45 update_service
46 .do_update(¤t_version, ProgressBarReporter::from(pb))
47 .await?;
48 ctx.log
49 .result(format!("Successfully updated to {}", current_version));
50
51 Ok(0)
52}