#![allow(non_local_definitions)]
use std::path::PathBuf;
use abscissa_core::{config::Override, Command, Configurable, FrameworkError, Runnable};
use crate::config::ZakuradConfig;
pub use self::{entry_point::EntryPoint, start::StartCmd};
use self::{
copy_state::CopyStateCmd, generate::GenerateCmd, prune_state::PruneStateCmd,
rollback_state::RollbackStateCmd, tip_height::TipHeightCmd,
validate_vct_sprout_history::ValidateVctSproutHistoryCmd,
};
pub mod start;
mod copy_state;
mod entry_point;
mod generate;
pub mod prune_state;
pub mod rollback_state;
mod tip_height;
mod validate_vct_sprout_history;
#[cfg(test)]
mod tests;
use ZakuradCmd::*;
pub const CONFIG_FILE: &str = "zakura.toml";
const LEGACY_CONFIG_FILE: &str = "zebrad.toml";
#[derive(Command, Debug, clap::Subcommand)]
pub enum ZakuradCmd {
CopyState(CopyStateCmd),
Generate(GenerateCmd),
PruneState(PruneStateCmd),
RollbackState(RollbackStateCmd),
Start(StartCmd),
TipHeight(TipHeightCmd),
ValidateVctSproutHistory(ValidateVctSproutHistoryCmd),
}
impl ZakuradCmd {
pub(crate) fn is_server(&self) -> bool {
match self {
CopyState(_) | Start(_) => true,
Generate(_)
| PruneState(_)
| RollbackState(_)
| TipHeight(_)
| ValidateVctSproutHistory(_) => false,
}
}
pub(crate) fn uses_intro(&self) -> bool {
match self {
Start(_) => true,
CopyState(_)
| Generate(_)
| PruneState(_)
| RollbackState(_)
| TipHeight(_)
| ValidateVctSproutHistory(_) => false,
}
}
pub(crate) fn should_ignore_load_config_error(&self) -> bool {
matches!(self, ZakuradCmd::Generate(_))
}
pub(crate) fn default_tracing_filter(&self, verbose: bool) -> &'static str {
let only_show_warnings = match self {
Generate(_)
| PruneState(_)
| RollbackState(_)
| TipHeight(_)
| ValidateVctSproutHistory(_) => true,
CopyState(_) | Start(_) => false,
};
if only_show_warnings && !verbose {
"warn"
} else if only_show_warnings || !verbose {
"info"
} else {
"debug"
}
}
}
impl Runnable for ZakuradCmd {
fn run(&self) {
match self {
CopyState(cmd) => cmd.run(),
Generate(cmd) => cmd.run(),
PruneState(cmd) => cmd.run(),
RollbackState(cmd) => cmd.run(),
Start(cmd) => cmd.run(),
TipHeight(cmd) => cmd.run(),
ValidateVctSproutHistory(cmd) => cmd.run(),
}
}
}
impl Configurable<ZakuradConfig> for ZakuradCmd {
fn config_path(&self) -> Option<PathBuf> {
let if_exists = |f: PathBuf| if f.exists() { Some(f) } else { None };
dirs::preference_dir().and_then(|path| {
let config_path = path.join(CONFIG_FILE);
if let Some(config_path) = if_exists(config_path) {
return Some(config_path);
}
let legacy_config_path = path.join(LEGACY_CONFIG_FILE);
if let Some(legacy_config_path) = if_exists(legacy_config_path) {
tracing::warn!(
config_file = ?legacy_config_path,
"zebrad.toml is deprecated; rename this config file to zakura.toml"
);
return Some(legacy_config_path);
}
None
})
}
fn process_config(&self, config: ZakuradConfig) -> Result<ZakuradConfig, FrameworkError> {
match self {
ZakuradCmd::Start(cmd) => cmd.override_config(config),
_ => Ok(config),
}
}
}