From 891f61f046376476ad2c5f0b540d6bf4e2341989 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Thu, 8 Jul 2021 12:55:48 +0200 Subject: [PATCH] Add option for generating default config --- src/main.rs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 472f6c0..1521117 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,10 @@ use std::{ boxed::Box, error::Error, }; -use serde::Deserialize; +use serde::{ + Deserialize, + Serialize, +}; use sqlx::{ prelude::*, sqlite::{ @@ -34,8 +37,13 @@ struct Options { /// Path to database #[structopt(short, long, value_name = "path", default_value = DEFAULT_DATABASE_PATH)] database_path: PathBuf, + + /// Prints default config + #[structopt(long)] + generate_config: bool, } +#[derive(Debug, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case")] struct Config { database_path: PathBuf, @@ -53,7 +61,12 @@ impl Default for Config { async fn main() -> Result<(), Box> { let clap_matches = Options::clap().get_matches(); let options = Options::from_clap(&clap_matches); - + + if options.generate_config { + println!("{}", toml::to_string_pretty(&Config::default())?); + return Ok(()); + } + let mut config = get_config(&options)?; if let Some(db_path) = clap_matches.value_of("database_path") { @@ -99,15 +112,15 @@ fn get_config(options: &Options) -> std::io::Result { if options.no_config { Ok(Config::default()) } else { - let mut config_file = File::open(&options.config)?; - let mut config_string = String::new(); - config_file.read_to_string(&mut config_string)?; - - toml::from_str(&config_string) - .map_err(|err| std::io::Error::new( - std::io::ErrorKind::InvalidData, - err + let mut config_file = File::open(&options.config)?; + let mut config_string = String::new(); + config_file.read_to_string(&mut config_string)?; + + toml::from_str(&config_string) + .map_err(|err| std::io::Error::new( + std::io::ErrorKind::InvalidData, + err + ) ) - ) -} + } } \ No newline at end of file