Skip to content

Commit

Permalink
Add some todos
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Aug 23, 2023
1 parent 19e6d85 commit 872b0c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion aquadoggo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
74 changes: 39 additions & 35 deletions aquadoggo_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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::<Vec<String>>()
.join(", ")
)
}
}

impl From<Configuration> for NodeConfiguration {
fn from(cli: Configuration) -> Self {
let supported_schema_ids = if cli.supported_schema_ids.is_empty() {
Expand Down Expand Up @@ -129,10 +161,10 @@ fn try_determine_config_file_path() -> Option<PathBuf> {

fn load_config() -> Result<Configuration, figment::Error> {
// 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()
Expand All @@ -142,54 +174,26 @@ fn load_config() -> Result<Configuration, figment::Error> {
// 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"),
Expand Down

0 comments on commit 872b0c4

Please sign in to comment.