tagit_impl/
lib.rs

1//! Implementation of `tagit` CLI.
2
3use clap_complete::{generate, shells::Bash};
4use clap_mangen::generate_to;
5use tagit_command::Command;
6
7/// Handle [`Command`].
8pub fn run(
9    tagit_package: &'static str,
10    tagit_version: &'static str,
11    command: Command,
12    to_command: impl FnOnce() -> clap::Command,
13) -> anyhow::Result<()> {
14    match command {
15        #[cfg(feature = "tag")]
16        Command::Tag {
17            dry_run,
18            no_retag,
19            total_order,
20            sign,
21        } => tagit_tag::tag(
22            tagit_package,
23            tagit_version,
24            dry_run,
25            no_retag,
26            total_order,
27            sign,
28        ),
29        #[cfg(feature = "sub")]
30        Command::Sub { command } => tagit_sub::sub(command.unwrap_or_default()),
31        #[cfg(feature = "changelog")]
32        Command::Changelog { dry_run } => tagit_workspace_changelog::bump_changelog(dry_run),
33        Command::Completions => {
34            generate(
35                Bash,
36                &mut to_command(),
37                env!("CARGO_PKG_NAME"),
38                &mut std::io::stdout(),
39            );
40            Ok(())
41        }
42        Command::Doc => {
43            std::fs::create_dir_all("target/man1")?;
44            generate_to(to_command(), "target/man1")?;
45            Ok(())
46        }
47        #[cfg(not(feature = "tag"))]
48        Command::Tag { .. } => {
49            let _ = tagit_package;
50            let _ = tagit_version;
51            anyhow::bail!("enable `tag` feature")
52        }
53        #[cfg(not(feature = "sub"))]
54        Command::Sub { .. } => anyhow::bail!("enable `sub` feature"),
55        #[cfg(not(feature = "changelog"))]
56        Command::Changelog { .. } => anyhow::bail!("enable `changelog` feature"),
57    }
58}