From b13e3916f6c83b205c43b5df7cd645d34f7a7ab9 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 1 Aug 2023 17:11:59 +0200 Subject: [PATCH] refactor: move gitea command into its own file Signed-off-by: kjuulh --- crates/cuddle-please-commands/src/command.rs | 109 ++---------------- .../src/config_command.rs | 7 +- .../src/gitea_command.rs | 102 ++++++++++++++++ crates/cuddle-please-commands/src/lib.rs | 1 + .../src/release_command.rs | 2 +- crates/cuddle-please/src/command.rs | 1 - 6 files changed, 117 insertions(+), 105 deletions(-) create mode 100644 crates/cuddle-please-commands/src/gitea_command.rs delete mode 100644 crates/cuddle-please/src/command.rs diff --git a/crates/cuddle-please-commands/src/command.rs b/crates/cuddle-please-commands/src/command.rs index 90e2aa4..b070f39 100644 --- a/crates/cuddle-please-commands/src/command.rs +++ b/crates/cuddle-please-commands/src/command.rs @@ -12,7 +12,11 @@ use cuddle_please_misc::{ LocalGitClient, NextVersion, RemoteGitEngine, StdinFn, VcsClient, }; -use crate::{config_command::ConfigCommandHandler, release_command::ReleaseCommand}; +use crate::{ + config_command::{ConfigCommand, ConfigCommandHandler}, + gitea_command::{GiteaCommand, GiteaCommandHandler}, + release_command::ReleaseCommand, +}; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -89,8 +93,6 @@ impl Command { match &self.commands { Some(Commands::Release {}) => { - tracing::debug!("running command: release"); - ReleaseCommand::new(config, git_client, gitea_client) .execute(self.global.dry_run)?; } @@ -99,77 +101,8 @@ impl Command { ConfigCommandHandler::new(self.ui, config).execute(command)?; } Some(Commands::Gitea { command }) => { - let git_url = url::Url::parse(config.get_api_url())?; - - let mut url = String::new(); - url.push_str(git_url.scheme()); - url.push_str("://"); - url.push_str(&git_url.host().unwrap().to_string()); - if let Some(port) = git_url.port() { - url.push_str(format!(":{port}").as_str()); - } - - let client = GiteaClient::new(&url, self.global.token.as_deref()); - match command { - GiteaCommand::Connect {} => { - client.connect(config.get_owner(), config.get_repository())?; - self.ui.write_str_ln("connected succesfully go gitea"); - } - GiteaCommand::Tags { command } => match command { - Some(GiteaTagsCommand::MostSignificant {}) => { - let tags = - client.get_tags(config.get_owner(), config.get_repository())?; - - match get_most_significant_version(tags.iter().collect()) { - Some(tag) => { - self.ui.write_str_ln(&format!( - "found most significant tags: {}", - tag.name - )); - } - None => { - self.ui.write_str_ln("found no tags with versioning schema"); - } - } - } - None => { - let tags = - client.get_tags(config.get_owner(), config.get_repository())?; - self.ui.write_str_ln("got tags from gitea"); - for tag in tags { - self.ui.write_str_ln(&format!("- {}", tag.name)) - } - } - }, - GiteaCommand::SinceCommit { sha, branch } => { - let commits = client.get_commits_since( - config.get_owner(), - config.get_repository(), - Some(sha), - branch, - )?; - self.ui.write_str_ln("got commits from gitea"); - for commit in commits { - self.ui.write_str_ln(&format!("- {}", commit.get_title())) - } - } - GiteaCommand::CheckPr {} => { - let pr = - client.get_pull_request(config.get_owner(), config.get_repository())?; - - match pr { - Some(index) => { - self.ui.write_str_ln(&format!( - "found cuddle-please (index={}) pr from gitea", - index - )); - } - None => { - self.ui.write_str_ln("found no cuddle-please pr from gitea"); - } - } - } - } + GiteaCommandHandler::new(self.ui, config, gitea_client) + .execute(command, self.global.token.expect("token to be set").deref())?; } Some(Commands::Doctor {}) => { match std::process::Command::new("git").arg("-v").output() { @@ -246,34 +179,6 @@ pub enum Commands { Doctor {}, } -#[derive(Subcommand, Debug, Clone)] -pub enum ConfigCommand { - /// List will list the final configuration - List {}, -} - -#[derive(Subcommand, Debug, Clone)] -pub enum GiteaCommand { - Connect {}, - Tags { - #[command(subcommand)] - command: Option, - }, - SinceCommit { - #[arg(long)] - sha: String, - - #[arg(long)] - branch: String, - }, - CheckPr {}, -} - -#[derive(Subcommand, Debug, Clone)] -pub enum GiteaTagsCommand { - MostSignificant {}, -} - fn get_current_path( optional_current_dir: Option<&Path>, optional_source_path: Option, diff --git a/crates/cuddle-please-commands/src/config_command.rs b/crates/cuddle-please-commands/src/config_command.rs index 75f8eb3..6904053 100644 --- a/crates/cuddle-please-commands/src/config_command.rs +++ b/crates/cuddle-please-commands/src/config_command.rs @@ -1,7 +1,12 @@ +use clap::Subcommand; use cuddle_please_frontend::PleaseConfig; use cuddle_please_misc::DynUi; -use crate::command::ConfigCommand; +#[derive(Subcommand, Debug, Clone)] +pub enum ConfigCommand { + /// List will list the final configuration + List {}, +} pub struct ConfigCommandHandler { ui: DynUi, diff --git a/crates/cuddle-please-commands/src/gitea_command.rs b/crates/cuddle-please-commands/src/gitea_command.rs new file mode 100644 index 0000000..e59396d --- /dev/null +++ b/crates/cuddle-please-commands/src/gitea_command.rs @@ -0,0 +1,102 @@ +use clap::Subcommand; +use cuddle_please_frontend::PleaseConfig; +use cuddle_please_misc::{get_most_significant_version, DynRemoteGitClient, DynUi, GiteaClient}; + +#[derive(Subcommand, Debug, Clone)] +pub enum GiteaCommand { + Connect {}, + Tags { + #[command(subcommand)] + command: Option, + }, + SinceCommit { + #[arg(long)] + sha: String, + + #[arg(long)] + branch: String, + }, + CheckPr {}, +} + +#[derive(Subcommand, Debug, Clone)] +pub enum GiteaTagsCommand { + MostSignificant {}, +} + +pub struct GiteaCommandHandler { + ui: DynUi, + config: PleaseConfig, + gitea_client: DynRemoteGitClient, +} + +impl GiteaCommandHandler { + pub fn new(ui: DynUi, config: PleaseConfig, gitea_client: DynRemoteGitClient) -> Self { + Self { + ui, + config, + gitea_client, + } + } + + pub fn execute(&self, command: &GiteaCommand, token: &str) -> anyhow::Result<()> { + let owner = self.config.get_owner(); + let repository = self.config.get_repository(); + + match command { + GiteaCommand::Connect {} => { + self.gitea_client.connect(owner, repository)?; + self.ui.write_str_ln("connected succesfully go gitea"); + } + GiteaCommand::Tags { command } => match command { + Some(GiteaTagsCommand::MostSignificant {}) => { + let tags = self.gitea_client.get_tags(owner, repository)?; + + match get_most_significant_version(tags.iter().collect()) { + Some(tag) => { + self.ui.write_str_ln(&format!( + "found most significant tags: {}", + tag.name + )); + } + None => { + self.ui.write_str_ln("found no tags with versioning schema"); + } + } + } + None => { + let tags = self.gitea_client.get_tags(owner, repository)?; + self.ui.write_str_ln("got tags from gitea"); + for tag in tags { + self.ui.write_str_ln(&format!("- {}", tag.name)) + } + } + }, + GiteaCommand::SinceCommit { sha, branch } => { + let commits = + self.gitea_client + .get_commits_since(owner, repository, Some(sha), branch)?; + self.ui.write_str_ln("got commits from gitea"); + for commit in commits { + self.ui.write_str_ln(&format!("- {}", commit.get_title())) + } + } + GiteaCommand::CheckPr {} => { + let pr = self.gitea_client.get_pull_request(owner, repository)?; + + match pr { + Some(index) => { + self.ui.write_str_ln(&format!( + "found cuddle-please (index={}) pr from gitea", + index + )); + } + None => { + self.ui.write_str_ln("found no cuddle-please pr from gitea"); + } + } + } + } + Ok(()) + } +} diff --git a/crates/cuddle-please-commands/src/lib.rs b/crates/cuddle-please-commands/src/lib.rs index eb2ad1b..1e2c4d6 100644 --- a/crates/cuddle-please-commands/src/lib.rs +++ b/crates/cuddle-please-commands/src/lib.rs @@ -1,5 +1,6 @@ mod command; mod config_command; +mod gitea_command; mod release_command; pub use command::Command as PleaseCommand; diff --git a/crates/cuddle-please-commands/src/release_command.rs b/crates/cuddle-please-commands/src/release_command.rs index 2ad1cdd..dd06873 100644 --- a/crates/cuddle-please-commands/src/release_command.rs +++ b/crates/cuddle-please-commands/src/release_command.rs @@ -28,7 +28,7 @@ impl ReleaseCommand { } pub fn execute(&self, dry_run: bool) -> anyhow::Result<()> { - tracing::debug!("running bare command"); + tracing::debug!("running command: release"); let owner = self.config.get_owner(); let repository = self.config.get_repository(); let branch = self.config.get_branch(); diff --git a/crates/cuddle-please/src/command.rs b/crates/cuddle-please/src/command.rs deleted file mode 100644 index 8b13789..0000000 --- a/crates/cuddle-please/src/command.rs +++ /dev/null @@ -1 +0,0 @@ -