diff --git a/src/conf.rs b/src/conf.rs index e3e5e5ee2b2e63fc692a43d6a12fbd26a2303971..d292c8a72d9d958f3dbe9344d5a45684e4e52656 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 b74bcb1430c620a2008b4c81afa610ccc50aa647..d3b1afe95ec702134484a964ea6abc8c7e65d383 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 58dbc46e4bbc370b49466c1fbf03de139f7119bf..196cde23766e9404676272013679337d99fbc907 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, }