From 872b0c4ab0643796530734000ced05c711acc771 Mon Sep 17 00:00:00 2001 From: adz Date: Wed, 23 Aug 2023 10:10:32 +0200 Subject: [PATCH] Add some todos --- Cargo.lock | 1 + aquadoggo_cli/Cargo.toml | 2 +- aquadoggo_cli/src/main.rs | 74 +++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e071f9464..6f32aabe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -926,6 +926,7 @@ dependencies = [ "anstream", "anstyle", "clap_lex", + "once_cell", "strsim", ] diff --git a/aquadoggo_cli/Cargo.toml b/aquadoggo_cli/Cargo.toml index 425074731..b616a8116 100644 --- a/aquadoggo_cli/Cargo.toml +++ b/aquadoggo_cli/Cargo.toml @@ -21,7 +21,7 @@ doc = false [dependencies] anyhow = "1.0.62" -clap = { version = "4.1.8", features = ["derive"] } +clap = { version = "4.1.8", features = ["derive", "cargo"] } directories = "5.0.1" env_logger = "0.9.0" figment = { version = "0.10.10", features = ["toml", "env"] } diff --git a/aquadoggo_cli/src/main.rs b/aquadoggo_cli/src/main.rs index 4de1d295e..c6f0b266d 100644 --- a/aquadoggo_cli/src/main.rs +++ b/aquadoggo_cli/src/main.rs @@ -2,18 +2,20 @@ mod key_pair; +use std::fmt::Display; use std::net::{IpAddr, SocketAddr}; use std::path::PathBuf; use anyhow::Result; use aquadoggo::{AllowList, Configuration as NodeConfiguration, NetworkConfiguration, Node}; -use clap::Parser; +use clap::{crate_version, Parser}; use directories::ProjectDirs; use figment::providers::{Env, Format, Serialized, Toml}; use figment::Figment; use libp2p::multiaddr::Protocol; use libp2p::Multiaddr; use p2panda_rs::schema::SchemaId; +use p2panda_rs::Human; use serde::{Deserialize, Serialize}; const CONFIG_FILE_NAME: &str = "config.toml"; @@ -73,6 +75,36 @@ struct Configuration { im_a_relay: bool, } +impl Display for Configuration { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + self.config + .as_ref() + .map_or("No config file provided".into(), |ref path| format!( + "Loading config file from {}", + path.display() + )) + )?; + + write!(f, "\n\n")?; + + // @TODO: Nicer printing of all values + write!(f, "Schemas\n")?; + write!( + f, + "{:<20} {:<20}\n", + "supported_schema_ids", + self.supported_schema_ids + .iter() + .map(|id| id.display()) + .collect::>() + .join(", ") + ) + } +} + impl From for NodeConfiguration { fn from(cli: Configuration) -> Self { let supported_schema_ids = if cli.supported_schema_ids.is_empty() { @@ -129,10 +161,10 @@ fn try_determine_config_file_path() -> Option { fn load_config() -> Result { // Parse command line arguments first - let cli = Configuration::parse(); + let mut cli = Configuration::parse(); // Determine if a config file path was provided or if we should look for it in common locations - let config_file_path = if cli.config.is_some() { + cli.config = if cli.config.is_some() { cli.config.clone() } else { try_determine_config_file_path() @@ -142,54 +174,26 @@ fn load_config() -> Result { // arguments let mut figment = Figment::new(); - if let Some(path) = config_file_path { + if let Some(path) = &cli.config { figment = figment.merge(Toml::file(path)); } + // @TODO: Fix not overriding values when empty array was set figment .merge(Env::prefixed(CONFIG_ENV_VAR_PREFIX)) .merge(Serialized::defaults(cli)) .extract() } -fn panda_da() -> String { - r#" - ██████ ███████ ████ - ████████ ██████ - ██████ ███ - █████ ██ - █ ████ █████ - █ ██████ █ █████ - ██ ████ ███ █████ - █████ ██████ █ - ███████ ██ - █████████ █████████████ - ███████████ █████████ - █████████████████ ████ - ██████ ███████████ ██ - ██████████ █████ █ - █████████ ██ ███ ██ - ██████ █ █ ██ - ██ ██ ███████ ██ - ███████████ ██████ - ████████ ████████████ ██████ - ████ ██████ ██████████ █ ████ - █████████ ████████ ███ ███████ - ████████ ██████ ████████ - █████████ ████████████████████████ ███ - █████████ ██"# - .into() -} - #[tokio::main] async fn main() { env_logger::init(); match load_config() { Ok(config) => { - // @TODO: Nicer print - println!("{}\n\n{:#?}", panda_da(), config); + println!("aquadoggo v{}\n\n{:?}", crate_version!(), config); + // @TODO: Create folders when paths for db or key was set let key_pair = match &config.private_key { Some(path) => key_pair::generate_or_load_key_pair(path.clone()) .expect("Could not load private key from file"),