Add option for generating default config

This commit is contained in:
Elias Schriefer 2021-07-08 12:55:48 +02:00
parent ef1c124395
commit 891f61f046

View File

@ -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<dyn Error>> {
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<Config> {
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
)
)
)
}
}
}