-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli):
plumb version
subcommand (#5)
* feat(cli): Implemented plumb version command * cicd: more pre-commit hooks * fix(cli): some more help messages
- Loading branch information
Showing
6 changed files
with
370 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |