-
Notifications
You must be signed in to change notification settings - Fork 10
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
Parse configuration file #101
Conversation
Just to make sure, there is nothing you are waiting on from me here, correct @robinkrahl ? |
I’m waiting on a decision on #100 – if you want to go with this
approach, I can update the pull request with the missing documentation
etc.
|
I updated the pull request – I think it could be merged now. I made these changes to the patch series:
|
Thank you, Robin. I hope to take a closer look tomorrow. I'll probably merge it if it looks alright. From my perspective, it mostly comes down to technical details that differentiate the solutions and I don't have the time to experiment with alternative approaches right now. |
Seems as if we are pulling in two version of |
Sorry, I did not mean to close this, I was just cleaning up old branches.
Ultimately,
As far as I see, they are similar but not the same:
Which one is the third? |
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.
Publishing this review as GitHub doesn't allow me to comment otherwise. Comments may be stale & irrelevant but I didn't want them to get lost.
nitrocli/src/args.rs
Outdated
|
||
DeviceModel::from_str(v).map_err(|_| { | ||
E::custom(format!( | ||
"unexpected device model '{}', expected one of {}", |
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.
If I am not mistaken this string is contained in the final error in a way that only the expected type/values should be contained. Need to double check.
nitrocli/src/config.rs
Outdated
pub no_cache: bool, | ||
#[serde(default)] |
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.
Nit: Move one line down.
nitrocli/CHANGELOG.md
Outdated
@@ -11,6 +11,11 @@ Unreleased | |||
- Added support for configuration files: | |||
- Added `config` dependency in version `0.10.1` | |||
- Added `serde` dependency in version `1.0.104` | |||
- Reworked environment variables: | |||
- Added the `NITROCLI_MODEL` and `NITROCLI_VERBOSITY` variables that set the |
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.
"variables" -> "environment variables"?
nitrocli/src/config.rs
Outdated
@@ -37,9 +43,13 @@ pub struct Config { | |||
|
|||
impl Config { | |||
pub fn load() -> Result<Self> { | |||
let project_dirs = directories::ProjectDirs::from("", "", "nitrocli") | |||
.ok_or_else(|| error::Error::from("Could not determine the home directory"))?; |
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.
Lower case the error message for consistency?
nitrocli/src/config.rs
Outdated
@@ -37,9 +43,13 @@ pub struct Config { | |||
|
|||
impl Config { | |||
pub fn load() -> Result<Self> { | |||
let project_dirs = directories::ProjectDirs::from("", "", "nitrocli") |
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.
Mind introducing named variables for the two empty string arguments? It's not really clear what they refer to otherwise. With "nitrocli"
one can at least take a guess.
nitrocli/src/tests/run.rs
Outdated
|
||
#[test] | ||
fn config_file() { | ||
let mut config = ::config::Config::new(); |
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.
Indentation too wide.
Thanks for digging. I understand, but irrespective of who's fault it is, it's still not nice and something I'd really want to avoid.
|
In the next patch, we will implement configuration handling using the config crate. This patch adds a ConfigError variant to the Error struct that wraps the config::ConfigError returned by the config crate.
This patch implements basic configuration handling that reads a configuration file and stores the parsed data in the ExecCtx and RunCtx structs. It supports three configuration items: - model (previously only --model) - no_cache (previously only NITROCLI_NO_CACHE) - verbosity (previously only --verbose)
To make parsing easier, we use serde to deserialize the configuration data and store it in the Config struct. As the default Deserialize implementation for DeviceModel would only accept "Pro" and "Storage" and the Enum! macro does not allow per-field attributes, we manually implement Deserialize using the FromStr implementation.
This patch uses the config crate to parse the environment. A variable NITROCLI_KEY can be used to overwrite the configuration for *key*. This has the side effect that the NITROCLI_NO_CACHE variable is evaluated as a boolean variable (instead of only checking whether it is set). We also accept two new variables, NITROCLI_MODEL and NITROCLI_VERBOSITY.
This patch uses the directories crate to query the appropriate path for the configuration files. For Linux, paths according to the XDG Base Directory Specification are used. Note that directories does not yet support the XDG_CONFIG_DIRS variable for system-wide configuration files. Therefore we only use a user configuration file.
This patch adds a simple configuration file that demonstrates the syntax and contains some documentation. I suggest to ship this file together with nitrocli and to install it e. g. in the /usr/share/doc/nitrocli directory. This patch also adds a simple test case that makes sure that the example file is parsed correctly.
This patch adds a new --no-cache option that corresponds to the NITROCLI_NO_CACHE environment variable and the no_cache configuration. This makes the user interface more consistent as all configuration items are now backed by both an environment variable and a command-line option.
This patch updates the man page for the last changes: - new option --no-cache - changes to the environment variables - configuration files
Just noticing the update. Is this the updated pull request ready for review already, Robin? |
No, I just rebased it onto the current devel. I’ll open a new PR with
the new implementation so that you can compare both, okay?
|
Sounds good. |
We can probably close this pull request now that v2 has been merged. |
This patch series reads selected options (currently no_cache and model) from a
configuration file. This is only for discussion in #100. As I already
implemented it, I thought I might as well share it, even if we end up using a
different approach.