From 62cafa4a9edd32a0f791e0ecbf422b461d9a85c1 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 30 Jul 2023 14:09:51 +0200 Subject: [PATCH] feat: with tags command Signed-off-by: kjuulh --- crates/cuddle-please/src/command.rs | 43 +++++++++++++++---- crates/cuddle-please/src/versioning/semver.rs | 4 +- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/crates/cuddle-please/src/command.rs b/crates/cuddle-please/src/command.rs index ca34418..c38b693 100644 --- a/crates/cuddle-please/src/command.rs +++ b/crates/cuddle-please/src/command.rs @@ -14,6 +14,7 @@ use crate::{ git_client::VcsClient, gitea_client::GiteaClient, ui::{ConsoleUi, DynUi}, + versioning::semver::get_most_significant_version, }; #[derive(Parser)] @@ -180,14 +181,32 @@ impl Command { client.connect(self.global.owner.unwrap(), self.global.repo.unwrap())?; self.ui.write_str_ln("connected succesfully go gitea"); } - GiteaCommand::Tags {} => { - let tags = client - .get_tags(self.global.owner.unwrap(), self.global.repo.unwrap())?; - self.ui.write_str_ln("got tags from gitea"); - for tag in tags { - self.ui.write_str_ln(&format!("- {}", tag.name)) + GiteaCommand::Tags { command } => match command { + Some(GiteaTagsCommand::MostSignificant {}) => { + let tags = client + .get_tags(self.global.owner.unwrap(), self.global.repo.unwrap())?; + + 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(self.global.owner.unwrap(), self.global.repo.unwrap())?; + 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( self.global.owner.unwrap(), @@ -269,7 +288,10 @@ enum ConfigCommand { #[derive(Subcommand, Debug, Clone)] enum GiteaCommand { Connect {}, - Tags {}, + Tags { + #[command(subcommand)] + command: Option, + }, SinceCommit { #[arg(long)] sha: String, @@ -279,6 +301,11 @@ enum GiteaCommand { }, } +#[derive(Subcommand, Debug, Clone)] +enum GiteaTagsCommand { + MostSignificant {}, +} + fn get_current_path( optional_current_dir: Option<&Path>, optional_source_path: Option, diff --git a/crates/cuddle-please/src/versioning/semver.rs b/crates/cuddle-please/src/versioning/semver.rs index 7d5cac8..8003a7d 100644 --- a/crates/cuddle-please/src/versioning/semver.rs +++ b/crates/cuddle-please/src/versioning/semver.rs @@ -3,8 +3,8 @@ use std::cmp::Reverse; use crate::gitea_client::{Commit, Tag}; use semver::Version; -pub fn get_most_significant_version<'a>(commits: Vec<&'a Tag>) -> Option<&'a Tag> { - let mut versions: Vec<(&'a Tag, Version)> = commits +pub fn get_most_significant_version<'a>(tags: Vec<&'a Tag>) -> Option<&'a Tag> { + let mut versions: Vec<(&'a Tag, Version)> = tags .into_iter() .filter_map(|c| { if let Some(version) = c.name.trim_start_matches("v").parse::().ok() {