Skip to content
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

feat: add cli parameter to show the default configuration #95

Merged
merged 1 commit into from
Dec 23, 2022

Conversation

rbuckland
Copy link

Added a sample (from defaults) versionrc-default.yaml
Updated the README.md to discuss how the configuration is loaded

Copy link
Collaborator

@hdevalke hdevalke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR. It is always useful to quickly figure out what configuration is used.

README.md Outdated
Comment on lines 28 to 33
The configuration file is loaded in the following order
1. Load the internal defaults
* specified in [src/conventional/config.rs](src/conventional/config.rs),
* see these defaults as YAML in [./versionrc-default.yaml](versionrc-default.yaml).
2. Then override with values from the commandline, `convco -c|--config path/to/.versionrc`
3. Or, if not specificied via `-c|--config`, load `${PWD}/.versionrc` if it exists.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran markdownlint on it. This is the suggestion:

Suggested change
The configuration file is loaded in the following order
1. Load the internal defaults
* specified in [src/conventional/config.rs](src/conventional/config.rs),
* see these defaults as YAML in [./versionrc-default.yaml](versionrc-default.yaml).
2. Then override with values from the commandline, `convco -c|--config path/to/.versionrc`
3. Or, if not specificied via `-c|--config`, load `${PWD}/.versionrc` if it exists.
The configuration file is loaded in the following order
1. Load the internal defaults
- specified in [src/conventional/config.rs](src/conventional/config.rs),
- see these defaults as YAML in [./versionrc-default.yaml](versionrc-default.yaml).
2. Then override with values from the commandline, `convco -c|--config path/to/.versionrc`
3. Or, if not specificied via `-c|--config`, load `${PWD}/.versionrc` if it exists.

README.md Outdated
Comment on lines 35 to 37

To obtain the final derived configuration that convco runs with
- run `convco config get`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To obtain the final derived configuration that convco runs with
- run `convco config get`
To get the final derived configuration, run `convco config get`.

@@ -4,6 +4,7 @@ mod changelog;
mod check;
mod commit;
mod version;
mod config;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run cargo +nightly fmt it will order the modules and change some formatting here and there

  • cargo +nightly fmt

@@ -0,0 +1,105 @@
# Header to be used for the CHANGELOG
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not add it in the repo as it would have to be kept in sync. It is already part of the documentation site: https://convco.github.io/configuration/ too.

As this new subcommand is used to print out the configuration.
Maybe it is better to make it print out the default configuration as well.

e.g.:

convco config # print out the current config
convco config --default # print out the default config

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a patch for all remarks i made:

diff --git a/README.md b/README.md
index b925768..a50d699 100644
--- a/README.md
+++ b/README.md
@@ -26,15 +26,14 @@ This build depends on `git2` with the `zlib-ng-compat` feature. It requires `cma
 `convco` uses follows the [conventional-changelog-config-spec][3].
 
 The configuration file is loaded in the following order
+
 1. Load the internal defaults
-   * specified in [src/conventional/config.rs](src/conventional/config.rs),
-   * see these defaults as YAML in [./versionrc-default.yaml](versionrc-default.yaml).
+    - specified in [src/conventional/config.rs](src/conventional/config.rs),
+    - see these defaults as YAML in [./versionrc-default.yaml](versionrc-default.yaml).
 2. Then override with values from the commandline, `convco -c|--config path/to/.versionrc`
 3. Or, if not specificied via `-c|--config`, load `${PWD}/.versionrc` if it exists.
 
-
-To obtain the final derived configuration that convco runs with
-  - run `convco config get`
+To get the final derived configuration run `convco config get`.
 
 The `host: ...`, `owner: ...` and `repository: ...` when not supplied via custom or the `.versionrc` are loaded
 from the `git remote origin` value.
diff --git a/src/cli.rs b/src/cli.rs
index d73aa6c..3adf30c 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,6 +1,6 @@
 use std::{path::PathBuf, str::FromStr};
 
-use clap::{Parser, Subcommand};
+use clap::Parser;
 
 #[derive(Debug, Parser)]
 #[clap(name = "convco", about = "Conventional commit tools", version)]
@@ -30,15 +30,9 @@ pub enum Command {
 
 #[derive(Debug, Parser)]
 pub struct ConfigCommand {
-    /// what action for config
-    #[clap(subcommand)]
-    pub action: ConfigAction,
-}
-
-#[derive(Debug, Subcommand)]
-pub enum ConfigAction {
-    /// Displays the current configuriation
-    Get,
+    /// Print out the default configuration instead of the current configuration.
+    #[clap(short, long)]
+    pub default: bool,
 }
 
 #[derive(Debug, Parser)]
diff --git a/src/cmd.rs b/src/cmd.rs
index 5251fc8..bfb1974 100644
--- a/src/cmd.rs
+++ b/src/cmd.rs
@@ -3,8 +3,8 @@ use crate::conventional::Config;
 mod changelog;
 mod check;
 mod commit;
-mod version;
 mod config;
+mod version;
 
 pub(crate) trait Command {
     fn exec(&self, config: Config) -> anyhow::Result<()>;
diff --git a/src/cmd/config.rs b/src/cmd/config.rs
index 176c3c7..960af44 100644
--- a/src/cmd/config.rs
+++ b/src/cmd/config.rs
@@ -1,27 +1,25 @@
+use std::io::{stdout, Write};
 
-use crate::{
-    cli::{ConfigCommand, ConfigAction},
-    cmd::Command,
-    conventional::Config,
-    error::Error,
-};
-
+use crate::{cli::ConfigCommand, cmd::Command, conventional::Config, error::Error};
 
 impl ConfigCommand {
-    fn as_yaml_string(&self, config: &Config) -> Result<String, Error> {
-        Ok(serde_yaml::to_string(config)?)
+    fn write_yaml(&self, config: &Config, w: impl Write) -> Result<(), Error> {
+        Ok(serde_yaml::to_writer(w, config)?)
     }
 }
 
 impl Command for ConfigCommand {
     fn exec(&self, config: Config) -> anyhow::Result<()> {
-        println!("{}",self.as_yaml_string(&config)?);
+        let config = if self.default {
+            Config::default()
+        } else {
+            config
+        };
+        self.write_yaml(&config, stdout().lock())?;
         Ok(())
     }
 }
 
-
-
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -30,9 +28,13 @@ mod tests {
     /// config is working
     #[test]
     fn test_as_yaml() {
-        let config_cmd: ConfigCommand = ConfigCommand{action: ConfigAction::Get};
+        let config_cmd: ConfigCommand = ConfigCommand { default: true };
         let config: Config = Config::default();
-        let yaml_config_default = config_cmd.as_yaml_string(&config).unwrap();
+        let mut yaml_config_default = Vec::new();
+        config_cmd
+            .write_yaml(&config, &mut yaml_config_default)
+            .unwrap();
+        let yaml_config_default = String::from_utf8(yaml_config_default).unwrap();
         let reparsed_config: Config = serde_yaml::from_str(&yaml_config_default).unwrap();
         assert_eq!(&reparsed_config, &config);
     }

Comment on lines 10 to 21
impl ConfigCommand {
fn as_yaml_string(&self, config: &Config) -> Result<String, Error> {
Ok(serde_yaml::to_string(config)?)
}
}

impl Command for ConfigCommand {
fn exec(&self, config: Config) -> anyhow::Result<()> {
println!("{}",self.as_yaml_string(&config)?);
Ok(())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl ConfigCommand {
fn as_yaml_string(&self, config: &Config) -> Result<String, Error> {
Ok(serde_yaml::to_string(config)?)
}
}
impl Command for ConfigCommand {
fn exec(&self, config: Config) -> anyhow::Result<()> {
println!("{}",self.as_yaml_string(&config)?);
Ok(())
}
}
impl ConfigCommand {
fn write_yaml(&self, config: &Config, w: impl Write) -> Result<(), Error> {
Ok(serde_yaml::to_writer(w, config)?)
}
}
impl Command for ConfigCommand {
fn exec(&self, config: Config) -> anyhow::Result<()> {
self.write_yaml(&config, stdout().lock())?;
Ok(())
}
}

if we ever decide to write down the config to a file this will be easier

Either the derived configuration or the default configuration can be
shown.
Updated the README.md to discuss how the configuration is loaded.

```
convco config [--default]
```

Refs: convco#95
@hdevalke hdevalke merged commit 2a8c92a into convco:master Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants