feat: create storage directory if doesn't exists
This commit is contained in:
parent
e50ee66b0e
commit
adcbada699
|
@ -19,6 +19,8 @@ pub struct Database {
|
||||||
pub pool: SqlitePool,
|
pub pool: SqlitePool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const DATABASE_TABLES: &str = include_str!("../../sql/tables.sql");
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
/// Open a SQLite database
|
/// Open a SQLite database
|
||||||
pub async fn open(path: &str) -> Result<Self> {
|
pub async fn open(path: &str) -> Result<Self> {
|
||||||
|
@ -44,7 +46,7 @@ impl Database {
|
||||||
/// Create all required tables for HomeDisk.
|
/// Create all required tables for HomeDisk.
|
||||||
pub async fn create_tables(&self) -> Result<SqliteQueryResult> {
|
pub async fn create_tables(&self) -> Result<SqliteQueryResult> {
|
||||||
self.pool
|
self.pool
|
||||||
.execute(include_str!("../../tables.sql"))
|
.execute(DATABASE_TABLES)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Error::CreateTables(e.to_string()))
|
.map_err(|e| Error::CreateTables(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -6,7 +6,11 @@
|
||||||
//!
|
//!
|
||||||
//! Go to [config] module
|
//! 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 clap::Parser;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
@ -42,7 +46,7 @@ async fn main() {
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
let config_path = args.config;
|
let config_path = args.config;
|
||||||
let database_path = &args.database;
|
let database_path = args.database;
|
||||||
|
|
||||||
// if configuration file doesn't exist, create it
|
// if configuration file doesn't exist, create it
|
||||||
if !Path::new(&config_path).exists() {
|
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");
|
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
|
// open database connection
|
||||||
let db =
|
let db =
|
||||||
// if database file doesn't exists create it
|
// if database file doesn't exists create it
|
||||||
|
@ -69,10 +79,10 @@ async fn main() {
|
||||||
info!("Creating database file...");
|
info!("Creating database file...");
|
||||||
|
|
||||||
// create an empty 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
|
// open database file
|
||||||
let db = Database::open(database_path)
|
let db = Database::open(&database_path)
|
||||||
.await
|
.await
|
||||||
.expect("notrace - Failed to open database file");
|
.expect("notrace - Failed to open database file");
|
||||||
|
|
||||||
|
@ -85,7 +95,7 @@ async fn main() {
|
||||||
}
|
}
|
||||||
// if database file exists
|
// if database file exists
|
||||||
else {
|
else {
|
||||||
Database::open(database_path)
|
Database::open(&database_path)
|
||||||
.await
|
.await
|
||||||
.expect("notrace - Failed to open database file")
|
.expect("notrace - Failed to open database file")
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue