diff --git a/tables.sql b/sql/tables.sql similarity index 100% rename from tables.sql rename to sql/tables.sql diff --git a/src/database/sqlite.rs b/src/database/sqlite.rs index 5150c3a..3655881 100644 --- a/src/database/sqlite.rs +++ b/src/database/sqlite.rs @@ -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 { @@ -44,7 +46,7 @@ impl Database { /// Create all required tables for HomeDisk. pub async fn create_tables(&self) -> Result { self.pool - .execute(include_str!("../../tables.sql")) + .execute(DATABASE_TABLES) .await .map_err(|e| Error::CreateTables(e.to_string())) } diff --git a/src/main.rs b/src/main.rs index aea067f..002cb51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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") };