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
No related branches found
No related tags found
1 merge request!7Big refacto
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);
}
};
......
......@@ -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()
......
......@@ -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,
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment