Skip to content

Commit

Permalink
feat(cli): support custom start shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-dn committed Sep 19, 2023
1 parent eff89a8 commit ce7c261
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
29 changes: 24 additions & 5 deletions core/cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use clap::Parser;
use lightning_interfaces::infu_collection::Collection;
use lightning_signer::Signer;
use log::LevelFilter;
Expand Down Expand Up @@ -30,13 +31,15 @@ impl Cli {
Self { args }
}

pub fn parse() -> Self {
Self {
args: Args::parse(),
}
}

pub async fn exec(self) -> Result<()> {
self.setup();

let config_path = ResolvedPathBuf::try_from(self.args.config.as_str())
.expect("Failed to resolve config path");
ensure_parent_exist(&config_path)?;

let config_path = self.resolve_config_path()?;
match self.args.with_mock_consensus {
true => {
log::info!("Using MockConsensus");
Expand All @@ -46,6 +49,15 @@ impl Cli {
}
}

pub async fn exec_with_custom_start_shutdown<C>(self, cb: CustomStartShutdown<C>) -> Result<()>
where
C: Collection<ConfigProviderInterface = TomlConfigProvider<C>, SignerInterface = Signer<C>>,
{
self.setup();
let config_path = self.resolve_config_path()?;
self.run::<C>(config_path, Some(cb)).await
}

async fn run<C>(
self,
config_path: ResolvedPathBuf,
Expand Down Expand Up @@ -133,4 +145,11 @@ impl Cli {

log4rs::init_config(config).unwrap();
}

fn resolve_config_path(&self) -> Result<ResolvedPathBuf> {
let config_path = ResolvedPathBuf::try_from(self.args.config.as_str())
.expect("Failed to resolve config path");
ensure_parent_exist(&config_path)?;
Ok(config_path)
}
}
8 changes: 8 additions & 0 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod args;
pub mod cli;
mod commands;
pub mod config;
mod shutdown;
mod testnet_sync;
pub mod types;
mod utils;
2 changes: 1 addition & 1 deletion core/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod args;
mod cli;
pub mod cli;
mod commands;
mod config;
mod shutdown;
Expand Down

0 comments on commit ce7c261

Please sign in to comment.