1use std::env;
2
3use clap::Args;
4use color_eyre::eyre::{self, Context, Result};
5use fluent_templates::Loader;
6use self_update::backends::github;
7use tokio::task;
8use url::Url;
9
10use crate::{LANG_ID, LOCALES};
11
12#[must_use]
13#[derive(Args)]
14#[command(about = LOCALES.lookup(&LANG_ID, "update_command"))]
15pub struct Update {
16 #[arg(long, num_args = 0..=1, default_missing_value = super::DEFAULT_PROXY_SURGE,
17 help = LOCALES.lookup(&LANG_ID, "proxy"))]
18 pub proxy: Option<Url>,
19}
20
21pub async fn execute(config: Update) -> Result<()> {
22 if let Some(proxy) = config.proxy {
23 env::set_var("HTTP_PROXY", proxy.to_string());
24 env::set_var("HTTPS_PROXY", proxy.to_string());
25 }
26
27 let token = std::env::var("NOVEL_GITHUB_TOKEN")
28 .or(std::env::var("GITHUB_TOKEN_NOVEL"))
29 .or(std::env::var("GITHUB_TOKEN"))
30 .wrap_err("The GITHUB_TOKEN environment variable was not found")?;
31
32 task::spawn_blocking(move || {
33 github::Update::configure()
34 .repo_owner("novel-rs")
35 .repo_name("cli")
36 .bin_name(env!("CARGO_PKG_NAME"))
37 .show_download_progress(true)
38 .current_version(self_update::cargo_crate_version!())
39 .auth_token(&token)
40 .build()?
41 .update()?;
42
43 eyre::Ok(())
44 })
45 .await??;
46
47 Ok(())
48}