Skip to content
Snippets Groups Projects
Commit ba2e3165 authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Hugo Trentesaux
Browse files

WIP improve config loading

parent 5ecba798
Branches
Tags
1 merge request!7Big refacto
use crate::*; use crate::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
const APP_NAME: &str = "gcli";
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
version: u8, duniter_endpoint: String,
indexer_endpoint: String,
} }
impl std::default::Default for Config { impl std::default::Default for Config {
fn default() -> Self { fn default() -> Self {
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)] #[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand { pub enum Subcommand {
#[default] #[default]
/// Show config path
Where,
/// Show config /// Show config
Show, Show,
} }
/// handle conf command
pub fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> { pub fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> {
// match subcommand // match subcommand
match command { match command {
Subcommand::Where => {
println!("{}", confy::get_configuration_file_path(APP_NAME, None)?.display());
}
Subcommand::Show => { Subcommand::Show => {
println!("{:?}", data.cfg); println!("{:?}", data.cfg);
} }
......
...@@ -45,7 +45,7 @@ impl Data { ...@@ -45,7 +45,7 @@ impl Data {
pub fn new(args: Args) -> Self { pub fn new(args: Args) -> Self {
Self { Self {
args, args,
cfg: confy::load("gcli", None).expect("must be able to build config"), cfg: conf::load_conf(),
token_decimals: 0, token_decimals: 0,
token_symbol: "tokens".into(), token_symbol: "tokens".into(),
..Default::default() ..Default::default()
......
...@@ -75,8 +75,8 @@ pub struct Args { ...@@ -75,8 +75,8 @@ pub struct Args {
/// Subcommands /// Subcommands
#[clap(subcommand)] #[clap(subcommand)]
pub subcommand: Subcommand, pub subcommand: Subcommand,
/// Indexer URL /// Overwrite indexer endpoint
#[clap(short, long, default_value = "http://localhost:8080/v1/graphql")] #[clap(short, long, default_value = "")]
indexer: String, indexer: String,
/// Do not use indexer /// Do not use indexer
#[clap(long)] #[clap(long)]
...@@ -91,8 +91,8 @@ pub struct Args { ...@@ -91,8 +91,8 @@ pub struct Args {
/// Address /// Address
#[clap(short, long)] #[clap(short, long)]
address: Option<String>, address: Option<String>,
/// Websocket RPC endpoint /// Overwrite duniter websocket RPC endpoint
#[clap(short, long, default_value = "ws://localhost:9944")] #[clap(short, long, default_value = "")]
url: String, url: String,
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment