Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement query client state command #132

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading