zebra/zebrad/src/commands.rs

91 lines
2.6 KiB
Rust
Raw Normal View History

//! Zebrad Subcommands
2020-06-18 11:38:46 -07:00
mod generate;
mod revhex;
mod seed;
mod start;
mod version;
use self::ZebradCmd::*;
use self::{
2020-08-04 23:15:46 -07:00
generate::GenerateCmd, revhex::RevhexCmd, seed::SeedCmd, start::StartCmd, version::VersionCmd,
};
use crate::config::ZebradConfig;
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
#[derive(Command, Debug, Options, Runnable)]
pub enum ZebradCmd {
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
/// The `help` subcommand
#[options(help = "get usage information")]
Help(Help<Self>),
/// The `revhex` subcommand
#[options(help = "reverses the endianness of a hex string, like a block or transaction hash")]
Revhex(RevhexCmd),
/// The `seed` subcommand
#[options(help = "dns seeder")]
Seed(SeedCmd),
/// The `start` subcommand
#[options(help = "start the application")]
2020-06-16 11:02:01 -07:00
Start(StartCmd),
/// The `version` subcommand
#[options(help = "display version information")]
Version(VersionCmd),
}
impl ZebradCmd {
/// 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-08-04 23:15:46 -07:00
Seed(_) | Start(_) => true,
Generate(_) | Help(_) | Revhex(_) | Version(_) => false,
}
}
}
/// 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> {
let if_exists = |f: PathBuf| if f.exists() { Some(f) } else { None };
dirs::preference_dir()
.map(|path| path.join(CONFIG_FILE))
.and_then(if_exists)
// Note: Changes in how configuration is loaded may need usage
// edits in generate.rs
}
/// 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.
fn process_config(&self, config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
match self {
ZebradCmd::Start(cmd) => cmd.override_config(config),
_ => Ok(config),
}
}
}