diff options
author | Marcel Müller <neikos@neikos.email> | 2023-04-29 12:40:36 +0200 |
---|---|---|
committer | Marcel Müller <neikos@neikos.email> | 2023-04-29 12:49:23 +0200 |
commit | ff035f876813f7190e83f2532f164dddb4884467 (patch) | |
tree | ad2c1ccb91ba6d2b6979a6d919c50eaac38a127f | |
parent | 71253a2de7b4554ed1186d36a7b3194f1702b462 (diff) |
Add the ability for multiple config files
Signed-off-by: Marcel Müller <neikos@neikos.email>
-rw-r--r-- | src/config.rs | 26 | ||||
-rw-r--r-- | src/error.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 2 |
3 files changed, 23 insertions, 12 deletions
diff --git a/src/config.rs b/src/config.rs index cae5d7b..0ed2a6b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ use miette::IntoDiagnostic; use crate::error::Error; use crate::fragment::FragmentDataDesc; -pub const CONFIG_FILE_NAME: &str = ".changelog.toml"; +pub const CONFIG_FILE_NAMES: &[&str] = &[".changelog.toml", "changelog.toml"]; pub const DEFAULT_CONFIG: &str = include_str!("../assets/default_config.toml"); #[derive(Debug, getset::Getters, getset::CopyGetters, serde::Deserialize, serde::Serialize)] @@ -108,16 +108,24 @@ impl std::str::FromStr for EditFormat { /// Load the configuration from the repository pub fn load(repo_workdir_path: &Path) -> miette::Result<Configuration> { - let changelog_config_path = { - let mut cfg_path = repo_workdir_path.to_path_buf(); - cfg_path.push(CONFIG_FILE_NAME); - cfg_path - }; - - if !changelog_config_path.exists() { - miette::bail!(Error::ConfigDoesNotExist(changelog_config_path)) + let mut changelog_config_path = None; + for config_path in CONFIG_FILE_NAMES { + let check_path = { + let mut cfg_path = repo_workdir_path.to_path_buf(); + cfg_path.push(config_path); + cfg_path + }; + + if check_path.exists() { + changelog_config_path = Some(check_path); + break; + } } + let Some(changelog_config_path) = changelog_config_path else { + miette::bail!(Error::ConfigDoesNotExist) + }; + let config = std::fs::read_to_string(changelog_config_path) .map_err(Error::from) .into_diagnostic()?; diff --git a/src/error.rs b/src/error.rs index 5d869bc..9330d40 100644 --- a/src/error.rs +++ b/src/error.rs @@ -35,8 +35,11 @@ pub enum Error { #[error("Repository has no worktree")] NoWorkTree, - #[error("Configuration file does not exist: {0}")] - ConfigDoesNotExist(PathBuf), + #[error( + "Configuration file does not exist, tried {:?}", + crate::config::CONFIG_FILE_NAMES + )] + ConfigDoesNotExist, #[error("Not a file: {0}")] NotAFile(PathBuf), diff --git a/src/main.rs b/src/main.rs index 0802a24..0b27a63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,7 @@ fn init(repo_workdir_path: PathBuf) -> miette::Result<()> { .create(true) .append(false) .write(true) - .open(repo_workdir_path.join(crate::config::CONFIG_FILE_NAME)) + .open(repo_workdir_path.join(crate::config::CONFIG_FILE_NAMES[0])) .map_err(Error::from) .into_diagnostic()?; |