Skip to content

Commit

Permalink
feat(cli): plumb version subcommand (#5)
Browse files Browse the repository at this point in the history
* feat(cli): Implemented plumb version command

* cicd: more pre-commit hooks

* fix(cli): some more help messages
  • Loading branch information
acovaci authored May 19, 2024
1 parent 907c6f5 commit d7f8ded
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ repos:
rev: v1.5.5
hooks:
- id: remove-crlf
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
- id: cargo-check
- id: clippy
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.2.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
230 changes: 230 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
pub fn hello() -> String {
"Hello, world!".to_string()
pub mod version;

pub fn command_version() -> String {
let version = version::version();
format!("plumb {}", version)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_version() {
assert!(command_version().contains("plumb"));
}
}
30 changes: 28 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
use plumb::hello;
use clap::{Parser, Subcommand};
use plumb::command_version;

#[derive(Parser, Debug)]
#[command(
name = "plumb",
about = "A project manager for your development projects."
)]
struct Args {
#[command(subcommand)]
command: Option<Command>,
}

#[derive(Debug, Subcommand)]
enum Command {
/// Prints the version of the CLI.
Version,
}

pub fn main() {
println!("{}", hello());
let cli = Args::parse();

match cli.command {
Some(Command::Version) => {
command_version();
}
_ => {
println!("Invalid arguments. Try `plumb help` for more information.");
}
}
}
85 changes: 85 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::fmt::{Display, Formatter, Result};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SemanticVersion {
major: u8,
minor: u8,
patch: u8,
}

pub fn version() -> SemanticVersion {
SemanticVersion::from_env()
}

impl SemanticVersion {
pub fn new(major: u8, minor: u8, patch: u8) -> Self {
Self {
major,
minor,
patch,
}
}

fn from_env() -> Self {
Self::from(env!("CARGO_PKG_VERSION"))
}

pub fn major(&self) -> u8 {
self.major
}

pub fn minor(&self) -> u8 {
self.minor
}

pub fn patch(&self) -> u8 {
self.patch
}
}

impl From<&str> for SemanticVersion {
fn from(version: &str) -> Self {
let parts: Vec<&str> = version.split('.').collect();
let major = parts[0].parse().unwrap();
let minor = parts[1].parse().unwrap();
let patch = parts[2].parse().unwrap();

Self {
major,
minor,
patch,
}
}
}

impl Display for SemanticVersion {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}

#[cfg(test)]
mod tests {
use std::env;

use super::*;

#[test]
fn test_version() {
assert_eq!(version().to_string(), env!("CARGO_PKG_VERSION"));
}

#[test]
fn test_from_str() {
let version = SemanticVersion::from("0.1.0");
assert_eq!(version.major(), 0);
assert_eq!(version.minor(), 1);
assert_eq!(version.patch(), 0);
}

#[test]
fn test_display() {
let version = SemanticVersion::new(0, 1, 0);
assert_eq!(version.to_string(), "0.1.0");
}
}

0 comments on commit d7f8ded

Please sign in to comment.