Skip to content

Commit 6acab5c

Browse files
feat: adding verbose CLI option (#232)
1 parent d88a55f commit 6acab5c

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ A tooling and language agnostic utility to calculate the next semantic version b
2020
+ [Usage - Consecutive Mode](#usage-consecutive-mode)
2121
+ [Usage - Batch Mode](#usage-batch-mode)
2222
+ [Usage - Git Environment Variables](#usage-git-environment-variables)
23-
+ [Usage - Logging](#usage-logging)
2423
* [CICD Examples](#cicd-examples)
2524
+ [GitLab CI Rust Project Example](#gitlab-ci-rust-project-example)
2625
+ [Via Cargo](#via-cargo)
@@ -127,12 +126,6 @@ When `${GIT_DIR}` is set, it takes precedence and Conventional Commits Next Vers
127126
When `${GIT_DIR}` is not set, Conventional Commits Next Version searches for a repository beginning in the current directory.
128127

129128

130-
### Usage - Logging
131-
The crates `pretty_env_logger` and `log` are used to provide logging.
132-
The environment variable `RUST_LOG` can be used to set the logging level.
133-
See [https://crates.io/crates/pretty_env_logger](https://crates.io/crates/pretty_env_logger) for more detailed documentation.
134-
135-
136129
## CICD Examples
137130
### GitLab CI Rust Project Example
138131
#### Via Cargo

src/cli/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ pub(crate) struct Arguments {
4141
)]
4242
pub(crate) current_version: Option<Version>,
4343

44+
#[arg(
45+
long,
46+
help = "Enable verbose output, respects RUST_LOG environment variable if set."
47+
)]
48+
pub(crate) verbose: bool,
49+
4450
#[arg(
4551
help = "The Git reference from where to start taking the range of commits from till HEAD to lint. The range is inclusive of HEAD and exclusive of the provided reference. '-' indicates to read the standard input and lint the input as a Git commit message."
4652
)]

src/commits/commit/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Commit {
4747
pub(super) fn from_git(commit: &git2::Commit) -> Commit {
4848
let message = match commit.message().map(|m| m.to_string()) {
4949
Some(message) => {
50-
trace!(
50+
debug!(
5151
"Found the commit message {message:?} for the commit with the hash '{}'.",
5252
commit.id()
5353
);

src/commits/filters/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Filters {
6969

7070
if !self.commits_must_effect.is_empty() {
7171
let files_in_commit = get_all_files_changed_in_commit(repository, commit)?;
72-
trace!(
72+
debug!(
7373
"Commit with the hash '{}' changes the files {files_in_commit:?}.",
7474
commit.id(),
7575
);
@@ -83,7 +83,7 @@ impl Filters {
8383
for file_in_commit in files_in_commit {
8484
for filter in &self.commits_must_effect {
8585
if file_in_commit.starts_with(filter) {
86-
trace!("The file {file_in_commit:?} affects the path {filter:?}.");
86+
debug!("The file {file_in_commit:?} affects the path {filter:?}.");
8787
return true;
8888
}
8989
}

src/commits/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ fn get_commits_till_head_from_oid(
168168
}
169169
}
170170

171-
debug!("Operating upon {} commits.", commits.len());
171+
if commits.is_empty() {
172+
bail!("No Git commits within the provided range.");
173+
}
174+
175+
info!("Found {} commits within the provided range.", commits.len());
172176
Ok(Commits { commits })
173177
}
174178

@@ -178,7 +182,7 @@ fn get_reference_oid(repository: &Repository, matching: &str) -> Result<Oid> {
178182
.context(format!(
179183
"Could not find a reference with the name {matching:?}."
180184
))?;
181-
trace!(
185+
debug!(
182186
"Matched {:?} to the reference {:?}.",
183187
matching,
184188
reference.name().unwrap()
@@ -190,7 +194,7 @@ fn get_reference_oid(repository: &Repository, matching: &str) -> Result<Oid> {
190194
fn parse_to_oid(repository: &Repository, oid: &str) -> Result<Oid> {
191195
match oid.len() {
192196
1..=39 => {
193-
trace!("Attempting to find a match for the short commit hash {oid:?}.");
197+
debug!("Attempting to find a match for the short commit hash {oid:?}.");
194198
let matching_oid_lowercase = oid.to_lowercase();
195199

196200
let mut revwalker = repository.revwalk()?;
@@ -202,6 +206,7 @@ fn parse_to_oid(repository: &Repository, oid: &str) -> Result<Oid> {
202206
let oid_lowercase = oid.to_string().to_lowercase();
203207

204208
if oid_lowercase.starts_with(&matching_oid_lowercase) {
209+
debug!("Found a match for the short commit hash {oid:?}.");
205210
return Some(oid);
206211
}
207212

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ mod history_mode;
2121
const ERROR_EXIT_CODE: i32 = 1;
2222

2323
fn main() {
24-
pretty_env_logger::init();
25-
trace!("Version {}.", env!("CARGO_PKG_VERSION"));
24+
info!("Version {}.", env!("CARGO_PKG_VERSION"));
2625
let arguments = Arguments::parse();
27-
trace!("The command line arguments provided are {arguments:?}.");
26+
debug!("The command line arguments provided are {arguments:?}.");
27+
28+
// Set up logging: if verbose is true and RUST_LOG is not set, default to info level
29+
if arguments.verbose && std::env::var("RUST_LOG").is_err() {
30+
std::env::set_var("RUST_LOG", "info");
31+
}
32+
33+
pretty_env_logger::init();
2834

2935
if let Err(err) = run(arguments) {
3036
error!("{err:?}");

0 commit comments

Comments
 (0)