Skip to content

Commit

Permalink
refactor(cli)!: move dry-run flag to individual sub-commands
Browse files Browse the repository at this point in the history
BREAKING-CHANGE: `--description` is renamed to `--comment`
BREAKING-CHANGE: `--dry-run` is no longer passed to the top-level command
  • Loading branch information
justinrubek committed May 18, 2024
1 parent c322382 commit ca04c78
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
17 changes: 8 additions & 9 deletions src/bin/bomp/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{BaseArgs, Bump, Changelog, RawBump};
use crate::cli::{Bump, Changelog, RawBump};
use bomper::{
changelog::generate_changelog_entry,
config::Config,
Expand All @@ -12,13 +12,12 @@ use similar::{ChangeTag, TextDiff};
use std::{fmt, io::Write, path::PathBuf, process::Command};

pub struct App {
pub args: BaseArgs,
pub config: Config,
}

impl App {
pub fn new(args: BaseArgs, config: Config) -> App {
App { args, config }
pub fn new(config: Config) -> App {
App { config }
}
}

Expand All @@ -30,7 +29,7 @@ impl App {

let increment = opts.options.determine_increment(&commits, &tag.version)?;
let new_tag = tag.increment_version(increment);
let version_description = if opts.description {
let version_description = if opts.comment {
if let Some(description) = prompt_for_description()? {
Some(description)
} else {
Expand All @@ -55,7 +54,7 @@ impl App {
};
let mut file_changes = determine_changes(&self.config, &replacement)?;
file_changes.push(apply_changelog(&changelog_entry)?);
if let Some(changes) = apply_changes(file_changes, &self.args)? {
if let Some(changes) = apply_changes(file_changes, opts.dry_run)? {
let new_tree = prepare_commit(&repo, &changes)?;
let object_id = repo.write_object(&new_tree)?;
let commit = repo.commit(
Expand Down Expand Up @@ -129,7 +128,7 @@ impl App {
new_version: opts.new_version.clone(),
};
let file_changes = determine_changes(&self.config, &replacement)?;
apply_changes(file_changes, &self.args)?;
apply_changes(file_changes, opts.dry_run)?;

Ok(())
}
Expand All @@ -138,8 +137,8 @@ impl App {
/// Persist file changes to the filesystem.
/// This function is responsible for respecting the `dry_run` flag, so it will only persist changes
/// if the flag is not set.
fn apply_changes(changes: Vec<file::Replacer>, args: &BaseArgs) -> Result<Option<Vec<PathBuf>>> {
if args.dry_run {
fn apply_changes(changes: Vec<file::Replacer>, dry_run: bool) -> Result<Option<Vec<PathBuf>>> {
if dry_run {
println!("Dry run, not persisting changes");
for replacer in changes {
let original = std::fs::read_to_string(&replacer.path).unwrap_or_default();
Expand Down
10 changes: 7 additions & 3 deletions src/bin/bomp/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub struct Args {
pub(crate) struct BaseArgs {
#[arg(short, long)]
pub config_file: Option<PathBuf>,
#[arg(short, long)]
pub dry_run: bool,
/// the directory of the repository to run on.
#[arg(short, long)]
pub repository: Option<PathBuf>,
Expand All @@ -38,6 +36,9 @@ pub(crate) enum Commands {
pub(crate) struct RawBump {
pub old_version: String,
pub new_version: String,

#[arg(short, long)]
pub dry_run: bool,
}

#[derive(clap::Args, Debug)]
Expand All @@ -47,7 +48,10 @@ pub(crate) struct Bump {

/// Whether to prompt for a hand-written summary message
#[arg(short, long)]
pub description: bool,
pub comment: bool,

#[arg(short, long)]
pub dry_run: bool,
}

#[derive(clap::Args, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/bin/bomp/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::from_ron(&config_path)?;
tracing::debug!(?config);

let app = App::new(args.base_args, config);
let app = App::new(config);
match args.command {
Commands::Changelog(changelog) => {
app.changelog(&changelog)?;
Expand Down

0 comments on commit ca04c78

Please sign in to comment.