use std::env; use std::path::{Path, PathBuf}; use std::sync::OnceLock; /// /// # Parameters /// * `execution_path`: path of the command (directory arg) or current path /// * `root`: path of the repo pub struct Config { pub execution_path: PathBuf, pub is_custom_execution_path: bool, root: OnceLock>, } impl Config { pub fn new() -> Self { Config { execution_path: PathBuf::from(env::current_dir().unwrap()), is_custom_execution_path: false, root: OnceLock::new(), } } pub fn from(exec_path: Option<&String>) -> Self { match exec_path { Some(path) => Config { execution_path: PathBuf::from(path), is_custom_execution_path: true, root: OnceLock::new(), }, None => Config::new(), } } pub fn get_root(&self) -> &Option { self.root.get_or_init(|| { let mut path = self.execution_path.clone(); let root = loop { path.push(".nextsync"); if path.exists() { path.pop(); break Some(path); } path.pop(); path.pop(); if path == Path::new("/") { break None; } }; root }) } pub fn get_root_unsafe(&self) -> &PathBuf { match self.get_root() { Some(path) => path, None => panic!("fatal: not a nextsync repository (or any parent up to mount point /)"), } } }