From ba2e31655365a681eb6ff1a3724a2e29aec6d2e4 Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Tue, 6 Jun 2023 14:43:14 +0200 Subject: [PATCH] WIP improve config loading --- src/conf.rs | 30 +++++++++++++++++++++++++++--- src/data.rs | 2 +- src/main.rs | 8 ++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index e3e5e5e..d292c8a 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,31 +1,55 @@ use crate::*; use serde::{Deserialize, Serialize}; +const APP_NAME: &str = "gcli"; + #[derive(Serialize, Deserialize, Debug)] pub struct Config { - version: u8, + duniter_endpoint: String, + indexer_endpoint: String, } impl std::default::Default for Config { fn default() -> Self { Self { - version: 0, + duniter_endpoint: String::from("ws://localhost:9944"), + indexer_endpoint: String::from("http://localhost:8080/v1/graphql"), } } } +/// load config file and manage error if could not +pub fn load_conf() -> Config { + match confy::load(APP_NAME, None) { + Ok(cfg) => cfg, + Err(e) => { + log::warn!("met error while loading config file"); + log::error!("{}", e); + log::info!("removing the old conf file and creating a new one"); + let cfg = Config::default(); + confy::store(APP_NAME, None, &cfg).expect("unable to write default config"); + cfg + } + } +} #[derive(Clone, Default, Debug, clap::Parser)] pub enum Subcommand { #[default] + /// Show config path + Where, /// Show config Show, } +/// handle conf command pub fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> { // match subcommand match command { - Subcommand::Show => { + Subcommand::Where => { + println!("{}", confy::get_configuration_file_path(APP_NAME, None)?.display()); + } + Subcommand::Show => { println!("{:?}", data.cfg); } }; diff --git a/src/data.rs b/src/data.rs index b74bcb1..d3b1afe 100644 --- a/src/data.rs +++ b/src/data.rs @@ -45,7 +45,7 @@ impl Data { pub fn new(args: Args) -> Self { Self { args, - cfg: confy::load("gcli", None).expect("must be able to build config"), + cfg: conf::load_conf(), token_decimals: 0, token_symbol: "tokens".into(), ..Default::default() diff --git a/src/main.rs b/src/main.rs index 58dbc46..196cde2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,8 +75,8 @@ pub struct Args { /// Subcommands #[clap(subcommand)] pub subcommand: Subcommand, - /// Indexer URL - #[clap(short, long, default_value = "http://localhost:8080/v1/graphql")] + /// Overwrite indexer endpoint + #[clap(short, long, default_value = "")] indexer: String, /// Do not use indexer #[clap(long)] @@ -91,8 +91,8 @@ pub struct Args { /// Address #[clap(short, long)] address: Option<String>, - /// Websocket RPC endpoint - #[clap(short, long, default_value = "ws://localhost:9944")] + /// Overwrite duniter websocket RPC endpoint + #[clap(short, long, default_value = "")] url: String, } -- GitLab