fix: stop --dry-run from writing to the working tree
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use super::{Django, Link, Postgres};
|
||||
use clap::builder::styling::{AnsiColor, Effects, Styles};
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap::error::ErrorKind;
|
||||
use clap::{CommandFactory, Parser, Subcommand};
|
||||
use clap_complete::Shell;
|
||||
|
||||
/// A program for interacting with various dockerized applications
|
||||
@@ -55,13 +56,63 @@ fn help_styles() -> Styles {
|
||||
.placeholder(AnsiColor::Cyan.on_default())
|
||||
}
|
||||
|
||||
/// Exit with a usage error when `--dry-run` would be a lie
|
||||
pub fn reject_unsupported_dry_run(command: &Commands) {
|
||||
if let Some(name) = writes_locally(command) {
|
||||
Ahab::command()
|
||||
.error(
|
||||
ErrorKind::ArgumentConflict,
|
||||
format!("--dry-run is not supported by `{name}`, which writes to the working tree"),
|
||||
)
|
||||
.exit()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:(@janezicmatej) honour --dry-run in these commands instead of refusing it
|
||||
fn writes_locally(command: &Commands) -> Option<&'static str> {
|
||||
match command {
|
||||
Commands::Link { command } => match command {
|
||||
Link::Add { .. } => Some("link add"),
|
||||
Link::Restore { .. } => Some("link restore"),
|
||||
Link::Check { .. } => None,
|
||||
},
|
||||
Commands::Django { command } => match command {
|
||||
Django::MakeCommand { .. } => Some("django make-command"),
|
||||
_ => None,
|
||||
},
|
||||
Commands::Postgres { .. } | Commands::Completions { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Ahab;
|
||||
use clap::CommandFactory;
|
||||
use clap::{CommandFactory, Parser};
|
||||
|
||||
#[test]
|
||||
fn the_command_is_well_formed() {
|
||||
Ahab::command().debug_assert();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dry_run_is_refused_only_where_it_cannot_be_honoured() {
|
||||
let command = |args: &[&str]| Ahab::try_parse_from(args).unwrap().command;
|
||||
|
||||
assert_eq!(
|
||||
super::writes_locally(&command(&["ahab", "link", "add", ".env"])),
|
||||
Some("link add")
|
||||
);
|
||||
assert_eq!(
|
||||
super::writes_locally(&command(&["ahab", "django", "make-command", "app", "name"])),
|
||||
Some("django make-command")
|
||||
);
|
||||
assert_eq!(
|
||||
super::writes_locally(&command(&["ahab", "link", "check"])),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
super::writes_locally(&command(&["ahab", "postgres", "dump", "out.sql"])),
|
||||
None
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user