#![allow(non_local_definitions)]
use std::path::PathBuf;
use abscissa_core::{config::Override, Command, Configurable, FrameworkError, Runnable};
use crate::config::ZebradConfig;
pub use self::{entry_point::EntryPoint, start::StartCmd};
use self::{copy_state::CopyStateCmd, generate::GenerateCmd, tip_height::TipHeightCmd};
pub mod start;
mod copy_state;
mod entry_point;
mod generate;
mod tip_height;
#[cfg(test)]
mod tests;
use ZebradCmd::*;
pub const CONFIG_FILE: &str = "zebrad.toml";
#[derive(Command, Debug, clap::Subcommand)]
pub enum ZebradCmd {
CopyState(CopyStateCmd),
Generate(GenerateCmd),
Start(StartCmd),
TipHeight(TipHeightCmd),
}
impl ZebradCmd {
pub(crate) fn is_server(&self) -> bool {
match self {
CopyState(_) | Start(_) => true,
Generate(_) | TipHeight(_) => false,
}
}
pub(crate) fn uses_intro(&self) -> bool {
match self {
Start(_) => true,
CopyState(_) | Generate(_) | TipHeight(_) => false,
}
}
pub(crate) fn should_ignore_load_config_error(&self) -> bool {
matches!(self, ZebradCmd::Generate(_))
}
pub(crate) fn default_tracing_filter(&self, verbose: bool) -> &'static str {
let only_show_warnings = match self {
Generate(_) | TipHeight(_) => true,
CopyState(_) | Start(_) => false,
};
if only_show_warnings && !verbose {
"warn"
} else if only_show_warnings || !verbose {
"info"
} else {
"debug"
}
}
}
impl Runnable for ZebradCmd {
fn run(&self) {
match self {
CopyState(cmd) => cmd.run(),
Generate(cmd) => cmd.run(),
Start(cmd) => cmd.run(),
TipHeight(cmd) => cmd.run(),
}
}
}
impl Configurable<ZebradConfig> for ZebradCmd {
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)
}
fn process_config(&self, config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
match self {
ZebradCmd::Start(cmd) => cmd.override_config(config),
_ => Ok(config),
}
}
}