"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
This commit is contained in:
Francesco Tolomei 2022-03-27 17:12:13 +02:00
parent 2e0d035b2f
commit 784ef88e81
No known key found for this signature in database
GPG key ID: 739BF0B2B1CEDE39

View file

@ -12,6 +12,7 @@ use regex::Regex;
use remote::RemoteSync; use remote::RemoteSync;
use storage::{AuthStorage, Memo, MemoStorage, SQLiteStorage, StateStorage, JsonStorage}; use storage::{AuthStorage, Memo, MemoStorage, SQLiteStorage, StateStorage, JsonStorage};
use utils::{find_by_regex, parse_human_duration, HumanDisplay}; use utils::{find_by_regex, parse_human_duration, HumanDisplay};
use std::path::PathBuf;
const GIT_VERSION: &str = git_version!(); const GIT_VERSION: &str = git_version!();
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
@ -68,15 +69,32 @@ enum Commands {
fn main() { fn main() {
let args = Cli::parse(); 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 { //might want to move the pathfinding to its own function
db_path = std::path::PathBuf::from(db); 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(); let mut storage = SQLiteStorage::new(db_path.to_str().unwrap(), true).unwrap();
if args.sync { if args.sync {