novel_cli/cmd/
update.rs

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        unsafe {
24            env::set_var("HTTP_PROXY", proxy.to_string());
25            env::set_var("HTTPS_PROXY", proxy.to_string());
26        }
27    }
28
29    let token = std::env::var("NOVEL_GITHUB_TOKEN")
30        .or(std::env::var("GITHUB_TOKEN_NOVEL"))
31        .or(std::env::var("GITHUB_TOKEN"))
32        .wrap_err("The GITHUB_TOKEN environment variable was not found")?;
33
34    task::spawn_blocking(move || {
35        github::Update::configure()
36            .repo_owner("novel-rs")
37            .repo_name("cli")
38            .bin_name(env!("CARGO_PKG_NAME"))
39            .show_download_progress(true)
40            .current_version(self_update::cargo_crate_version!())
41            .auth_token(&token)
42            .build()?
43            .update()?;
44
45        eyre::Ok(())
46    })
47    .await??;
48
49    Ok(())
50}