use std::env;
use std::path::PathBuf;
use clap::Args;
use color_eyre::eyre::{Context, Result};
use fluent_templates::Loader;
use novel_api::SelfUpdate;
use semver::Version;
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,
help = LOCALES.lookup(&LANG_ID, "proxy"))]
pub proxy: Option<Url>,
#[arg(long, default_value_t = false,
help = LOCALES.lookup(&LANG_ID, "no_proxy"))]
pub no_proxy: bool,
#[arg(long, num_args = 0..=1, default_missing_value = super::default_cert_path(),
help = super::cert_help_msg())]
pub cert: Option<PathBuf>,
}
pub async fn execute(config: Update) -> Result<()> {
let token = std::env::var("NOVEL_GITHUB_TOKEN")
.or(std::env::var("GITHUB_TOKEN_NOVEL"))
.or(std::env::var("GITHUB_TOKEN"))
.wrap_err("The GITHUB_TOKEN environment variable was not found")?;
SelfUpdate::builder()
.owner("novel-rs")
.repo("cli")
.bin_name(env!("CARGO_PKG_NAME"))
.auth_token(&token)
.current_version(Version::parse(env!("CARGO_PKG_VERSION"))?)
.show_release_body(true)
.maybe_proxy(config.proxy)
.no_proxy(config.no_proxy)
.maybe_cert_path(config.cert)
.build()
.await?
.update()
.await?;
Ok(())
}