Преглед изворни кода

dnetview: Support custom config file through args, and cleanup args.

Luther Blissett пре 3 година
родитељ
комит
94b0c40e4e
4 измењених фајлова са 34 додато и 50 уклоњено
  1. 6 1
      bin/dnetview/Cargo.toml
  2. 1 0
      bin/dnetview/src/config.rs
  3. 14 10
      bin/dnetview/src/main.rs
  4. 13 39
      bin/dnetview/src/options.rs

+ 6 - 1
bin/dnetview/Cargo.toml

@@ -1,7 +1,12 @@
 [package]
 name = "dnetview"
+description = "P2P network monitoring TUI utility"
 version = "0.3.0"
 edition = "2021"
+authors = ["darkfi <dev@dark.fi>"]
+license = "AGPL-3.0-only"
+homepage = "https://dark.fi"
+repository = "https://github.com/darkrenaissance/darkfi"
 
 [dependencies.darkfi]
 path = "../../"
@@ -19,7 +24,7 @@ easy-parallel = "3.2.0"
 async-channel = "1.7.1"
 
 # Misc
-clap = "3.2.18"
+clap = {version = "3.2.18", features = ["derive"]}
 rand = "0.8.5"
 simplelog = "0.12.0"
 log = "0.4.17"

+ 1 - 0
bin/dnetview/src/config.rs

@@ -1,5 +1,6 @@
 use serde::{Deserialize, Serialize};
 
+pub const CONFIG_FILE: &str = "dnetview_config.toml";
 pub const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../dnetview_config.toml");
 
 #[derive(Clone, Debug, Deserialize, Serialize)]

+ 14 - 10
bin/dnetview/src/main.rs

@@ -1,9 +1,11 @@
 use async_std::sync::Arc;
 use std::{fs::File, io, io::Read, path::PathBuf};
 
+use clap::Parser;
 use darkfi::util::{
     cli::{get_log_config, get_log_level, spawn_config, Config},
-    join_config_path,
+    expand_path,
+    path::get_config_path,
 };
 use easy_parallel::Parallel;
 use log::info;
@@ -24,10 +26,10 @@ pub mod util;
 pub mod view;
 
 use crate::{
-    config::{DnvConfig, CONFIG_FILE_CONTENTS},
+    config::{DnvConfig, CONFIG_FILE, CONFIG_FILE_CONTENTS},
     error::{DnetViewError, DnetViewResult},
     model::Model,
-    options::ProgramOptions,
+    options::Args,
     parser::DataParser,
     view::View,
 };
@@ -107,19 +109,21 @@ impl DnetView {
 #[async_std::main]
 async fn main() -> DnetViewResult<()> {
     //debug!(target: "dnetview", "main() START");
-    let options = ProgramOptions::load()?;
+    let args = Args::parse();
 
-    let verbosity_level = options.app.occurrences_of("verbose");
-
-    let log_level = get_log_level(verbosity_level);
+    let log_level = get_log_level(args.verbose.into());
     let log_config = get_log_config();
 
-    let file = File::create(&*options.log_path).unwrap();
+    let log_file_path = PathBuf::from(expand_path(&args.log_path)?);
+    if let Some(parent) = log_file_path.parent() {
+        std::fs::create_dir_all(parent)?;
+    };
+
+    let file = File::create(log_file_path)?;
     WriteLogger::init(log_level, log_config, file)?;
     info!("Log level: {}", log_level);
 
-    let config_path = join_config_path(&PathBuf::from("dnetview_config.toml"))?;
-
+    let config_path = get_config_path(args.config, CONFIG_FILE)?;
     spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
 
     let config = Config::<DnvConfig>::load(config_path)?;

+ 13 - 39
bin/dnetview/src/options.rs

@@ -1,43 +1,17 @@
-use clap::{Arg, ArgMatches, Command};
+use darkfi::cli_desc;
 
-use darkfi::Result;
+#[derive(clap::Parser)]
+#[clap(name = "dnetview", about = cli_desc!(), version)]
+pub struct Args {
+    #[clap(short, parse(from_occurrences))]
+    /// Increase verbosity (-vvv supported)
+    pub verbose: u8,
 
-pub struct ProgramOptions {
-    pub log_path: Box<std::path::PathBuf>,
-    pub app: ArgMatches,
-}
-
-impl ProgramOptions {
-    pub fn load() -> Result<ProgramOptions> {
-        let app = Command::new("dnetview")
-            .version("0.1.0")
-            .author("lunar_mining")
-            .about("dnetview")
-            .arg(
-                Arg::new("LOG_PATH")
-                    .long("log")
-                    .value_name("LOG_PATH")
-                    .help("Logfile path")
-                    .takes_value(true),
-            )
-            .arg(
-                Arg::new("verbose")
-                    .short('v')
-                    .long("verbose")
-                    .multiple_occurrences(true)
-                    .help("Sets the level of verbosity"),
-            )
-            .get_matches();
-
-        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/dnetview.log")
-            }
-            .to_path_buf(),
-        );
+    /// Logfile path
+    #[clap(default_value = "~/.local/darkfi/dnetview.log")]
+    pub log_path: String,
 
-        Ok(ProgramOptions { log_path, app })
-    }
+    /// Sets a custom config file
+    #[clap(short, long)]
+    pub config: Option<String>,
 }