cuprated/config/
args.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::{io::Write, path::PathBuf, process::exit};

use clap::builder::TypedValueParser;
use serde_json::Value;

use cuprate_helper::network::Network;

use crate::{config::Config, constants::EXAMPLE_CONFIG, version::CupratedVersionInfo};

/// Cuprate Args.
#[derive(clap::Parser, Debug)]
#[command(about)]
pub struct Args {
    /// The network to run on.
    #[arg(
        long,
        default_value_t = Network::Mainnet,
        value_parser = clap::builder::PossibleValuesParser::new(["mainnet", "testnet", "stagenet"])
            .map(|s| s.parse::<Network>().unwrap()),
    )]
    pub network: Network,

    /// The amount of outbound clear-net connections to maintain.
    #[arg(long)]
    pub outbound_connections: Option<usize>,

    /// The PATH of the `cuprated` config file.
    #[arg(long)]
    pub config_file: Option<PathBuf>,

    /// Generate a config file and print it to stdout.
    #[arg(long)]
    pub generate_config: bool,

    /// Print misc version information in JSON.
    #[arg(short, long)]
    pub version: bool,
}

impl Args {
    /// Complete any quick requests asked for in [`Args`].
    ///
    /// May cause the process to [`exit`].
    pub fn do_quick_requests(&self) {
        if self.version {
            let version_info = CupratedVersionInfo::new();
            let json = serde_json::to_string_pretty(&version_info).unwrap();
            println!("{json}");
            exit(0);
        }

        if self.generate_config {
            println!("{EXAMPLE_CONFIG}");
            exit(0);
        }
    }

    /// Apply the [`Args`] to the given [`Config`].
    ///
    /// This may exit the program if a config value was set that requires an early exit.
    pub const fn apply_args(&self, mut config: Config) -> Config {
        config.network = self.network;

        if let Some(outbound_connections) = self.outbound_connections {
            config.p2p.clear_net.general.outbound_connections = outbound_connections;
        }

        config
    }
}