Skip to main content

novel_cli/cmd/
update.rs

1use std::env;
2use std::path::PathBuf;
3
4use clap::Args;
5use color_eyre::eyre::{Context, Result};
6use fluent_templates::Loader;
7use novel_api::SelfUpdate;
8use semver::Version;
9use url::Url;
10
11use crate::{LANG_ID, LOCALES};
12
13#[must_use]
14#[derive(Args)]
15#[command(about = LOCALES.lookup(&LANG_ID, "update_command"))]
16pub struct Update {
17    #[arg(long, num_args = 0..=1, default_missing_value = super::DEFAULT_PROXY,
18        help = LOCALES.lookup(&LANG_ID, "proxy"))]
19    pub proxy: Option<Url>,
20
21    #[arg(long, default_value_t = false,
22        help = LOCALES.lookup(&LANG_ID, "no_proxy"))]
23    pub no_proxy: bool,
24
25    #[arg(long, num_args = 0..=1, default_missing_value = super::default_cert_path(),
26        help = super::cert_help_msg())]
27    pub cert: Option<PathBuf>,
28}
29
30pub async fn execute(config: Update) -> Result<()> {
31    let token = std::env::var("NOVEL_GITHUB_TOKEN")
32        .or(std::env::var("GITHUB_TOKEN_NOVEL"))
33        .or(std::env::var("GITHUB_TOKEN"))
34        .wrap_err("The GITHUB_TOKEN environment variable was not found")?;
35
36    SelfUpdate::builder()
37        .owner("novel-rs")
38        .repo("cli")
39        .bin_name(env!("CARGO_PKG_NAME"))
40        .auth_token(&token)
41        .current_version(Version::parse(env!("CARGO_PKG_VERSION"))?)
42        .show_release_body(true)
43        .maybe_proxy(config.proxy)
44        .no_proxy(config.no_proxy)
45        .maybe_cert_path(config.cert)
46        .build()
47        .await?
48        .update()
49        .await?;
50
51    Ok(())
52}