git_iris/changes/
cli.rs

1use super::changelog::ChangelogGenerator;
2use super::releasenotes::ReleaseNotesGenerator;
3use crate::common::{CommonParams, DetailLevel};
4use crate::config::Config;
5use crate::git::GitRepo;
6use crate::ui;
7use anyhow::{Context, Result};
8use colored::Colorize;
9use std::env;
10use std::str::FromStr;
11use std::sync::Arc;
12
13/// Handles the changelog generation command.
14///
15/// This function orchestrates the process of generating a changelog based on the provided
16/// parameters. It sets up the necessary environment, creates a `GitRepo` instance,
17/// and delegates the actual generation to the `ChangelogGenerator`.
18///
19/// # Arguments
20///
21/// * `common` - Common parameters for the command, including configuration overrides.
22/// * `from` - The starting point (commit or tag) for the changelog.
23/// * `to` - The ending point for the changelog. Defaults to "HEAD" if not provided.
24///
25/// # Returns
26///
27/// Returns a Result indicating success or containing an error if the operation failed.
28pub async fn handle_changelog_command(
29    common: CommonParams,
30    from: String,
31    to: Option<String>,
32) -> Result<()> {
33    // Load and apply configuration
34    let mut config = Config::load()?;
35    common.apply_to_config(&mut config)?;
36
37    // Create a spinner to indicate progress
38    let spinner = ui::create_spinner("Generating changelog...");
39
40    // Ensure we're in a git repository
41    if let Err(e) = config.check_environment() {
42        ui::print_error(&format!("Error: {e}"));
43        ui::print_info("\nPlease ensure the following:");
44        ui::print_info("1. Git is installed and accessible from the command line.");
45        ui::print_info("2. You are running this command from within a Git repository.");
46        ui::print_info("3. You have set up your configuration using 'git-iris config'.");
47        return Err(e);
48    }
49
50    // Get the current directory and create a GitRepo instance
51    let repo_path = env::current_dir()?;
52    let git_repo = Arc::new(GitRepo::new(&repo_path).context("Failed to create GitRepo")?);
53
54    // Set the default 'to' reference if not provided
55    let to = to.unwrap_or_else(|| "HEAD".to_string());
56
57    // Parse the detail level for the changelog
58    let detail_level = DetailLevel::from_str(&common.detail_level)?;
59
60    // Generate the changelog
61    let changelog =
62        ChangelogGenerator::generate(git_repo, &from, &to, &config, detail_level).await?;
63
64    // Clear the spinner and display the result
65    spinner.finish_and_clear();
66
67    println!("{}", "━".repeat(50).bright_purple());
68    println!("{}", &changelog);
69    println!("{}", "━".repeat(50).bright_purple());
70
71    Ok(())
72}
73
74/// Handles the release notes generation command.
75///
76/// This function orchestrates the process of generating release notes based on the provided
77/// parameters. It sets up the necessary environment, creates a `GitRepo` instance,
78/// and delegates the actual generation to the `ReleaseNotesGenerator`.
79///
80/// # Arguments
81///
82/// * `common` - Common parameters for the command, including configuration overrides.
83/// * `from` - The starting point (commit or tag) for the release notes.
84/// * `to` - The ending point for the release notes. Defaults to "HEAD" if not provided.
85///
86/// # Returns
87///
88/// Returns a Result indicating success or containing an error if the operation failed.
89pub async fn handle_release_notes_command(
90    common: CommonParams,
91    from: String,
92    to: Option<String>,
93) -> Result<()> {
94    // Load and apply configuration
95    let mut config = Config::load()?;
96    common.apply_to_config(&mut config)?;
97
98    // Create a spinner to indicate progress
99    let spinner = ui::create_spinner("Generating release notes...");
100
101    // Check environment prerequisites
102    if let Err(e) = config.check_environment() {
103        ui::print_error(&format!("Error: {e}"));
104        ui::print_info("\nPlease ensure the following:");
105        ui::print_info("1. Git is installed and accessible from the command line.");
106        ui::print_info("2. You are running this command from within a Git repository.");
107        ui::print_info("3. You have set up your configuration using 'git-iris config'.");
108        return Err(e);
109    }
110
111    // Get the current directory and create a GitRepo instance
112    let repo_path = env::current_dir()?;
113    let git_repo = Arc::new(GitRepo::new(&repo_path).context("Failed to create GitRepo")?);
114
115    // Set the default 'to' reference if not provided
116    let to = to.unwrap_or_else(|| "HEAD".to_string());
117
118    // Parse the detail level for the release notes
119    let detail_level = DetailLevel::from_str(&common.detail_level)?;
120
121    // Generate the release notes
122    let release_notes =
123        ReleaseNotesGenerator::generate(git_repo, &from, &to, &config, detail_level).await?;
124
125    // Clear the spinner and display the result
126    spinner.finish_and_clear();
127
128    println!("{}", "━".repeat(50).bright_purple());
129    println!("{}", &release_notes);
130    println!("{}", "━".repeat(50).bright_purple());
131
132    Ok(())
133}