feat(cli): default dotfiles dir to ~/.notfiles, config to ~/.config/notfiles/notfiles.toml
Some checks failed
Public Repo Readiness / Check Public Repo Readiness (push) Has been cancelled
Some checks failed
Public Repo Readiness / Check Public Repo Readiness (push) Has been cancelled
- Add `default_dotfiles_dir()` (~/.notfiles) and `default_config_path()` (~/.config/notfiles/notfiles.toml) to notcore::config - Add `Config::load_from(path)` for explicit config path; keep `Config::load(dir)` as the legacy/library variant (config inside dotfiles dir) - CLI resolves config via XDG default or --config override; library functions stay legacy so the public API is unaffected - `notfiles init` creates ~/.notfiles and ~/.config/notfiles/notfiles.toml - Add --config global flag to override the config path - Update integration tests to pass --config explicitly so they are isolated from any real ~/.config/notfiles/notfiles.toml on the test machine
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::NotfilesError;
|
||||
|
||||
@@ -102,13 +102,28 @@ where
|
||||
toml::from_str(&content).map_err(|err| parse_error(path, err))
|
||||
}
|
||||
|
||||
/// Returns the default config path: `~/.config/notfiles/notfiles.toml`.
|
||||
pub fn default_config_path() -> PathBuf {
|
||||
dirs::config_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("~/.config"))
|
||||
.join("notfiles")
|
||||
.join("notfiles.toml")
|
||||
}
|
||||
|
||||
/// Returns the default dotfiles directory: `~/.notfiles`.
|
||||
pub fn default_dotfiles_dir() -> PathBuf {
|
||||
dirs::home_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("~"))
|
||||
.join(".notfiles")
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(dotfiles_dir: &Path) -> Result<Self, NotfilesError> {
|
||||
let config_path = dotfiles_dir.join("notfiles.toml");
|
||||
/// Load config from an explicit path. Returns `Config::default()` if the file does not exist.
|
||||
pub fn load_from(config_path: &Path) -> Result<Self, NotfilesError> {
|
||||
if !config_path.exists() {
|
||||
return Ok(Config::default());
|
||||
}
|
||||
let content = std::fs::read_to_string(&config_path).map_err(|e| {
|
||||
let content = std::fs::read_to_string(config_path).map_err(|e| {
|
||||
NotfilesError::Config(format!("reading {}: {e}", config_path.display()))
|
||||
})?;
|
||||
let config: Config = toml::from_str(&content).map_err(|e| {
|
||||
@@ -117,6 +132,12 @@ impl Config {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Load config from `<dotfiles_dir>/notfiles.toml` (legacy location).
|
||||
/// Prefer [`Config::load_from`] with an explicit path in new call sites.
|
||||
pub fn load(dotfiles_dir: &Path) -> Result<Self, NotfilesError> {
|
||||
Self::load_from(&dotfiles_dir.join("notfiles.toml"))
|
||||
}
|
||||
|
||||
pub fn method_for(&self, package: &str) -> Method {
|
||||
self.packages
|
||||
.get(package)
|
||||
|
||||
Reference in New Issue
Block a user