zebrad/commands/
entry_point.rs1use 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
11static BUILD_VERSION: LazyLock<String> = LazyLock::new(|| build_version().to_string());
13
14#[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 #[clap(subcommand)]
34 pub cmd: Option<ZebradCmd>,
35
36 #[clap(long, short, help = "path to configuration file")]
38 pub config: Option<PathBuf>,
39
40 #[clap(long, short, help = "be verbose")]
42 pub verbose: bool,
43
44 #[clap(long, help = "tracing filters which override the zebrad.toml config")]
47 filters: Vec<String>,
48}
49
50impl EntryPoint {
51 pub fn cmd(&self) -> &ZebradCmd {
57 self.cmd
58 .as_ref()
59 .expect("should default to start if not provided")
60 }
61
62 pub fn default_cmd_as_str() -> &'static str {
64 "start"
65 }
66
67 fn should_add_default_subcommand(&self) -> bool {
69 self.cmd.is_none()
70 }
71
72 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 if entry_point.should_add_default_subcommand() {
79 args.push(EntryPoint::default_cmd_as_str().into());
80 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 fn name() -> &'static str {
99 ZebradCmd::name()
100 }
101
102 fn description() -> &'static str {
104 ZebradCmd::description()
105 }
106
107 fn authors() -> &'static str {
109 ZebradCmd::authors()
110 }
111}
112
113impl Configurable<ZebradConfig> for EntryPoint {
114 fn config_path(&self) -> Option<PathBuf> {
116 match &self.config {
117 Some(cfg) => Some(cfg.clone()),
119
120 None => self.cmd().config_path(),
122 }
123 }
124
125 fn process_config(&self, config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
128 self.cmd().process_config(config)
129 }
130}