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