feat: create storage directory if doesn't exists

This commit is contained in:
MedzikUser 2022-09-21 20:53:22 +02:00
parent e50ee66b0e
commit adcbada699
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
3 changed files with 18 additions and 6 deletions

View File

@ -19,6 +19,8 @@ pub struct Database {
pub pool: SqlitePool,
}
pub const DATABASE_TABLES: &str = include_str!("../../sql/tables.sql");
impl Database {
/// Open a SQLite database
pub async fn open(path: &str) -> Result<Self> {
@ -44,7 +46,7 @@ impl Database {
/// Create all required tables for HomeDisk.
pub async fn create_tables(&self) -> Result<SqliteQueryResult> {
self.pool
.execute(include_str!("../../tables.sql"))
.execute(DATABASE_TABLES)
.await
.map_err(|e| Error::CreateTables(e.to_string()))
}

View File

@ -6,7 +6,11 @@
//!
//! Go to [config] module
use std::{fs::File, io::Write, path::Path};
use std::{
fs::{self, File},
io::Write,
path::Path,
};
use clap::Parser;
use config::Config;
@ -42,7 +46,7 @@ async fn main() {
let args = Cli::parse();
let config_path = args.config;
let database_path = &args.database;
let database_path = args.database;
// if configuration file doesn't exist, create it
if !Path::new(&config_path).exists() {
@ -61,6 +65,12 @@ async fn main() {
let config = Config::parse(&config_path).expect("notrace - Failed to parse configuration file");
// if storage directory doesn't exist, create it
if !Path::new(&config.storage.path).exists() {
fs::create_dir_all(&config.storage.path)
.expect("notrace - Failed to create storage directory");
}
// open database connection
let db =
// if database file doesn't exists create it
@ -69,10 +79,10 @@ async fn main() {
info!("Creating database file...");
// create an empty database file
File::create(database_path).expect("notrace - Failed to create a database file");
File::create(&database_path).expect("notrace - Failed to create a database file");
// open database file
let db = Database::open(database_path)
let db = Database::open(&database_path)
.await
.expect("notrace - Failed to open database file");
@ -85,7 +95,7 @@ async fn main() {
}
// if database file exists
else {
Database::open(database_path)
Database::open(&database_path)
.await
.expect("notrace - Failed to open database file")
};