Skip to content

Commit

Permalink
feat: application data directory (#22)
Browse files Browse the repository at this point in the history
* feat: app data dir logic

* fmt

* suggestion

* suggestions

* renamed

* empty org
  • Loading branch information
ogabrielides authored Nov 1, 2024
1 parent 2f571a9 commit 0ffa920
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ arboard = { version = "3.4.0", default-features = false, features = [
"windows-sys",
] }
enum_dispatch = "0.3.13"
directories = "5.0"

rusqlite = { version = "0.32.1", features = ["functions"]}
serde_yaml = "0.9.34+deprecated"
Expand Down
9 changes: 8 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::app_dir::{
app_user_data_file_path, copy_env_file_if_not_exists,
create_app_user_data_directory_if_not_exists,
};
use crate::context::AppContext;
use crate::database::Database;
use crate::logging::initialize_logger;
Expand Down Expand Up @@ -99,8 +103,11 @@ impl BitOrAssign for AppAction {
}
impl AppState {
pub fn new() -> Self {
create_app_user_data_directory_if_not_exists().expect("Failed to create app user_data directory");
copy_env_file_if_not_exists();
initialize_logger();
let db = Arc::new(Database::new("identities.db").unwrap());
let db_file_path = app_user_data_file_path("data.db").expect("should create db file path");
let db = Arc::new(Database::new(db_file_path).unwrap());
db.initialize().unwrap();

let settings = db.get_settings().expect("expected to get settings");
Expand Down
56 changes: 56 additions & 0 deletions src/app_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use directories::ProjectDirs;
use std::fs;
use std::path::{Path, PathBuf};

const QUALIFIER: &str = ""; // Typically empty on macOS and Linux
const ORGANIZATION: &str = "";
const APPLICATION: &str = "DashEvoTool";

pub fn app_user_data_dir_path() -> Result<PathBuf, std::io::Error> {
let proj_dirs = ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION)
.ok_or_else(|| std::io::Error::new(
std::io::ErrorKind::NotFound,
"Failed to determine project directories",
))?;
Ok(proj_dirs.config_dir().to_path_buf())
}
pub fn create_app_user_data_directory_if_not_exists() -> Result<(), std::io::Error> {
let app_data_dir = app_user_data_dir_path()?;
fs::create_dir_all(&app_data_dir)?;

// Verify directory permissions
let metadata = fs::metadata(&app_data_dir)?;
if !metadata.is_dir() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Created path is not a directory",
));
}
Ok(())
}

pub fn app_user_data_file_path(filename: &str) -> Result<PathBuf, std::io::Error> {
if filename.is_empty() || filename.contains(std::path::is_separator) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid filename",
));
}
let app_data_dir = app_user_data_dir_path()?;
Ok(app_data_dir.join(filename))
}

pub fn copy_env_file_if_not_exists() {
let app_data_dir = app_user_data_dir_path().expect("Failed to determine application data directory");
let env_file = app_data_dir.join(".env".to_string());
if env_file.exists() && env_file.is_file() {
} else {
let env_example_file = PathBuf::from(".env.example");
let target_env_file_path = app_user_data_file_path(".env").expect("should create target env file path");
fs::copy(
&env_example_file,
target_env_file_path,
)
.expect("Failed to copy main net env file");
}
}
4 changes: 3 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::str::FromStr;

use crate::app_dir::app_user_data_file_path;
use dash_sdk::dapi_client::AddressList;
use dash_sdk::dpp::dashcore::Network;
use dash_sdk::sdk::Uri;
Expand Down Expand Up @@ -59,7 +60,8 @@ impl Config {
/// Loads the configuration for all networks from environment variables and `.env` file.
pub fn load() -> Result<Self, ConfigError> {
// Load the .env file if available
if let Err(err) = dotenvy::from_path(".env") {
let env_file_path = app_user_data_file_path(".env").expect("should create .env file path");
if let Err(err) = dotenvy::from_path(env_file_path) {
tracing::warn!(
?err,
"Failed to load .env file. Continuing with environment variables."
Expand Down
5 changes: 4 additions & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::app_dir::app_user_data_file_path;
use std::panic;
use tracing::{error, info};
use tracing_subscriber::EnvFilter;

pub fn initialize_logger() {
// Initialize log file, with improved error handling
let log_file = match std::fs::File::create("explorer.log") {
let log_file_path = app_user_data_file_path("explorer.log").expect("should create log file path");
let log_file = match std::fs::File::create(log_file_path)
{
Ok(file) => file,
Err(e) => panic!("Failed to create log file: {:?}", e),
};
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod logging;
mod sdk_wrapper;
mod ui;

mod app_dir;
mod components;
mod context;
mod context_provider;
Expand Down

0 comments on commit 0ffa920

Please sign in to comment.