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:
parent
c41b0478d6
commit
f5d8341234
19
src/main.rs
19
src/main.rs
|
@ -6,7 +6,7 @@
|
||||||
//!
|
//!
|
||||||
//! Go to [config] module
|
//! Go to [config] module
|
||||||
|
|
||||||
use std::{fs::File, path::Path};
|
use std::{fs::File, io::Write, path::Path};
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
@ -23,11 +23,28 @@ mod server;
|
||||||
pub const DATABASE_FILE: &str = "homedisk.db";
|
pub const DATABASE_FILE: &str = "homedisk.db";
|
||||||
/// Default configuration file.
|
/// Default configuration file.
|
||||||
pub const CONFIG_FILE: &str = "config.toml";
|
pub const CONFIG_FILE: &str = "config.toml";
|
||||||
|
/// Default configuration file content.
|
||||||
|
pub static DEFAULT_CONFIG_CONTENT: &'static [u8] = include_bytes!("../config.toml");
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
logger::init();
|
logger::init();
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
|
||||||
|
file.write_all(DEFAULT_CONFIG_CONTENT)
|
||||||
|
.expect("notrace - Failed to write default configuration to config file");
|
||||||
|
|
||||||
|
info!("Created default configuration file.");
|
||||||
|
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
let config = Config::parse(CONFIG_FILE).expect("notrace - Failed to parse configuration file");
|
let config = Config::parse(CONFIG_FILE).expect("notrace - Failed to parse configuration file");
|
||||||
|
|
||||||
// open database connection
|
// open database connection
|
||||||
|
|
Loading…
Reference in New Issue