feat: add cli ext event hooks example (#4935)

This commit is contained in:
Matthias Seitz
2023-10-06 20:05:03 +02:00
committed by GitHub
parent c825aafbc8
commit 36306ce916
6 changed files with 108 additions and 1 deletions

View File

@ -8,7 +8,7 @@ use clap::Args;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_tasks::TaskSpawner;
use std::fmt;
use std::{fmt, marker::PhantomData};
/// A trait that allows for extending parts of the CLI with additional functionality.
///
@ -119,6 +119,14 @@ impl RethNodeCommandConfig for DefaultRethNodeCommandConfig {}
impl RethNodeCommandConfig for () {}
/// A helper type for [RethCliExt] extension that don't require any additional clap Arguments.
#[derive(Debug, Clone, Copy)]
pub struct NoArgsCliExt<Conf>(PhantomData<Conf>);
impl<Conf: RethNodeCommandConfig> RethCliExt for NoArgsCliExt<Conf> {
type Node = NoArgs<Conf>;
}
/// A helper struct that allows for wrapping a [RethNodeCommandConfig] value without providing
/// additional CLI arguments.
///
@ -214,6 +222,12 @@ impl<T: RethNodeCommandConfig> RethNodeCommandConfig for NoArgs<T> {
}
}
impl<T> From<T> for NoArgs<T> {
fn from(value: T) -> Self {
Self::with(value)
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -113,6 +113,15 @@ impl<Ext: RethCliExt> Cli<Ext> {
reth_tracing::init(layers);
Ok(guard.flatten())
}
/// Configures the given node extension.
pub fn with_node_extension<C>(mut self, conf: C) -> Self
where
C: Into<Ext::Node>,
{
self.command.set_node_extension(conf.into());
self
}
}
/// Convenience function for parsing CLI options, set up logging and run the chosen command.
@ -156,6 +165,17 @@ pub enum Commands<Ext: RethCliExt = ()> {
Recover(recover::Command),
}
impl<Ext: RethCliExt> Commands<Ext> {
/// Sets the node extension if it is the [NodeCommand](node::NodeCommand).
///
/// This is a noop if the command is not the [NodeCommand](node::NodeCommand).
pub fn set_node_extension(&mut self, ext: Ext::Node) {
if let Commands::Node(command) = self {
command.ext = ext
}
}
}
/// The log configuration.
#[derive(Debug, Args)]
#[command(next_help_heading = "Logging")]