use super::change_log::ChangelogGenerator;
use super::releasenotes::ReleaseNotesGenerator;
use crate::common::{CommonParams, DetailLevel};
use crate::config::Config;
use crate::git::GitRepo;
use crate::ui;
use anyhow::{Context, Result};
use colored::Colorize;
use std::env;
use std::str::FromStr;
use std::sync::Arc;
pub async fn handle_changelog_command(
common: CommonParams,
from: String,
to: Option<String>,
repository_url: Option<String>,
update_file: bool,
changelog_path: Option<String>,
version_name: Option<String>,
) -> Result<()> {
let mut config = Config::load()?;
common.apply_to_config(&mut config)?;
let mut spinner = ui::create_tui_spinner("Generating changelog...");
if let Err(e) = config.check_environment() {
ui::print_error(&format!("Error: {e}"));
ui::print_info("\nPlease ensure the following:");
ui::print_info("1. Git is installed and accessible from the command line.");
ui::print_info(
"2. You are running this command from within a Git repository or provide a repository URL with --repo.",
);
ui::print_info("3. You have set up your configuration using 'git config'.");
return Err(e);
}
let repo_url = repository_url.or(common.repository_url);
let git_repo = if let Some(url) = repo_url {
Arc::new(GitRepo::clone_remote_repository(&url).context("Failed to clone repository")?)
} else {
let repo_path = env::current_dir()?;
Arc::new(GitRepo::new(&repo_path).context("Failed to create GitRepo")?)
};
let git_repo_for_update = Arc::clone(&git_repo);
let to = to.unwrap_or_else(|| "HEAD".to_string());
let detail_level = DetailLevel::from_str(&common.detail_level)?;
let changelog =
ChangelogGenerator::generate(git_repo, &from, &to, &config, detail_level).await?;
spinner.tick();
ui::print_bordered_content(&changelog);
if update_file {
let path = changelog_path.unwrap_or_else(|| "CHANGELOG.md".to_string());
let mut update_spinner =
ui::create_tui_spinner(&format!("Updating changelog file at {path}..."));
match ChangelogGenerator::update_changelog_file(
&changelog,
&path,
&git_repo_for_update,
&to,
version_name,
) {
Ok(()) => {
update_spinner.tick();
ui::print_success(&format!(
"✨ Changelog successfully updated at {}",
path.bright_green()
));
}
Err(e) => {
update_spinner.tick();
ui::print_error(&format!("Failed to update changelog file: {e}"));
}
}
}
Ok(())
}
pub async fn handle_release_notes_command(
common: CommonParams,
from: String,
to: Option<String>,
repository_url: Option<String>,
version_name: Option<String>,
) -> Result<()> {
let mut config = Config::load()?;
common.apply_to_config(&mut config)?;
let mut spinner = ui::create_tui_spinner("Generating release notes...");
if let Err(e) = config.check_environment() {
ui::print_error(&format!("Error: {e}"));
ui::print_info("\nPlease ensure the following:");
ui::print_info("1. Git is installed and accessible from the command line.");
ui::print_info(
"2. You are running this command from within a Git repository or provide a repository URL with --repo.",
);
ui::print_info("3. You have set up your configuration using 'git config'.");
return Err(e);
}
let repo_url = repository_url.or(common.repository_url);
let git_repo = if let Some(url) = repo_url {
Arc::new(GitRepo::clone_remote_repository(&url).context("Failed to clone repository")?)
} else {
let repo_path = env::current_dir()?;
Arc::new(GitRepo::new(&repo_path).context("Failed to create GitRepo")?)
};
let to = to.unwrap_or_else(|| "HEAD".to_string());
let detail_level = DetailLevel::from_str(&common.detail_level)?;
let release_notes =
ReleaseNotesGenerator::generate(git_repo, &from, &to, &config, detail_level, version_name)
.await?;
spinner.tick();
ui::print_bordered_content(&release_notes);
Ok(())
}