diff --git a/src/bumping.rs b/src/bumping.rs index 143c2a9..ea20dda 100644 --- a/src/bumping.rs +++ b/src/bumping.rs @@ -4,6 +4,7 @@ use git2::Commit; use std::convert::TryFrom; use std::error::Error; use simple_error::SimpleError; +use std::str::FromStr; /// Extension methods for `&str`, useful for handling conventional commits pub trait FirstLine<'a> { @@ -91,6 +92,20 @@ pub trait Bump { fn bump(self, bt: &BumpType) -> Self; } +impl FromStr for BumpType { + type Err = SimpleError; + + fn from_str(s: &str) -> Result { + match s.to_ascii_lowercase().as_str() { + "none" => Ok(BumpType::None), + "patch" => Ok(BumpType::Patch), + "minor" => Ok(BumpType::Minor), + "major" => Ok(BumpType::Major), + x => Err(SimpleError::new(format!("Not a bump specification: {}", x))) + } + } +} + impl From<&str> for BumpType { fn from(commit_msg: &str) -> Self { let first_line = commit_msg.first_line(); diff --git a/src/main.rs b/src/main.rs index 89bea62..658ec02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use structopt::StructOpt; use crate::bumping::{Bump, BumpType}; use crate::changelog::ChangeLog; +use simple_error::SimpleError; mod bumping; mod repo; @@ -26,6 +27,7 @@ mod changelog; #[structopt(name = "what-bump")] struct Config { /// Analyse commits up to this one (exclusive) + #[structopt(required_unless = "bump", default_value = "")] up_to_revision: String, /// Current version of your software @@ -39,11 +41,26 @@ struct Config { /// Also generate a changelog, and write it to this file #[structopt(long, short)] changelog: Option, + + /// Perform the specified version bump (you must also specify `--from`) + #[structopt(long, short)] + bump: Option, } fn main() -> Result<(), Box> { let config: Config = Config::from_args(); + match (config.bump, &config.from) { + (Some(ref bump_type), Some(ref version)) => { + println!("{}", version.clone().bump(bump_type)); + return Ok(()); + }, + (Some(_), None) => { + return Err(Box::new(SimpleError::new("If you specify `--bump`, you must also specify `--from`, otherwise I don't know what version to bump."))); + } + _ => (), + } + let max_bump_type = config.path.commits_up_to(&config.up_to_revision)? .map(|commit| Ok(BumpType::from(commit.message().unwrap_or("")))) .max()?