Skip to content

Commit

Permalink
feat(config): embed the default configuration file into the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Aug 18, 2021
1 parent 6f154ce commit e5148e3
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 10 deletions.
102 changes: 97 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tera = "1.12.1"
regex = "1.5.4"
serde_regex = "1.1.0"
indexmap = "1.7.0"
toml = "0.5.8"

[dependencies.git2]
version = "0.13.21"
Expand All @@ -27,9 +28,13 @@ version = "0.11.0"
default-features = false
features = ["toml", "yaml"]

[dev-dependencies]
pretty_assertions = "0.7"

[dependencies.git-conventional]
version = "0.9.2"
features = ["serde"]

[dependencies.rust-embed]
version = "6.0.0"
features = ["debug-embed"]

[dev-dependencies]
pretty_assertions = "0.7"
37 changes: 37 additions & 0 deletions git-cliff-core/src/embed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![allow(missing_docs)] // RustEmbed generated functions does not have doc comments

use crate::config::Config;
use crate::error::{
Error,
Result,
};
use rust_embed::RustEmbed;
use std::str;

/// Configuration file embedder/extractor.
///
/// Embeds `config/`[`DEFAULT_CONFIG`] into the binary.
///
/// [`DEFAULT_CONFIG`]: crate::DEFAULT_CONFIG
#[derive(Debug, RustEmbed)]
#[folder = "../config/"]
pub struct EmbeddedConfig;

impl EmbeddedConfig {
/// Extracts the embedded content.
pub fn get_config() -> Result<String> {
match Self::get(crate::DEFAULT_CONFIG) {
Some(v) => Ok(str::from_utf8(&v.data.into_owned()).unwrap().to_string()),
None => Err(Error::EmbeddedError(String::from(
"Embedded config not found",
))),
}
}

/// Parses the extracted content into [`Config`].
///
/// [`Config`]: Config
pub fn parse() -> Result<Config> {
Ok(toml::from_str(&Self::get_config()?)?)
}
}
6 changes: 6 additions & 0 deletions git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub enum Error {
/// Error that may occur while parsing the command line arguments.
#[error("Argument error: `{0}`")]
ArgumentError(String),
/// Error that may occur while extracting the embedded content.
#[error("Embedded error: `{0}`")]
EmbeddedError(String),
/// Errors that may occur when deserializing types from TOML format.
#[error("Cannot parse TOML: `{0}`")]
DeserializeError(#[from] toml::de::Error),
}

/// Result type of the core library.
Expand Down
2 changes: 2 additions & 0 deletions git-cliff-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub use regex;
pub mod commit;
/// Config file parser.
pub mod config;
/// Embedded file handler.
pub mod embed;
/// Error handling.
pub mod error;
/// Common release type.
Expand Down
11 changes: 9 additions & 2 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use args::Opt;
use changelog::Changelog;
use git_cliff_core::commit::Commit;
use git_cliff_core::config::Config;
use git_cliff_core::embed::EmbeddedConfig;
use git_cliff_core::error::{
Error,
Result,
Expand Down Expand Up @@ -38,13 +39,19 @@ pub fn run(mut args: Opt) -> Result<()> {
}

// Parse configuration file.
let mut config = Config::parse(match args.config.to_str() {
let path = match args.config.to_str() {
Some(v) => Ok(v.to_string()),
None => Err(Error::IoError(io::Error::new(
io::ErrorKind::Other,
"path contains invalid characters",
))),
}?)?;
}?;
let mut config = if fs::metadata(&path).is_ok() {
Config::parse(path)?
} else {
warn!("{:?} is not found, using the default configuration.", path);
EmbeddedConfig::parse()?
};

// Update the configuration based on command line arguments.
match args.strip.as_deref() {
Expand Down

0 comments on commit e5148e3

Please sign in to comment.