-
Notifications
You must be signed in to change notification settings - Fork 63
Add XDG Support to confy
#119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "confy" | ||
version = "1.0.0" | ||
version = "2.0.0" | ||
authors = ["Katharina Fey <[email protected]>"] | ||
description = "Boilerplate-free configuration management" | ||
license = "MIT/X11 OR Apache-2.0" | ||
|
@@ -11,12 +11,12 @@ edition = "2024" | |
|
||
[dependencies] | ||
ron = { version = "0.10.1", optional = true } | ||
directories = "6" | ||
etcetera = "0.10.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a link to https://docs.rs/etcetera/latest/etcetera/#native-strategy (or the discussion we had here) to let peoples know how to configure confy? At least some documentation (or example/test) around it to show can be useful. |
||
serde = "^1.0" | ||
serde_yaml = { version = "0.9", optional = true } | ||
thiserror = "2.0" | ||
basic-toml = { version = "0.1.10", optional = true } | ||
toml = { version = "0.8", optional = true } | ||
toml = { version = "0.9", optional = true } | ||
|
||
[features] | ||
default = ["toml_conf"] | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -73,9 +73,9 @@ | |||||||||
//! | ||||||||||
//! ### Tip | ||||||||||
//! to add this crate to your project with the default, toml config do the following: `cargo add confy`, otherwise do something like: `cargo add confy --no-default-features --features yaml_conf`, for more info, see [cargo docs on features] | ||||||||||
//! | ||||||||||
//! | ||||||||||
//! [cargo docs on features]: https://docs.rust-lang.org/cargo/reference/resolver.html#features | ||||||||||
//! | ||||||||||
//! | ||||||||||
//! feature | file format | description | ||||||||||
//! ------- | ----------- | ----------- | ||||||||||
//! **default**: `toml_conf` | [toml] | considered a reasonable default, uses the standard-compliant [`toml` crate] | ||||||||||
|
@@ -94,8 +94,8 @@ | |||||||||
mod utils; | ||||||||||
use utils::*; | ||||||||||
|
||||||||||
use directories::ProjectDirs; | ||||||||||
use serde::{de::DeserializeOwned, Serialize}; | ||||||||||
use etcetera::{AppStrategy, AppStrategyArgs, app_strategy::choose_app_strategy}; | ||||||||||
use serde::{Serialize, de::DeserializeOwned}; | ||||||||||
use std::fs::{self, File, OpenOptions, Permissions}; | ||||||||||
use std::io::{ErrorKind::NotFound, Write}; | ||||||||||
use std::path::{Path, PathBuf}; | ||||||||||
|
@@ -109,8 +109,8 @@ use toml::{ | |||||||||
|
||||||||||
#[cfg(feature = "basic_toml_conf")] | ||||||||||
use basic_toml::{ | ||||||||||
from_str as toml_from_str, to_string as toml_to_string_pretty, Error as TomlDeErr, | ||||||||||
Error as TomlSerErr, | ||||||||||
Error as TomlDeErr, Error as TomlSerErr, from_str as toml_from_str, | ||||||||||
to_string as toml_to_string_pretty, | ||||||||||
}; | ||||||||||
|
||||||||||
#[cfg(not(any( | ||||||||||
|
@@ -469,23 +469,26 @@ pub fn get_configuration_file_path<'a>( | |||||||||
config_name: impl Into<Option<&'a str>>, | ||||||||||
) -> Result<PathBuf, ConfyError> { | ||||||||||
let config_name = config_name.into().unwrap_or("default-config"); | ||||||||||
let project = ProjectDirs::from("rs", "", app_name).ok_or_else(|| { | ||||||||||
ConfyError::BadConfigDirectory("could not determine home directory path".to_string()) | ||||||||||
let project = choose_app_strategy(AppStrategyArgs { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I just understand now that you are "forcing" the app strategy? Not configurable then? (Still needs to document this.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I think you're right we should allow this to be configurable for GUI applications, I'll update! |
||||||||||
top_level_domain: "rs".to_string(), | ||||||||||
author: "".to_string(), | ||||||||||
app_name: app_name.to_string(), | ||||||||||
}) | ||||||||||
.map_err(|e| { | ||||||||||
ConfyError::BadConfigDirectory(format!("could not determine home directory path: {e}")) | ||||||||||
})?; | ||||||||||
|
||||||||||
let config_dir_str = get_configuration_directory_str(&project)?; | ||||||||||
|
||||||||||
let path = [config_dir_str, &format!("{config_name}.{EXTENSION}")] | ||||||||||
let path = [config_dir_str, format!("{config_name}.{EXTENSION}")] | ||||||||||
.iter() | ||||||||||
.collect(); | ||||||||||
|
||||||||||
Ok(path) | ||||||||||
} | ||||||||||
|
||||||||||
fn get_configuration_directory_str(project: &ProjectDirs) -> Result<&str, ConfyError> { | ||||||||||
let path = project.config_dir(); | ||||||||||
path.to_str() | ||||||||||
.ok_or_else(|| ConfyError::BadConfigDirectory(format!("{path:?} is not valid Unicode"))) | ||||||||||
fn get_configuration_directory_str(project: &impl AppStrategy) -> Result<String, ConfyError> { | ||||||||||
Ok(project.config_dir().display().to_string()) | ||||||||||
Comment on lines
+490
to
+491
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I know it is a private function, but maybe it is better to use the PathBuf directly instead of relying on the String approximation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think the function is used only once… it was a nice helper but may be useless now? |
||||||||||
} | ||||||||||
|
||||||||||
#[cfg(test)] | ||||||||||
|
@@ -601,10 +604,12 @@ mod tests { | |||||||||
|
||||||||||
store_path_perms(path, &config, permissions).expect("store_path_perms failed"); | ||||||||||
|
||||||||||
assert!(fs::metadata(path) | ||||||||||
.expect("reading metadata failed") | ||||||||||
.permissions() | ||||||||||
.readonly()); | ||||||||||
assert!( | ||||||||||
fs::metadata(path) | ||||||||||
.expect("reading metadata failed") | ||||||||||
.permissions() | ||||||||||
.readonly() | ||||||||||
); | ||||||||||
}) | ||||||||||
} | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure we have a change-log file, but writing somewhere what made change the major version can help peoples migrate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100% once we work out the API I am going to add into here: https://github.com/rust-cli/confy/?tab=readme-ov-file#breaking-changes