From 784ef88e81a9fb18069ab91587a731ac7e5b9c27 Mon Sep 17 00:00:00 2001 From: Francesco Tolomei <80046572+f-tlm@users.noreply.github.com> Date: Sun, 27 Mar 2022 17:12:13 +0200 Subject: [PATCH] "implemented" pathfinding. memo-cli will now look for an existing db file in the parent folder until it's found or until we hit root. currently will panic if the program lacks permissions to do so --- src/main.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index a4631d3..62baa55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use regex::Regex; use remote::RemoteSync; use storage::{AuthStorage, Memo, MemoStorage, SQLiteStorage, StateStorage, JsonStorage}; use utils::{find_by_regex, parse_human_duration, HumanDisplay}; +use std::path::PathBuf; const GIT_VERSION: &str = git_version!(); const PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -68,15 +69,32 @@ enum Commands { fn main() { let args = Cli::parse(); - //let home_path = std::env!("HOME").to_string(); //this is linux-only - let mut db_path: std::path::PathBuf = dirs::home_dir().unwrap(); - db_path.push(".local/share/memo-cli.db"); //+ "/.local/share/memo-cli.db"; - //WARNING: this is only for testing, it has no proper error handling and will panic is HOME is invalid or None - if let Some(db) = args.path { - db_path = std::path::PathBuf::from(db); + //might want to move the pathfinding to its own function + let mut db_path: PathBuf; + let filename = "memostorage.db"; //TODO: make this configurable + + if let Some(db) = args.path { //if we are given a starting path from command line, set it to that + db_path = PathBuf::from(db); + } else { //else use the current path, also should not browse up if path is specified + db_path = std::env::current_dir().unwrap(); + db_path.push(filename); //check if file exists in current folder + + while !db_path.exists() && !db_path.parent().is_none() { + db_path.pop().pop(); //browse parent folders until the given filename (or root) is found + db_path.push(filename); //we want to know about the db file, not the folder itself + } + + if (!db_path.exists()) { //fallback to default + //TODO: a less "nix-centered" default fallback, possibly configurable + //protip: cfg!(windows)/cfg!(unix)/cfg!(target_os = "macos") will give us the info we need + db_path = dirs::home_dir().unwrap(); + db_path.push(".local/share/"); //default fallback path + } } + //TODO: permissions check, this will panic if it finds an existing db it can't write to + let mut storage = SQLiteStorage::new(db_path.to_str().unwrap(), true).unwrap(); if args.sync {