Skip to content

Commit

Permalink
datashed: add --bump option (version) (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Wagner <[email protected]>
  • Loading branch information
nwagner84 authored Jul 15, 2024
1 parent 3224530 commit fd5de88
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions crates/datashed/src/commands/version.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
use clap::Parser;
use clap::{Parser, ValueEnum};
use semver::Version as SemVer;

use crate::prelude::*;

#[derive(Debug, Clone, ValueEnum)]
enum Bump {
Major,
Minor,
Patch,
}

/// Get or set the version of the datashed.
#[derive(Debug, Parser)]
pub(crate) struct Version {
/// Whether to overwrite the current version or not.
#[arg(short, long)]
force: bool,

/// Run verbosely. Print additional progress information to the
/// standard error stream. This option conflicts with the
/// `--quiet` option.
#[arg(short, long, conflicts_with = "quiet")]
verbose: bool,

/// Operate quietly; do not show progress. This option conflicts
/// with the `--verbose` option.
#[arg(short, long, conflicts_with = "verbose")]
quiet: bool,

#[arg(short, long, conflicts_with = "version")]
bump: Option<Bump>,

/// The new version of the datashed. Unless the `--force`/`-f`
/// option is set, the new version must be greater than the
/// current version. A datashed version consists of three
/// separated integers, which must conform to the semantic
/// versioning standard; invalid version strings are rejected.
version: Option<semver::Version>,
#[arg(conflicts_with = "bump")]
version: Option<SemVer>,
}

impl Version {
Expand All @@ -28,6 +51,23 @@ impl Version {
bail!("{version} must be greater than {current}");
}

config.metadata.version = version;
config.save()?;
} else if let Some(bump) = self.bump {
let major = config.metadata.version.major;
let minor = config.metadata.version.minor;
let patch = config.metadata.version.patch;

let version = match bump {
Bump::Patch => SemVer::new(major, minor, patch + 1),
Bump::Minor => SemVer::new(major, minor + 1, 0),
Bump::Major => SemVer::new(major + 1, 0, 0),
};

if self.verbose {
println!("bumped version to {version}");
}

config.metadata.version = version;
config.save()?;
} else {
Expand Down

0 comments on commit fd5de88

Please sign in to comment.