refactor: move gitea command into its own file

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-01 17:11:59 +02:00
parent ae9073bf0b
commit b13e3916f6
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>,