refactor: move gitea command into its own file

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-01 17:11:59 +02:00
parent ae9073bf0b
commit b13e3916f6
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
6 changed files with 117 additions and 105 deletions

View File

@ -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<GiteaTagsCommand>,
},
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<PathBuf>,

View File

@ -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,

View File

@ -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<GiteaTagsCommand>,
},
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(())
}
}

View File

@ -1,5 +1,6 @@
mod command;
mod config_command;
mod gitea_command;
mod release_command;
pub use command::Command as PleaseCommand;

View File

@ -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();

View File

@ -1 +0,0 @@