feat(config): create default config file if doesn't exists

Create configuration file with default values if file doesn't exists.
This commit is contained in:
MedzikUser 2022-09-21 18:10:12 +02:00
parent c41b0478d6
commit f5d8341234
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
1 changed files with 18 additions and 1 deletions

View File

@ -6,7 +6,7 @@
//! Go to [config] module
use std::{fs::File, path::Path};
use std::{fs::File, io::Write, path::Path};
use config::Config;
use tracing::{info, warn};
@ -23,11 +23,28 @@ mod server;
pub const DATABASE_FILE: &str = "homedisk.db";
/// Default configuration file.
pub const CONFIG_FILE: &str = "config.toml";
/// Default configuration file content.
pub static DEFAULT_CONFIG_CONTENT: &'static [u8] = include_bytes!("../config.toml");
async fn main() {
// if configuration file doesn't exist, create it
if !Path::new(CONFIG_FILE).exists() {
warn!("Configuration file doesn't exists.");
let mut file =
File::create(CONFIG_FILE).expect("notrace - Failed to create configuration file");
.expect("notrace - Failed to write default configuration to config file");
info!("Created default configuration file.");
let config = Config::parse(CONFIG_FILE).expect("notrace - Failed to parse configuration file");
// open database connection