novel_cli/cmd/
sign.rs

1use std::{path::PathBuf, sync::Arc};
2
3use clap::Args;
4use color_eyre::eyre::Result;
5use fluent_templates::Loader;
6use novel_api::{CiweimaoClient, CiyuanjiClient, Client, SfacgClient};
7use url::Url;
8
9use crate::{cmd::Source, utils, LANG_ID, LOCALES};
10
11#[must_use]
12#[derive(Args)]
13#[command(arg_required_else_help = true,
14    about = LOCALES.lookup(&LANG_ID, "sign_command"))]
15pub struct Sign {
16    #[arg(short, long,
17        help = LOCALES.lookup(&LANG_ID, "source"))]
18    pub source: Source,
19
20    #[arg(long, default_value_t = false,
21        help = LOCALES.lookup(&LANG_ID, "ignore_keyring"))]
22    pub ignore_keyring: bool,
23
24    #[arg(long, num_args = 0..=1, default_missing_value = super::DEFAULT_PROXY,
25        help = LOCALES.lookup(&LANG_ID, "proxy"))]
26    pub proxy: Option<Url>,
27
28    #[arg(long, default_value_t = false,
29        help = LOCALES.lookup(&LANG_ID, "no_proxy"))]
30    pub no_proxy: bool,
31
32    #[arg(long, num_args = 0..=1, default_missing_value = super::default_cert_path(),
33        help = super::cert_help_msg())]
34    pub cert: Option<PathBuf>,
35}
36
37pub async fn execute(config: Sign) -> Result<()> {
38    match config.source {
39        Source::Sfacg => {
40            let mut client = SfacgClient::new().await?;
41            super::set_options(&mut client, &config.proxy, &config.no_proxy, &config.cert);
42            utils::log_in(&client, &config.source, config.ignore_keyring).await?;
43            do_execute(client, config).await?
44        }
45        Source::Ciweimao => {
46            let mut client = CiweimaoClient::new().await?;
47            super::set_options(&mut client, &config.proxy, &config.no_proxy, &config.cert);
48            utils::log_in(&client, &config.source, config.ignore_keyring).await?;
49            do_execute(client, config).await?
50        }
51        Source::Ciyuanji => {
52            let mut client = CiyuanjiClient::new().await?;
53            super::set_options(&mut client, &config.proxy, &config.no_proxy, &config.cert);
54            utils::log_in_without_password(&client).await?;
55            do_execute(client, config).await?
56        }
57    }
58
59    Ok(())
60}
61
62async fn do_execute<T>(client: T, config: Sign) -> Result<()>
63where
64    T: Client + Send + Sync + 'static,
65{
66    let client = Arc::new(client);
67    super::handle_ctrl_c(&client);
68
69    client.sign_in().await?;
70
71    println!(
72        "{} {}",
73        config.source.as_ref(),
74        LOCALES.lookup(&LANG_ID, "sign_in_successfully")
75    );
76    println!(
77        "{}{}",
78        LOCALES.lookup(&LANG_ID, "current_money"),
79        client.money().await?
80    );
81
82    Ok(())
83}