Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 Update cli interface #7

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 74 additions & 24 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use clap::{Args, Parser, Subcommand};
use clap::{Args, Parser, Subcommand, ValueEnum};

use crate::versioning::Increment;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
#[clap(version_short = 'v')]
pub struct Cli {
#[clap(short = 'v', long = "version", action)]
pub version: bool,

#[clap(subcommand)]
pub command: Commands,
pub command: Option<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
#[clap(
about = "Calculates the next version number based on the latest matching tag. (alias: nv)",
about = "Calculates the next version number based on the latest matching tag or branch. (alias: nv)",
alias = "nv"
)]
NextVersion(NextVersionArgs),
#[clap(
about = "Finds the latest version tag in the repository matching a given pattern. (alias: lv)",
about = "Finds the latest version tag or branch in the repository matching a given pattern. (alias: lv)",
alias = "lv"
)]
LastVersion(LastVersionArgs),
Expand All @@ -30,41 +31,90 @@ pub struct NextVersionArgs {
#[clap(
help = "Specify the version part to increment: major, minor, or patch",
long,
arg_enum,
value_enum,
default_value = "patch",
short = 'i'
)]
pub increment: Increment,
#[clap(help = "Get next version based on a given pattern", long, short = 'p')]
#[clap(
help = "Specify a custom pattern for version matching and generation. \
Use {major}, {minor}, and {patch} as placeholders. \
Example: 'v{major}.{minor}.{patch}' or 'release-{major}.{minor}.{patch}'",
long,
short = 'p'
)]
pub pattern: Option<String>,
#[clap(
help = "Tag current commit as the next version pattern",
help = "Enable verbose output for detailed information",
long,
short = 't',
short = 'v',
action
)]
pub tag: bool,
#[clap(help = "Verbose output", long, short = 'V', action)]
pub verbose: bool,
#[clap(
help = "Create new branch as the next version pattern",
help = "Specify the source for versioning: tag (default) or branch",
long,
short = 'b',
action
short = 's',
value_enum,
default_value = "tag"
)]
pub source: VersionSourceName,
#[clap(
help = "Action to perform: print (default) or create (creates a new tag or branch)",
long,
value_enum,
default_value = "print",
short = 'a'
)]
pub branch: bool,
pub action: NextVersionAction,
}

#[derive(Args, Debug)]
pub struct LastVersionArgs {
#[clap(help = "Get last version based on a given pattern", long, short = 'p')]
#[clap(
help = "Get last version based on a given pattern (e.g., 'v{major}.{minor}.{patch}')",
long,
short = 'p'
)]
pub pattern: Option<String>,
#[clap(help = "Check out to the last version", long, short = 'c', action)]
pub checkout: bool,
#[clap(help = "Verbose output", long, short = 'V', action)]
#[clap(
help = "Enable verbose output for detailed information",
long,
short = 'v',
action
)]
pub verbose: bool,
#[clap(help = "Get last version based on tag", long, short = 't', action)]
pub tag: bool,
#[clap(help = "Get last version based on branch", long, short = 'b', action)]
pub branch: bool,
#[clap(
help = "Specify the source for versioning: tag (default) or branch",
long,
value_enum,
default_value = "tag",
short = 's'
)]
pub source: VersionSourceName,
#[clap(
help = "Action to perform: print (default) or checkout (checks out the last version)",
long,
value_enum,
default_value = "print"
)]
pub action: LastVersionAction,
}

#[derive(Debug, Clone, ValueEnum)]
pub enum VersionSourceName {
Tag,
Branch,
}

#[derive(Debug, Clone, ValueEnum)]
pub enum NextVersionAction {
Print,
Create,
}

#[derive(Debug, Clone, ValueEnum)]
pub enum LastVersionAction {
Print,
Checkout,
}
8 changes: 2 additions & 6 deletions src/gitutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ use std::path::Path;

use git2::{Branch, DescribeFormatOptions, DescribeOptions, Repository};

use crate::utils::print_verbose;

#[derive(Default, Clone)]
pub struct CommandOptions {
pub verbose: bool,
}

fn print_verbose(message: &str, verbose: bool) {
if verbose {
println!("{}", message);
}
}

pub fn get_repo(path: &Path) -> Repository {
let repo = Repository::open(path).expect("Repository not found");
repo
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ mod testutils;
pub mod cli;
pub mod gitutils;
pub mod service;
pub mod utils;
pub mod version_source;
pub mod versioning;
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ fn main() {
let cli = Cli::parse();
let path = Path::new(".");
match &cli.command {
Commands::LastVersion(args) => {
Some(Commands::LastVersion(args)) => {
last_version(path, args);
}
Commands::NextVersion(args) => {
Some(Commands::NextVersion(args)) => {
next_version(path, args);
}
None => {}
}

if cli.version {
println!("{}", env!("CARGO_PKG_VERSION"));
}
}
Loading