Skip to main content

tagit_impl/
lib.rs

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