New clap arg to output the given semver to stdout

master
Wynd 2024-11-17 14:49:15 +02:00
parent 7f33e4a048
commit 79a1b5af30
3 changed files with 29 additions and 8 deletions

2
Cargo.lock generated
View File

@ -53,7 +53,7 @@ dependencies = [
[[package]] [[package]]
name = "cargo-semver" name = "cargo-semver"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"clap", "clap",
"semver", "semver",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cargo-semver" name = "cargo-semver"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
keywords = ["cargo", "semver"] keywords = ["cargo", "semver"]
categories = [ categories = [

View File

@ -1,6 +1,6 @@
use std::fs; use std::{default, fs};
use clap::{command, Parser}; use clap::{command, Parser, ValueEnum};
use semver::Version; use semver::Version;
use toml_edit::{value, DocumentMut}; use toml_edit::{value, DocumentMut};
@ -11,6 +11,13 @@ enum CargoCli {
Semver(SemverArgs), Semver(SemverArgs),
} }
#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
pub enum VerType {
Major,
Minor,
Patch,
}
#[derive(Clone, Debug, Parser, PartialEq, Eq)] #[derive(Clone, Debug, Parser, PartialEq, Eq)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct SemverArgs { pub struct SemverArgs {
@ -22,15 +29,14 @@ pub struct SemverArgs {
#[arg(long("bump-major"), default_value_t = false)] #[arg(long("bump-major"), default_value_t = false)]
pub bump_major: bool, pub bump_major: bool,
#[arg(long("get"), value_enum)]
pub get: Option<VerType>,
} }
fn main() { fn main() {
let CargoCli::Semver(args) = CargoCli::parse(); let CargoCli::Semver(args) = CargoCli::parse();
if !args.bump_major && !args.bump_minor && !args.bump_patch {
panic!("Specify which version to bump by using --bump-patch --bump-minor or --bump-major");
}
let toml = fs::read_to_string("./Cargo.toml").unwrap(); let toml = fs::read_to_string("./Cargo.toml").unwrap();
let mut doc = toml.parse::<DocumentMut>().expect("invalid doc"); let mut doc = toml.parse::<DocumentMut>().expect("invalid doc");
@ -38,6 +44,21 @@ fn main() {
let semver = semver.trim(); let semver = semver.trim();
let mut semver = Version::parse(semver).expect("invalid semver"); let mut semver = Version::parse(semver).expect("invalid semver");
if let Some(ver_type) = args.get {
let ver = match ver_type {
VerType::Major => semver.major,
VerType::Minor => semver.minor,
VerType::Patch => semver.patch,
};
print!("{ver}");
std::process::exit(0);
}
if !args.bump_major && !args.bump_minor && !args.bump_patch {
panic!("Specify which version to bump by using --bump-patch --bump-minor or --bump-major");
}
if args.bump_patch { if args.bump_patch {
semver.patch += 1; semver.patch += 1;
} }