From fdb271ce8050d81f3bc27c2d98f594ad240c5535 Mon Sep 17 00:00:00 2001 From: Adrian Covaci <6562353+acovaci@users.noreply.github.com> Date: Sun, 19 May 2024 14:20:52 +0100 Subject: [PATCH 1/3] feat(cli): Implemented plumb version command --- Cargo.lock | 230 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 17 +++- src/main.rs | 26 +++++- src/version.rs | 85 ++++++++++++++++++ 5 files changed, 355 insertions(+), 4 deletions(-) create mode 100644 src/version.rs diff --git a/Cargo.lock b/Cargo.lock index 1baeede..27042ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,236 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "plumb" version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/Cargo.toml b/Cargo.toml index 00ae9b4..0d6de94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/lib.rs b/src/lib.rs index de217c6..b84fb8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")); + } } diff --git a/src/main.rs b/src/main.rs index 2ac6f21..93df9e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,27 @@ -use plumb::hello; +use clap::{Parser, Subcommand}; +use plumb::command_version; + +#[derive(Parser, Debug)] +#[command(name = "plumb", version)] +struct Args { + #[command(subcommand)] + command: Option, +} + +#[derive(Debug, Subcommand)] +enum Command { + 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."); + } + } } diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 0000000..d5b3e9a --- /dev/null +++ b/src/version.rs @@ -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"); + } +} From 67df7022276be5ebc21fd6aa0bf93a7a6e2701a9 Mon Sep 17 00:00:00 2001 From: Adrian Covaci <6562353+acovaci@users.noreply.github.com> Date: Sun, 19 May 2024 14:22:14 +0100 Subject: [PATCH 2/3] cicd: more pre-commit hooks --- .pre-commit-config.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8bd7f69..9076aaf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] From 31c4eb0bef19b719e5a1569adf5fe79348aa387c Mon Sep 17 00:00:00 2001 From: Adrian Covaci <6562353+acovaci@users.noreply.github.com> Date: Sun, 19 May 2024 14:28:35 +0100 Subject: [PATCH 3/3] fix(cli): some more help messages --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 93df9e3..7016b5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,10 @@ use clap::{Parser, Subcommand}; use plumb::command_version; #[derive(Parser, Debug)] -#[command(name = "plumb", version)] +#[command( + name = "plumb", + about = "A project manager for your development projects." +)] struct Args { #[command(subcommand)] command: Option, @@ -10,6 +13,7 @@ struct Args { #[derive(Debug, Subcommand)] enum Command { + /// Prints the version of the CLI. Version, }