use std::path::PathBuf;
use std::process;
use abscissa_core::clap::Parser;
use abscissa_core::{Command, Configurable, FrameworkError, Runnable};
use clap::{IntoApp, ValueEnum};
use ibc_relayer::config::Config;
use ibc_relayer::util::debug_section::DebugSection;
use crate::commands::CliCmd;
#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum CliDebugSection {
Rpc,
Profiling,
ProfilingJson,
}
impl From<CliDebugSection> for DebugSection {
fn from(section: CliDebugSection) -> Self {
match section {
CliDebugSection::Rpc => DebugSection::Rpc,
CliDebugSection::Profiling => DebugSection::Profiling,
CliDebugSection::ProfilingJson => DebugSection::ProfilingJson,
}
}
}
#[derive(Command, Debug, Parser)]
#[clap(author, about, version)]
pub struct EntryPoint {
#[clap(long = "config", help = "Path to configuration file")]
pub config: Option<PathBuf>,
#[clap(long = "json", help = "Enable JSON output")]
pub json: bool,
#[clap(
long = "debug",
help = "Enable debug output for the given section(s), comma separated, can be repeated.",
value_enum,
value_delimiter = ','
)]
pub debug: Vec<CliDebugSection>,
#[clap(subcommand)]
pub command: Option<CliCmd>,
}
impl Runnable for EntryPoint {
fn run(&self) {
match &self.command {
Some(cmd) => cmd.run(),
None => {
EntryPoint::command().print_help().unwrap();
process::exit(0);
}
}
}
}
impl Configurable<Config> for EntryPoint {
fn config_path(&self) -> Option<PathBuf> {
match &self.command {
Some(CliCmd::Completions(_)) | Some(CliCmd::Version(_)) => {
return None;
}
_ => {}
}
match &self.config {
Some(cfg) => Some(cfg.clone()),
None => self.command.as_ref().and_then(|cmd| cmd.config_path()),
}
}
fn process_config(&self, config: Config) -> Result<Config, FrameworkError> {
match &self.command {
Some(cmd) => cmd.process_config(config),
None => Ok(config),
}
}
}