Skip to content

Commit

Permalink
Implement query client state command (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
romac authored Feb 9, 2024
1 parent f5bbd69 commit 58d221d
Show file tree
Hide file tree
Showing 23 changed files with 460 additions and 38 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/cli/cli-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ cgp-core = { workspace = true }
clap = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
oneline-eyre = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
21 changes: 17 additions & 4 deletions crates/cli/cli-framework/src/application/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ use hermes_relayer_runtime::types::runtime::HermesRuntime;
use crate::application::log::{enable_ansi, install_logger};
use crate::application::Application;
use crate::config::Config;
use crate::output;
use crate::Result;

pub fn boot<A>() -> Result<()>
where
A: Application,
{
let with_color = enable_ansi();
install_logger(with_color);

oneline_eyre::install()?;

let app = A::parse_from_env();
let config_path = app.config_path();

let with_color = enable_ansi();
let with_json = app.json_output();
install_logger(with_color, with_json);

output::set_json(with_json);

let config =
A::Config::load_from_path(config_path).map_err(|e| eyre!("failed to load config: {e}"))?;

Expand All @@ -32,8 +36,17 @@ where
let rt = HermesRuntime::new(Arc::new(rt));

rt.runtime
.block_on(app.run(rt.clone(), config))
.block_on(run(rt.clone(), app, config))
.map_err(|e| eyre!("Hermes command exited with an error: {e}"))?;

Ok(())
}

pub async fn run<A>(rt: HermesRuntime, app: A, config: A::Config) -> Result<()>
where
A: Application,
{
let output = app.run(rt, config).await?;
output.print();
Ok(())
}
20 changes: 12 additions & 8 deletions crates/cli/cli-framework/src/application/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
Install the [`tracing_subscriber`] logger handlers so that logs will
be displayed during test.
*/
pub fn install_logger(with_color: bool) {
pub fn install_logger(with_color: bool, with_json: bool) {
use tracing::level_filters::LevelFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::{fmt, registry};

// Use log level INFO by default if RUST_LOG is not set.
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();

let layer = tracing_subscriber::fmt::layer()
.with_ansi(with_color)
.with_target(false);
if with_json {
let fmt_layer = fmt::layer().with_target(false).json();
registry().with(env_filter).with(fmt_layer).init();
} else {
let fmt_layer = fmt::layer()
.with_ansi(with_color)
.with_target(false)
.compact();

tracing_subscriber::registry()
.with(env_filter)
.with(layer)
.init();
registry().with(env_filter).with(fmt_layer).init();
};
}

/// Check if both stdout and stderr are proper terminal (tty),
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/cli-framework/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use hermes_relayer_runtime::types::runtime::HermesRuntime;

use crate::command::Runnable;
use crate::config::Config;
use crate::output::Output;
use crate::Result;

#[async_trait]
Expand All @@ -21,5 +22,7 @@ pub trait Application: Sized {

fn config_path(&self) -> &Path;

async fn run(&self, runtime: HermesRuntime, config: Self::Config) -> Result<()>;
fn json_output(&self) -> bool;

async fn run(&self, runtime: HermesRuntime, config: Self::Config) -> Result<Output>;
}
3 changes: 2 additions & 1 deletion crates/cli/cli-framework/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use cgp_core::async_trait;
use hermes_cosmos_relayer::contexts::builder::CosmosBuilder;

use crate::output::Output;
use crate::Result;

#[async_trait]
pub trait Runnable {
async fn run(&self, builder: CosmosBuilder) -> Result<()>;
async fn run(&self, builder: CosmosBuilder) -> Result<Output>;
}
1 change: 1 addition & 0 deletions crates/cli/cli-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
pub mod application;
pub mod command;
pub mod config;
pub mod output;

pub type Result<T> = oneline_eyre::Result<T>;
Loading

0 comments on commit 58d221d

Please sign in to comment.