Add option for generating default config
This commit is contained in:
parent
ef1c124395
commit
891f61f046
37
src/main.rs
37
src/main.rs
@ -6,7 +6,10 @@ use std::{
|
|||||||
boxed::Box,
|
boxed::Box,
|
||||||
error::Error,
|
error::Error,
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::{
|
||||||
|
Deserialize,
|
||||||
|
Serialize,
|
||||||
|
};
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
sqlite::{
|
sqlite::{
|
||||||
@ -34,8 +37,13 @@ struct Options {
|
|||||||
/// Path to database
|
/// Path to database
|
||||||
#[structopt(short, long, value_name = "path", default_value = DEFAULT_DATABASE_PATH)]
|
#[structopt(short, long, value_name = "path", default_value = DEFAULT_DATABASE_PATH)]
|
||||||
database_path: PathBuf,
|
database_path: PathBuf,
|
||||||
|
|
||||||
|
/// Prints default config
|
||||||
|
#[structopt(long)]
|
||||||
|
generate_config: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[serde(default, rename_all = "kebab-case")]
|
#[serde(default, rename_all = "kebab-case")]
|
||||||
struct Config {
|
struct Config {
|
||||||
database_path: PathBuf,
|
database_path: PathBuf,
|
||||||
@ -53,7 +61,12 @@ impl Default for Config {
|
|||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let clap_matches = Options::clap().get_matches();
|
let clap_matches = Options::clap().get_matches();
|
||||||
let options = Options::from_clap(&clap_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)?;
|
let mut config = get_config(&options)?;
|
||||||
|
|
||||||
if let Some(db_path) = clap_matches.value_of("database_path") {
|
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 {
|
if options.no_config {
|
||||||
Ok(Config::default())
|
Ok(Config::default())
|
||||||
} else {
|
} else {
|
||||||
let mut config_file = File::open(&options.config)?;
|
let mut config_file = File::open(&options.config)?;
|
||||||
let mut config_string = String::new();
|
let mut config_string = String::new();
|
||||||
config_file.read_to_string(&mut config_string)?;
|
config_file.read_to_string(&mut config_string)?;
|
||||||
|
|
||||||
toml::from_str(&config_string)
|
toml::from_str(&config_string)
|
||||||
.map_err(|err| std::io::Error::new(
|
.map_err(|err| std::io::Error::new(
|
||||||
std::io::ErrorKind::InvalidData,
|
std::io::ErrorKind::InvalidData,
|
||||||
err
|
err
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user