Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Argument parsing via cli (#71)
Browse files Browse the repository at this point in the history
see #64 

Recreated PR to fix conflicts.

---------

Co-authored-by: Gianmarco Fraccaroli <>
  • Loading branch information
rllola authored Dec 20, 2023
1 parent 46f915f commit 5ef7553
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
runs-on: zondax-runners
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
# - uses: EmbarkStudios/cargo-deny-action@v1
- name: Scan for vulnerabilities
run: cargo deny check advisories
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
protoc --version
- name: Tests saving blocks
run: |
cargo test save_block -- --test-threads=1
cargo test --test save_block
- name: Tests getting blocks
run: |
cargo test block_tests
cargo test --test block_tests
70 changes: 70 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ tendermint = "0.34.0"
tendermint-config = "0.34.0"
tendermint-rpc = { version = "0.34.0", features = ["http-client"] }
tendermint-proto = "0.34.0"
clap = { version = "4.4.2", features = ["derive", "env"] }

[dev-dependencies]
criterion = { version = "0.5.1", features = [
Expand Down
75 changes: 74 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::Error;
use clap::{ArgAction, Parser};
use config::{Config, File};
use serde::Deserialize;
use std::{env, net::SocketAddr};
Expand Down Expand Up @@ -117,6 +118,42 @@ impl Default for DatabaseConfig {
}
}

#[derive(Debug, Deserialize, clap::Parser)]
pub struct CliSettings {
#[clap(long, env, default_value = "")]
pub log_level: String,
#[clap(long, env, default_value = DEFAULT_NETWORK)]
pub network: String,
#[clap(long, env, default_value = SERVER_ADDR)]
pub server_serve_at: String,
#[clap(long, env, default_value_t = SERVER_PORT)]
pub server_port: u16,
#[clap(long, env, default_value = "localhost")]
pub database_host: String,
#[clap(long, env, default_value = "postgres")]
pub database_user: String,
#[clap(long, env, default_value = "wow")]
pub database_password: String,
#[clap(long, env, default_value = "blockchain")]
pub database_dbname: String,
#[clap(long, env)]
pub database_connection_timeout: Option<u64>,
#[clap(long, env, default_value = TENDERMINT_ADDR)]
pub indexer_tendermint_addr: String,
#[clap(long, env, default_value_t = INDEXER_PORT)]
pub indexer_port: u16,
#[clap(long, env, action=ArgAction::SetFalse)]
pub jaeger_enable: bool,
#[clap(long, env, default_value = JAEGER_HOST)]
pub jaeger_host: String,
#[clap(long, env, default_value_t = JAEGER_PORT)]
pub jaeger_port: u16,
#[clap(long, env, default_value = PROMETHEUS_HOST)]
pub prometheus_host: String,
#[clap(long, env, default_value_t = PROMETHEUS_PORT)]
pub prometheus_port: u16,
}

#[derive(Debug, Deserialize)]
pub struct Settings {
pub log_level: String,
Expand All @@ -142,6 +179,39 @@ impl Default for Settings {
}
}

impl From<CliSettings> for Settings {
fn from(value: CliSettings) -> Self {
Self {
log_level: value.log_level,
network: value.network,
database: DatabaseConfig {
host: value.database_host,
user: value.database_user,
password: value.database_password,
dbname: value.database_dbname,
connection_timeout: value.database_connection_timeout,
},
server: ServerConfig {
serve_at: value.server_serve_at,
port: value.server_port,
},
indexer: IndexerConfig {
tendermint_addr: value.indexer_tendermint_addr,
port: value.indexer_port,
},
jaeger: JaegerConfig {
enable: value.jaeger_enable,
host: value.jaeger_host,
port: value.jaeger_port,
},
prometheus: PrometheusConfig {
host: value.prometheus_host,
port: value.prometheus_port,
},
}
}
}

impl Settings {
#[instrument(level = "debug")]
pub fn new() -> Result<Self, Error> {
Expand All @@ -164,7 +234,10 @@ impl Settings {
return Ok(settings);
}

Ok(Self::default())
let cli_settings = CliSettings::parse();
let settings = Settings::from(cli_settings);

Ok(settings)
}

pub fn server_config(&self) -> &ServerConfig {
Expand Down

0 comments on commit 5ef7553

Please sign in to comment.