Skip to main content

zebrad/commands/
entry_point.rs

1//! Zebrad EntryPoint
2
3use abscissa_core::{Command, Configurable, FrameworkError, Runnable};
4use clap::Parser;
5use std::{ffi::OsString, path::PathBuf, sync::LazyLock};
6
7use crate::{application::build_version, config::ZebradConfig};
8
9use super::ZebradCmd;
10
11/// The build version that `--version` and `--help` print.
12static BUILD_VERSION: LazyLock<String> = LazyLock::new(|| build_version().to_string());
13
14/// Toplevel entrypoint command.
15///
16/// Handles obtaining toplevel help as well as verbosity settings.
17#[derive(Debug, clap::Parser)]
18#[clap(
19    version = BUILD_VERSION.as_str(),
20    author="Zcash Foundation <[email protected]>",
21    help_template = "\
22{name} {version}\n
23{author}\n
24{usage-heading} {usage}\n
25{all-args}\
26"
27)]
28pub struct EntryPoint {
29    /// Subcommand to execute.
30    ///
31    /// The `command` option will delegate option parsing to the command type,
32    /// starting at the first free argument. Defaults to start.
33    #[clap(subcommand)]
34    pub cmd: Option<ZebradCmd>,
35
36    /// Path to the configuration file
37    #[clap(long, short, help = "path to configuration file")]
38    pub config: Option<PathBuf>,
39
40    /// Increase verbosity setting
41    #[clap(long, short, help = "be verbose")]
42    pub verbose: bool,
43
44    /// Filter strings which override the config file and defaults
45    // This can be applied to the default start command if no subcommand is provided.
46    #[clap(long, help = "tracing filters which override the zebrad.toml config")]
47    filters: Vec<String>,
48}
49
50impl EntryPoint {
51    /// Borrow the command in the option
52    ///
53    /// # Panics
54    ///
55    /// If `cmd` is None
56    pub fn cmd(&self) -> &ZebradCmd {
57        self.cmd
58            .as_ref()
59            .expect("should default to start if not provided")
60    }
61
62    /// Returns a string that parses to the default subcommand
63    pub fn default_cmd_as_str() -> &'static str {
64        "start"
65    }
66
67    /// Checks if the provided arguments include a subcommand
68    fn should_add_default_subcommand(&self) -> bool {
69        self.cmd.is_none()
70    }
71
72    /// Process command arguments and insert the default subcommand
73    /// if no subcommand is provided.
74    pub fn process_cli_args(mut args: Vec<OsString>) -> clap::error::Result<Vec<OsString>> {
75        let entry_point = EntryPoint::try_parse_from(&args)?;
76
77        // Add the default subcommand to args after the top-level args if cmd is None
78        if entry_point.should_add_default_subcommand() {
79            args.push(EntryPoint::default_cmd_as_str().into());
80            // This duplicates the top-level filters args, but the tracing component only checks `StartCmd.filters`.
81            for filter in entry_point.filters {
82                args.push(filter.into())
83            }
84        }
85
86        Ok(args)
87    }
88}
89
90impl Runnable for EntryPoint {
91    fn run(&self) {
92        self.cmd().run()
93    }
94}
95
96impl Command for EntryPoint {
97    /// Name of this program as a string
98    fn name() -> &'static str {
99        ZebradCmd::name()
100    }
101
102    /// Description of this program
103    fn description() -> &'static str {
104        ZebradCmd::description()
105    }
106
107    /// Authors of this program
108    fn authors() -> &'static str {
109        ZebradCmd::authors()
110    }
111}
112
113impl Configurable<ZebradConfig> for EntryPoint {
114    /// Path to the command's configuration file
115    fn config_path(&self) -> Option<PathBuf> {
116        match &self.config {
117            // Use explicit `-c`/`--config` argument if passed
118            Some(cfg) => Some(cfg.clone()),
119
120            // Otherwise defer to the toplevel command's config path logic
121            None => self.cmd().config_path(),
122        }
123    }
124
125    /// Process the configuration after it has been loaded, potentially
126    /// modifying it or returning an error if options are incompatible
127    fn process_config(&self, config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
128        self.cmd().process_config(config)
129    }
130}