Move config init completely into config module
This commit is contained in:
parent
cc2a453c97
commit
199e2169dc
@ -2,6 +2,10 @@ use serde::{
|
||||
Deserialize,
|
||||
Serialize,
|
||||
};
|
||||
use structopt::{
|
||||
StructOpt,
|
||||
clap::ArgMatches,
|
||||
};
|
||||
use std::{
|
||||
net::IpAddr,
|
||||
path::PathBuf,
|
||||
@ -21,9 +25,10 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub async fn get(options: &Options) -> std::io::Result<Self> {
|
||||
if options.no_config {
|
||||
Ok(Config::default())
|
||||
pub async fn get_with_options(clap_matches: ArgMatches<'_>) -> std::io::Result<(Self, Options)> {
|
||||
let options = Options::from_clap(&clap_matches);
|
||||
let mut config = if options.no_config {
|
||||
Config::default()
|
||||
} else {
|
||||
let mut config_file = File::open(&options.config).await?;
|
||||
let mut config_string = String::new();
|
||||
@ -34,8 +39,22 @@ impl Config {
|
||||
std::io::ErrorKind::InvalidData,
|
||||
err
|
||||
)
|
||||
)
|
||||
)?
|
||||
};
|
||||
|
||||
if clap_matches.occurrences_of("database-path") > 0 {
|
||||
config.database_path = options.database_path.clone();
|
||||
}
|
||||
|
||||
if clap_matches.occurrences_of("ip-address") > 0 {
|
||||
config.ip_address = options.ip_address;
|
||||
}
|
||||
|
||||
if clap_matches.occurrences_of("port") > 0 {
|
||||
config.port = options.port;
|
||||
}
|
||||
|
||||
Ok((config, options))
|
||||
}
|
||||
}
|
||||
|
||||
|
21
src/main.rs
21
src/main.rs
@ -31,28 +31,15 @@ const DEFAULT_PORT_STR: &'static str = "80";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let clap_matches = Options::clap().get_matches();
|
||||
let options = Options::from_clap(&clap_matches);
|
||||
let (config, options) = Config::get_with_options(
|
||||
Options::clap().get_matches()
|
||||
).await?;
|
||||
|
||||
if options.generate_config {
|
||||
println!("{}", toml::to_string_pretty(&Config::default())?);
|
||||
println!("{}", toml::to_string_pretty(&config)?);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut config = Config::get(&options).await?;
|
||||
|
||||
if clap_matches.occurrences_of("database-path") > 0 {
|
||||
config.database_path = options.database_path.clone();
|
||||
}
|
||||
|
||||
if clap_matches.occurrences_of("ip-address") > 0 {
|
||||
config.ip_address = options.ip_address;
|
||||
}
|
||||
|
||||
if clap_matches.occurrences_of("port") > 0 {
|
||||
config.port = options.port;
|
||||
}
|
||||
|
||||
dbg!(&config);
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
|
Loading…
Reference in New Issue
Block a user