refactor: put every filesystem write behind one module

This commit is contained in:
2026-09-08 12:04:51 +00:00
parent bbbca755be
commit a3c08f6eaa
10 changed files with 261 additions and 236 deletions

View File

@@ -1,7 +1,7 @@
use super::{Django, Link, Postgres};
use clap::builder::styling::{AnsiColor, Effects, Styles};
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser, Subcommand};
use clap::{Parser, Subcommand};
use clap_complete::Shell;
/// A program for interacting with various dockerized applications
@@ -56,63 +56,13 @@ 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, Parser};
use clap::CommandFactory;
#[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
);
}
}