cuddle-please/crates/cuddle-please-commands/src/gitea_command.rs
kjuulh af5d0f4af5
chore: fmt
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-08-01 17:12:50 +02:00

103 lines
3.3 KiB
Rust

use clap::Subcommand;
use cuddle_please_frontend::PleaseConfig;
use cuddle_please_misc::{get_most_significant_version, DynRemoteGitClient, DynUi};
#[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(())
}
}