diff --git a/Cargo.toml b/Cargo.toml index 2df47ea..dd0451b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,6 @@ path = "src/main.rs" clap = "2.33" toml = "0.5" rust_util = "0.6" +regex = "1.4" + diff --git a/src/cmd_default.rs b/src/cmd_default.rs index 902e81f..badfc73 100644 --- a/src/cmd_default.rs +++ b/src/cmd_default.rs @@ -1,5 +1,6 @@ use clap::{App, Arg, ArgMatches}; use crate::cmd::CommandError; +use crate::template_regex::CommitMsgCheckRegex; pub struct CommandImpl; @@ -14,6 +15,7 @@ impl CommandImpl { information!("Verbose count: {}", verbose_count); information!(r#"Print using command line: commit-msg usage"#); + CommitMsgCheckRegex::new_default(); Ok(()) } } \ No newline at end of file diff --git a/src/cmd_install.rs b/src/cmd_install.rs new file mode 100644 index 0000000..44c3641 --- /dev/null +++ b/src/cmd_install.rs @@ -0,0 +1,17 @@ +use clap::{ArgMatches, SubCommand, App}; +use crate::cmd::{Command, CommandError}; + +pub struct CommandImpl; + +impl Command for CommandImpl { + + fn name(&self) -> &str { "install" } + + fn subcommand<'a>(&self) -> App<'a, 'a> { + SubCommand::with_name(self.name()).about("Install subcommand") + } + + fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { + Ok(()) + } +} \ No newline at end of file diff --git a/src/cmd_usage.rs b/src/cmd_usage.rs index 2f1a483..de0fb46 100644 --- a/src/cmd_usage.rs +++ b/src/cmd_usage.rs @@ -8,7 +8,7 @@ impl Command for CommandImpl { fn name(&self) -> &str { "usage" } fn subcommand<'a>(&self) -> App<'a, 'a> { - SubCommand::with_name(self.name()).about("Sample subcommand") + SubCommand::with_name(self.name()).about("Usage subcommand") } fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { diff --git a/src/main.rs b/src/main.rs index 51aed83..421cd29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,17 @@ mod cmd; mod cmd_default; mod cmd_usage; +mod cmd_install; +mod template; +mod template_regex; use clap::App; use cmd::{Command, CommandError}; fn main() -> CommandError { let commands: Vec> = vec![ - Box::new(cmd_usage::CommandImpl) + Box::new(cmd_usage::CommandImpl), + Box::new(cmd_install::CommandImpl), ]; let mut app = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION")) diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..0c320bc --- /dev/null +++ b/src/template.rs @@ -0,0 +1,6 @@ + +pub trait CommitMsgCheck { + fn check(&self, msg: &str) -> bool; + + fn print_hint(&self); +} diff --git a/src/template_regex.rs b/src/template_regex.rs new file mode 100644 index 0000000..b1bc3b0 --- /dev/null +++ b/src/template_regex.rs @@ -0,0 +1,50 @@ +use regex::Regex; +use rust_util::util_term::{BOLD, UNDER, END}; +use crate::template::CommitMsgCheck; + +const DEFAULT_COMMIT_MSG_REGEXP: &str = "^(feat|fix|docs|style|refactor|test|chore)(\\([\\w\\-_.]+\\))?:\\s*.*"; + +pub struct CommitMsgCheckRegex { + regex: Regex, + hint: String, +} + +impl CommitMsgCheck for CommitMsgCheckRegex { + fn check(&self, msg: &str) -> bool { + self.regex.is_match(msg) + } + + fn print_hint(&self) { + println!("[USAGE] {}", self.hint); + } +} + +impl CommitMsgCheckRegex { + pub fn new_default() -> Self { + Self { + regex: Regex::new(DEFAULT_COMMIT_MSG_REGEXP).unwrap(), + hint: format!(r#"Please follow the commit spec: +Format: {b}{e}(){b}: {e} + is optional + +{b}feat{e}: add hat wobble +^--^ ^------------^ +| | +| +-> Summary in present tense. +| ++-------> Type: chore, docs, feat, fix, refactor, style, or test. + +{b}feat{e} : new feature for the user, not a new feature for build script +{b}fix{e} : bug fix for the user, not a fix to a build script +{b}docs{e} : changes to the documentation +{b}style{e} : formatting, missing semi colons, etc; no production code change +{b}refactor{e} : refactoring production code, eg. renaming a variable +{b}test{e} : adding missing tests, refactoring tests; no production code change +{b}chore{e} : updating grunt tasks etc; no production code change + +Reference: {u}https://gist.github.com/joshbuchea/6f47e86d2510bce28f8e7f42ae84c716{e} +or mirror: {u}https://hatter.ink/wiki/view_raw.action?__access_token=PUBLIC&id=42{e} +"#, b = BOLD, e = END, u = UNDER), + } + } +}