From e62f3ee2dc0093da0f02b70e8f94fe2a40b98cce Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Tue, 15 Jun 2021 14:45:38 +0000 Subject: [PATCH] Add config reading --- src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 08e7a9a..2e0f4cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,11 @@ use structopt::StructOpt; -use std::path::PathBuf; +use std::{ + path::PathBuf, + fs::File, + io::prelude::*, + boxed::Box, + error::Error, +}; use serde::Deserialize; const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite"; @@ -17,7 +23,6 @@ struct Options { #[serde(default)] struct Config { database_path: PathBuf, - } impl Default for Config { @@ -28,8 +33,31 @@ impl Default for Config { } } -fn main() { - let options = Options::from_args(); +fn main() -> Result<(), Box> { + let clap_matches = Options::clap().get_matches(); + let options = Options::from_clap(&clap_matches); + + let mut config = get_config(&options)?; + + clap_matches.value_of("database_path") + .map(|x| config.database_path = x.into()); + #[cfg(debug_assertions)] println!("{:?}", options); + + Ok(()) } + +/// +fn get_config(options: &Options) -> std::io::Result { + 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