From 5ecba798c5e4cb74cda0cb5c1bc0fc34b80c4d7c Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Tue, 6 Jun 2023 13:19:09 +0200 Subject: [PATCH] WIP add config --- Cargo.lock | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/conf.rs | 34 ++++++++++++++++++++++++++++++++++ src/data.rs | 3 +++ src/main.rs | 8 ++++++++ 5 files changed, 99 insertions(+) create mode 100644 src/conf.rs diff --git a/Cargo.lock b/Cargo.lock index 52b2ad5..bdbfe2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -349,6 +349,18 @@ dependencies = [ "unreachable", ] +[[package]] +name = "confy" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37668cb35145dcfaa1931a5f37fde375eeae8068b4c0d2f289da28a270b2d2c" +dependencies = [ + "directories", + "serde", + "thiserror", + "toml", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -566,6 +578,26 @@ dependencies = [ "subtle", ] +[[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "downcast-rs" version = "1.2.0" @@ -851,6 +883,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "confy", "env_logger", "futures", "graphql_client", @@ -2070,6 +2103,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom 0.2.8", + "redox_syscall", + "thiserror", +] + [[package]] name = "ref-cast" version = "1.0.15" @@ -3194,6 +3238,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "toml_datetime" version = "0.6.1" diff --git a/Cargo.toml b/Cargo.toml index 06ab6f5..f14c55c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ serde_json = "1.0.94" sp-core = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.32" } subxt = { git = "https://github.com/duniter/subxt.git", branch = "duniter-substrate-v0.9.32" } tokio = { version = "1.26.0", features = ["macros"] } +confy = "0.5.1" # allows to build gcli with different predefined networks [features] diff --git a/src/conf.rs b/src/conf.rs new file mode 100644 index 0000000..e3e5e5e --- /dev/null +++ b/src/conf.rs @@ -0,0 +1,34 @@ +use crate::*; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Config { + version: u8, +} + +impl std::default::Default for Config { + fn default() -> Self { + Self { + version: 0, + } + } +} + + +#[derive(Clone, Default, Debug, clap::Parser)] +pub enum Subcommand { + #[default] + /// Show config + Show, +} + +pub fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> { + // match subcommand + match command { + Subcommand::Show => { + println!("{:?}", data.cfg); + } + }; + + Ok(()) +} diff --git a/src/data.rs b/src/data.rs index 64c63be..b74bcb1 100644 --- a/src/data.rs +++ b/src/data.rs @@ -9,6 +9,8 @@ use indexer::Indexer; pub struct Data { // command line arguments pub args: Args, + // config + pub cfg: conf::Config, // rpc to substrate client pub client: Option<Client>, // graphql to duniter-indexer @@ -43,6 +45,7 @@ impl Data { pub fn new(args: Args) -> Self { Self { args, + cfg: confy::load("gcli", None).expect("must be able to build config"), token_decimals: 0, token_symbol: "tokens".into(), ..Default::default() diff --git a/src/main.rs b/src/main.rs index ca457a1..58dbc46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ mod cache; mod commands; +mod conf; mod data; mod indexer; mod keys; use clap::Parser; use codec::Encode; +use confy; use data::*; use keys::*; use serde::Deserialize; @@ -257,12 +259,17 @@ pub enum Subcommand { /// Indexer subcommands #[clap(subcommand)] Indexer(indexer::Subcommand), + /// Config subcommands + #[clap(subcommand)] + Config(conf::Subcommand), } #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), GcliError> { + // init logger env_logger::init(); + // parse argument and initialize data let args = Args::parse(); let mut data = Data::new(args.clone()); @@ -608,6 +615,7 @@ async fn main() -> Result<(), GcliError> { } } Subcommand::Indexer(subcommand) => indexer::handle_command(data, subcommand).await?, + Subcommand::Config(subcommand) => conf::handle_command(data, subcommand)?, } Ok(()) -- GitLab