2019-08-29 14:46:54 -07:00
|
|
|
//! Zebrad Subcommands
|
|
|
|
|
2021-11-19 15:02:56 -08:00
|
|
|
mod download;
|
2020-06-18 11:38:46 -07:00
|
|
|
mod generate;
|
2019-08-29 14:46:54 -07:00
|
|
|
mod start;
|
|
|
|
mod version;
|
|
|
|
|
2020-07-16 00:04:42 -07:00
|
|
|
use self::ZebradCmd::*;
|
2021-11-19 15:02:56 -08:00
|
|
|
use self::{download::DownloadCmd, generate::GenerateCmd, start::StartCmd, version::VersionCmd};
|
2020-07-16 00:04:42 -07:00
|
|
|
|
2020-12-01 13:20:53 -08:00
|
|
|
use crate::config::ZebradConfig;
|
2020-07-16 00:04:42 -07:00
|
|
|
|
2019-08-29 14:46:54 -07:00
|
|
|
use abscissa_core::{
|
|
|
|
config::Override, Command, Configurable, FrameworkError, Help, Options, Runnable,
|
|
|
|
};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
/// Zebrad Configuration Filename
|
|
|
|
pub const CONFIG_FILE: &str = "zebrad.toml";
|
|
|
|
|
|
|
|
/// Zebrad Subcommands
|
2020-12-01 12:13:20 -08:00
|
|
|
#[derive(Command, Debug, Options)]
|
2019-08-29 14:46:54 -07:00
|
|
|
pub enum ZebradCmd {
|
2021-11-19 15:02:56 -08:00
|
|
|
/// The `download` subcommand
|
|
|
|
#[options(help = "pre-download required parameter files")]
|
|
|
|
Download(DownloadCmd),
|
|
|
|
|
2020-06-18 11:38:46 -07:00
|
|
|
/// The `generate` subcommand
|
2019-10-18 17:04:41 -07:00
|
|
|
#[options(help = "generate a skeleton configuration")]
|
2020-06-18 11:38:46 -07:00
|
|
|
Generate(GenerateCmd),
|
2019-10-18 17:04:41 -07:00
|
|
|
|
2020-02-18 11:55:55 -08:00
|
|
|
/// The `help` subcommand
|
|
|
|
#[options(help = "get usage information")]
|
|
|
|
Help(Help<Self>),
|
|
|
|
|
|
|
|
/// The `start` subcommand
|
|
|
|
#[options(help = "start the application")]
|
2020-06-16 11:02:01 -07:00
|
|
|
Start(StartCmd),
|
2020-02-18 11:55:55 -08:00
|
|
|
|
|
|
|
/// The `version` subcommand
|
|
|
|
#[options(help = "display version information")]
|
|
|
|
Version(VersionCmd),
|
2019-08-29 14:46:54 -07:00
|
|
|
}
|
|
|
|
|
2020-07-14 23:16:07 -07:00
|
|
|
impl ZebradCmd {
|
2020-07-16 00:04:42 -07:00
|
|
|
/// Returns true if this command is a server command.
|
|
|
|
///
|
|
|
|
/// For example, `Start` acts as a Zcash node.
|
|
|
|
pub(crate) fn is_server(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
// List all the commands, so new commands have to make a choice here
|
2020-09-18 12:18:22 -07:00
|
|
|
Start(_) => true,
|
2021-11-19 15:02:56 -08:00
|
|
|
Download(_) | Generate(_) | Help(_) | Version(_) => false,
|
2020-12-01 12:13:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Runnable for ZebradCmd {
|
|
|
|
fn run(&self) {
|
|
|
|
match self {
|
2021-11-19 15:02:56 -08:00
|
|
|
Download(cmd) => cmd.run(),
|
2020-12-01 12:13:20 -08:00
|
|
|
Generate(cmd) => cmd.run(),
|
|
|
|
ZebradCmd::Help(cmd) => cmd.run(),
|
|
|
|
Start(cmd) => cmd.run(),
|
|
|
|
Version(cmd) => cmd.run(),
|
2020-07-16 00:04:42 -07:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 23:16:07 -07:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:46:54 -07:00
|
|
|
/// This trait allows you to define how application configuration is loaded.
|
|
|
|
impl Configurable<ZebradConfig> for ZebradCmd {
|
|
|
|
/// Location of the configuration file
|
|
|
|
fn config_path(&self) -> Option<PathBuf> {
|
2019-09-09 13:05:42 -07:00
|
|
|
let if_exists = |f: PathBuf| if f.exists() { Some(f) } else { None };
|
|
|
|
|
2020-07-23 04:12:20 -07:00
|
|
|
dirs::preference_dir()
|
|
|
|
.map(|path| path.join(CONFIG_FILE))
|
|
|
|
.and_then(if_exists)
|
2020-06-23 11:56:00 -07:00
|
|
|
|
|
|
|
// Note: Changes in how configuration is loaded may need usage
|
|
|
|
// edits in generate.rs
|
2019-08-29 14:46:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply changes to the config after it's been loaded, e.g. overriding
|
|
|
|
/// values in a config file using command-line options.
|
|
|
|
///
|
|
|
|
/// This can be safely deleted if you don't want to override config
|
|
|
|
/// settings from command-line options.
|
2019-09-09 13:05:42 -07:00
|
|
|
fn process_config(&self, config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
|
2019-08-29 14:46:54 -07:00
|
|
|
match self {
|
|
|
|
ZebradCmd::Start(cmd) => cmd.override_config(config),
|
|
|
|
_ => Ok(config),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|