Просмотр исходного кода

add option to change the path of config file for darkfid and drk cli & remove unused code
in src/cli

ghassmo 5 лет назад
Родитель
Сommit
b22d8ba0f6
5 измененных файлов с 63 добавлено и 195 удалено
  1. 16 4
      src/bin/darkfid.rs
  2. 13 5
      src/bin/drk.rs
  3. 18 93
      src/cli/darkfid_cli.rs
  4. 16 51
      src/cli/drk_cli.rs
  5. 0 42
      src/cli/gatewayd_cli.rs

+ 16 - 4
src/bin/darkfid.rs

@@ -277,16 +277,28 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&DarkfidConfig>) -> Resu
 }
 
 fn main() -> Result<()> {
+
     let options = Arc::new(DarkfidCli::load()?);
 
-    let path = join_config_path(&PathBuf::from("darkfid.toml")).unwrap();
+    let config_path: PathBuf;
 
-    let config: DarkfidConfig = if Path::new(&path).exists() {
-        Config::<DarkfidConfig>::load(path)?
+    match options.config.as_ref() {
+        Some(path) => {
+            config_path = path.to_owned();
+        }
+        None => {
+            config_path = join_config_path(&PathBuf::from("darkfid.toml"))?;
+        }
+    }
+
+
+    let config: DarkfidConfig = if Path::new(&config_path).exists() {
+        Config::<DarkfidConfig>::load(config_path)?
     } else {
-        Config::<DarkfidConfig>::load_default(path)?
+        Config::<DarkfidConfig>::load_default(config_path)?
     };
 
+
     let config_ptr = Arc::new(&config);
 
     let ex = Arc::new(Executor::new());

+ 13 - 5
src/bin/drk.rs

@@ -91,7 +91,6 @@ impl Drk {
         let r = jsonrpc::request(json!("withdraw"), json!([address, amount]));
         Ok(self.request(r).await?)
     }
-
 }
 
 async fn start(config: &DrkConfig, options: DrkCli) -> Result<()> {
@@ -144,12 +143,21 @@ async fn start(config: &DrkConfig, options: DrkCli) -> Result<()> {
 fn main() -> Result<()> {
     let options = DrkCli::load()?;
 
-    let path = join_config_path(&PathBuf::from("drk.toml")).unwrap();
+    let config_path: PathBuf;
+
+    match options.config.as_ref() {
+        Some(path) => {
+            config_path = path.to_owned();
+        }
+        None => {
+            config_path = join_config_path(&PathBuf::from("drk.toml"))?;
+        }
+    }
 
-    let config: DrkConfig = if Path::new(&path).exists() {
-        Config::<DrkConfig>::load(path)?
+    let config: DrkConfig = if Path::new(&config_path).exists() {
+        Config::<DrkConfig>::load(config_path)?
     } else {
-        Config::<DrkConfig>::load_default(path)?
+        Config::<DrkConfig>::load_default(config_path)?
     };
 
     {

+ 18 - 93
src/cli/darkfid_cli.rs

@@ -1,11 +1,12 @@
-//use super::cli_config::DarkfidCliConfig;
 use crate::Result;
 
+use std::path::PathBuf;
+
 use clap::{App, Arg};
 
 pub struct DarkfidCli {
-    //pub change_config: bool,
     pub verbose: bool,
+    pub config: Box<Option<PathBuf>>,
 }
 
 impl DarkfidCli {
@@ -21,101 +22,25 @@ impl DarkfidCli {
                     .long("verbose")
                     .takes_value(false),
             )
-            //.subcommand(
-            //    App::new("config")
-            //        .about("Configuration settings")
-            //        .aliases(&["get", "set"])
-            //        .setting(AppSettings::SubcommandRequiredElseHelp)
-            //        .subcommand(App::new("get").about("Get configuration settings"))
-            //        .subcommand(
-            //            App::new("set")
-            //                .about("Set configuration settings")
-            //                .args(&[
-            //                    Arg::new("connect_url")
-            //                        .about("Set Connect Url")
-            //                        .long("connect-url")
-            //                        .takes_value(true),
-            //                    Arg::new("subscriber_url")
-            //                        .about("Set Subscriber Url")
-            //                        .long("subscriber-url")
-            //                        .takes_value(true),
-            //                    Arg::new("rpc_url")
-            //                        .about("Set RPC Url")
-            //                        .long("rpc-url")
-            //                        .takes_value(true),
-            //                    Arg::new("database_path")
-            //                        .about("Set Database Path")
-            //                        .long("database-path")
-            //                        .takes_value(true),
-            //                    Arg::new("log_path")
-            //                        .about("Set Log Path")
-            //                        .long("log-path")
-            //                        .takes_value(true),
-            //                    Arg::new("password")
-            //                        .about("Set password")
-            //                        .long("password")
-            //                        .takes_value(true),
-            //                ])
-            //                .setting(AppSettings::ArgRequiredElseHelp),
-            //        ),
-            //)
+            .arg(
+                Arg::new("config")
+                    .short('c')
+                    .help_heading(Some("Path for config file"))
+                    .long("config")
+                    .takes_value(true),
+            )
             .get_matches();
 
-        //let mut change_config = false;
+        let config = Box::new(
+            if let Some(config_path) = app.value_of("config") {
+                Some(std::path::Path::new(config_path).to_path_buf())
+            } else {
+                None
+            }
+        );
 
         let verbose = app.is_present("verbose");
 
-        //match app.subcommand_matches("config") {
-        //    Some(config_sub) => match config_sub.subcommand() {
-        //        Some(c) => match c {
-        //            ("get", _) => {
-        //                change_config = true;
-        //                println!("Connect Url: {}", config.connect_url);
-        //                println!("Subscriber Url: {}", config.subscriber_url);
-        //                println!("RPC Url: {}", config.rpc_url);
-        //                println!("Database path: {}", config.database_path);
-        //                println!("Log Path: {}", config.log_path);
-        //                if config.password.trim().is_empty() {
-        //                    println!("Password is empty. Please set a password.")
-        //                };
-        //            }
-        //            ("set", c) => {
-        //                change_config = true;
-        //                if let Some(v) = c.value_of("connect_url") {
-        //                    config.connect_url = v.to_string();
-        //                    println!("Change Connect Url To {}", config.connect_url);
-        //                }
-        //                if let Some(v) = c.value_of("subscriber_url") {
-        //                    config.subscriber_url = v.to_string();
-        //                    println!("Change Subscriber Url To {}", config.subscriber_url);
-        //                }
-        //                if let Some(v) = c.value_of("rpc_url") {
-        //                    config.rpc_url = v.to_string();
-        //                    println!("Change RPC Url To {}", config.rpc_url);
-        //                }
-        //                if let Some(v) = c.value_of("database_path") {
-        //                    config.database_path = v.to_string();
-        //                    println!("Change Database Path To {}", config.database_path);
-        //                }
-        //                if let Some(v) = c.value_of("log_path") {
-        //                    config.log_path = v.to_string();
-        //                    println!("Change Log Path To {}", config.log_path);
-        //                }
-        //                if let Some(v) = c.value_of("password") {
-        //                    config.password = v.to_string();
-        //                    println!("Updated password. New password: {}", config.password);
-        //                }
-        //            }
-        //            _ => {}
-        //        },
-        //        None => {}
-        //    },
-        //    None => {}
-        //}
-
-        Ok(Self {
-            //change_config,
-            verbose,
-        })
+        Ok(Self { verbose , config})
     }
 }

+ 16 - 51
src/cli/drk_cli.rs

@@ -4,6 +4,8 @@ use crate::Result;
 use clap::{App, Arg};
 use serde::Deserialize;
 
+use std::path::PathBuf;
+
 fn is_u64<'a>(v: &'a str) -> std::result::Result<(), String> {
     if v.parse::<u64>().is_ok() {
         Ok(())
@@ -55,7 +57,6 @@ impl WithdrawParams {
 }
 
 pub struct DrkCli {
-    //pub change_config: bool,
     pub verbose: bool,
     pub cashier: bool,
     pub wallet: bool,
@@ -67,6 +68,7 @@ pub struct DrkCli {
     pub transfer: Option<TransferParams>,
     pub deposit: Option<Deposit>,
     pub withdraw: Option<WithdrawParams>,
+    pub config: Box<Option<PathBuf>>,
 }
 
 impl DrkCli {
@@ -129,6 +131,12 @@ impl DrkCli {
                     .help_heading(Some("Send a stop signal to the daemon"))
                     .takes_value(false),
             )
+            .arg(
+                Arg::new("config")
+                    .help_heading(Some("Path for config file"))
+                    .long("config")
+                    .takes_value(true),
+            )
             .subcommand(
                 App::new("transfer")
                     .about("Transfer DBTC between users")
@@ -173,32 +181,8 @@ impl DrkCli {
                     ),
             )
             .subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
-            //.subcommand(
-            //    App::new("config")
-            //        .about("Configuration settings")
-            //        .aliases(&["get", "set"])
-            //        .setting(AppSettings::SubcommandRequiredElseHelp)
-            //        .subcommand(App::new("get").about("Get configuration settings"))
-            //        .subcommand(
-            //            App::new("set")
-            //                .about("Set configuration settings")
-            //                .args(&[
-            //                    Arg::new("rpc_url")
-            //                        .about("Set RPC Url")
-            //                        .long("rpc-url")
-            //                        .takes_value(true),
-            //                    Arg::new("log_path")
-            //                        .about("Set Log Path")
-            //                        .long("log-path")
-            //                        .takes_value(true),
-            //                ])
-            //                .setting(AppSettings::ArgRequiredElseHelp),
-            //        ),
-            //)
             .get_matches();
 
-        //let mut change_config = false;
-
         let verbose = app.is_present("verbose");
         let cashier = app.is_present("cashier");
         let wallet = app.is_present("wallet");
@@ -245,34 +229,14 @@ impl DrkCli {
             }
             None => {}
         }
-        //match app.subcommand_matches("config") {
-        //    Some(config_sub) => match config_sub.subcommand() {
-        //        Some(c) => match c {
-        //            ("get", _) => {
-        //                change_config = true;
-        //                println!("RPC Url: {}", config.rpc_url);
-        //                println!("Log Path: {}", config.log_path);
-        //            }
-        //            ("set", c) => {
-        //                change_config = true;
-        //                if let Some(v) = c.value_of("rpc_url") {
-        //                    config.rpc_url = v.to_string();
-        //                    println!("Change RPC Url To {}", config.rpc_url);
-        //                }
-        //                if let Some(v) = c.value_of("log_path") {
-        //                    config.log_path = v.to_string();
-        //                    println!("Change Log Path To {}", config.log_path);
-        //                }
-        //            }
-        //            _ => {}
-        //        },
-        //        None => {}
-        //    },
-        //    None => {}
-        //}
+
+        let config = Box::new(if let Some(config_path) = app.value_of("config") {
+            Some(std::path::Path::new(config_path).to_path_buf())
+        } else {
+            None
+        });
 
         Ok(Self {
-            //change_config,
             verbose,
             cashier,
             wallet,
@@ -284,6 +248,7 @@ impl DrkCli {
             deposit,
             transfer,
             withdraw,
+            config,
         })
     }
 }

+ 0 - 42
src/cli/gatewayd_cli.rs

@@ -1,11 +1,7 @@
 use crate::Result;
 
 pub struct GatewaydCli {
-    //pub accept_addr: Option<SocketAddr>,
-    //pub pub_addr: Option<SocketAddr>,
     pub verbose: bool,
-    //pub database_path: Box<std::path::PathBuf>,
-    //pub log_path: Box<std::path::PathBuf>,
 }
 
 impl GatewaydCli {
@@ -14,52 +10,14 @@ impl GatewaydCli {
             (version: "0.1.0")
             (author: "Amir Taaki <amir@dyne.org>")
             (about: "run service daemon")
-            //(@arg ACCEPT: -a --accept +takes_value "Accept add//ress")
-            //(@arg PUB_ADDR: -p --pubaddr +takes_value "Publisher addr")
             (@arg VERBOSE: -v --verbose "Increase verbosity")
-            //(@arg DATABASE_PATH: --database +takes_value "database path")
-            //(@arg LOG_PATH: --log +takes_value "Logfile path")
         )
         .get_matches();
 
-        //let accept_addr = if let Some(accept_addr) = app.value_of("ACCEPT") {
-        //    Some(accept_addr.parse()?)
-        //} else {
-        //    None
-        //};
-
-        //let pub_addr = if let Some(pub_addr) = app.value_of("PUB_ADDR") {
-        //    Some(pub_addr.parse()?)
-        //} else {
-        //    None
-        //};
-
         let verbose = app.is_present("VERBOSE");
 
-        //let database_path = Box::new(
-        //    if let Some(database_path) = app.value_of("DATABASE_PATH") {
-        //        std::path::Path::new(database_path)
-        //    } else {
-        //        std::path::Path::new("database.db")
-        //    }
-        //    .to_path_buf(),
-        //);
-
-        //let log_path = Box::new(
-        //    if let Some(log_path) = app.value_of("LOG_PATH") {
-        //        std::path::Path::new(log_path)
-        //    } else {
-        //        std::path::Path::new("/tmp/darkfid_service_daemon.log")
-        //    }
-        //    .to_path_buf(),
-        //);
-
         Ok(Self {
-            //accept_addr,
-            //pub_addr,
             verbose,
-            //database_path,
-            //log_path,
         })
     }
 }