novel_cli/cmd/
update.rs

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
use std::env;

use clap::Args;
use color_eyre::eyre::{Context, Ok, Result};
use fluent_templates::Loader;
use self_update::{backends::github, cargo_crate_version};
use tokio::task;
use url::Url;

use crate::{LANG_ID, LOCALES};

#[must_use]
#[derive(Args)]
#[command(about = LOCALES.lookup(&LANG_ID, "update_command"))]
pub struct Update {
    #[arg(long, num_args = 0..=1, default_missing_value = super::DEFAULT_PROXY_SURGE,
        help = LOCALES.lookup(&LANG_ID, "proxy"))]
    pub proxy: Option<Url>,
}

pub async fn execute(config: Update) -> Result<()> {
    if let Some(proxy) = config.proxy {
        env::set_var("HTTP_PROXY", proxy.to_string());
        env::set_var("HTTPS_PROXY", proxy.to_string());
    }

    let token = std::env::var("NOVEL_GITHUB_TOKEN")
        .or(std::env::var("GITHUB_TOKEN"))
        .wrap_err("The GITHUB_TOKEN environment variable was not found")?;

    task::spawn_blocking(move || {
        github::Update::configure()
            .repo_owner("novel-rs")
            .repo_name("cli")
            .bin_name(env!("CARGO_PKG_NAME"))
            .show_download_progress(true)
            .current_version(cargo_crate_version!())
            .auth_token(&token)
            .build()?
            .update()?;

        Ok(())
    })
    .await??;

    Ok(())
}