cuprated/config/
args.rs

1use std::{io::Write, path::PathBuf, process::exit};
2
3use clap::builder::TypedValueParser;
4use serde_json::Value;
5
6use cuprate_helper::network::Network;
7
8use crate::{config::Config, constants::EXAMPLE_CONFIG, version::CupratedVersionInfo};
9
10/// Cuprate Args.
11#[derive(clap::Parser, Debug)]
12#[command(about)]
13pub struct Args {
14    /// The network to run on.
15    #[arg(
16        long,
17        default_value_t = Network::Mainnet,
18        value_parser = clap::builder::PossibleValuesParser::new(["mainnet", "testnet", "stagenet"])
19            .map(|s| s.parse::<Network>().unwrap()),
20    )]
21    pub network: Network,
22
23    /// Disable fast sync, all past blocks will undergo full verification when syncing.
24    ///
25    /// This significantly increases initial sync time. This provides no extra security, you just
26    /// have to trust the devs to insert the correct hashes (which are verifiable).
27    #[arg(long)]
28    no_fast_sync: bool,
29
30    /// The amount of outbound clear-net connections to maintain.
31    #[arg(long)]
32    pub outbound_connections: Option<usize>,
33
34    /// The PATH of the `cuprated` config file.
35    #[arg(long)]
36    pub config_file: Option<PathBuf>,
37
38    /// Generate a config file and print it to stdout.
39    #[arg(long)]
40    pub generate_config: bool,
41
42    /// Stops the missing config warning and startup delay if a config file is missing.
43    #[arg(long)]
44    pub skip_config_warning: bool,
45
46    /// Print misc version information in JSON.
47    #[arg(short, long)]
48    pub version: bool,
49}
50
51impl Args {
52    /// Complete any quick requests asked for in [`Args`].
53    ///
54    /// May cause the process to [`exit`].
55    pub fn do_quick_requests(&self) {
56        if self.version {
57            let version_info = CupratedVersionInfo::new();
58            let json = serde_json::to_string_pretty(&version_info).unwrap();
59            println!("{json}");
60            exit(0);
61        }
62
63        if self.generate_config {
64            println!("{EXAMPLE_CONFIG}");
65            exit(0);
66        }
67    }
68
69    /// Apply the [`Args`] to the given [`Config`].
70    ///
71    /// This may exit the program if a config value was set that requires an early exit.
72    pub const fn apply_args(&self, mut config: Config) -> Config {
73        config.network = self.network;
74        config.no_fast_sync = config.no_fast_sync || self.no_fast_sync;
75
76        if let Some(outbound_connections) = self.outbound_connections {
77            config.p2p.clear_net.general.outbound_connections = outbound_connections;
78        }
79
80        config
81    }
82}