diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2df47ea --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "commit-msg" +version = "0.0.1" +authors = ["Hatter Jiang "] +edition = "2018" +license = "MIT" +description = "Git commit message format check" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "commit-msg" +path = "src/main.rs" + +[dependencies] +clap = "2.33" +toml = "0.5" +rust_util = "0.6" + diff --git a/src/cmd.rs b/src/cmd.rs new file mode 100644 index 0000000..6ec2e12 --- /dev/null +++ b/src/cmd.rs @@ -0,0 +1,13 @@ +use clap::{ArgMatches, App}; +use rust_util::XResult; + +pub type CommandError = XResult<()>; + +pub trait Command { + + fn name(&self) -> &str; + + fn subcommand<'a>(&self) -> App<'a, 'a>; + + fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError; +} diff --git a/src/cmd_default.rs b/src/cmd_default.rs new file mode 100644 index 0000000..902e81f --- /dev/null +++ b/src/cmd_default.rs @@ -0,0 +1,19 @@ +use clap::{App, Arg, ArgMatches}; +use crate::cmd::CommandError; + +pub struct CommandImpl; + +impl CommandImpl { + + pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> { + app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info")) + } + + pub fn run(arg_matches: &ArgMatches) -> CommandError { + let verbose_count = arg_matches.occurrences_of("verbose"); + information!("Verbose count: {}", verbose_count); + information!(r#"Print using command line: +commit-msg usage"#); + Ok(()) + } +} \ No newline at end of file diff --git a/src/cmd_usage.rs b/src/cmd_usage.rs new file mode 100644 index 0000000..2f1a483 --- /dev/null +++ b/src/cmd_usage.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 { "usage" } + + fn subcommand<'a>(&self) -> App<'a, 'a> { + SubCommand::with_name(self.name()).about("Sample subcommand") + } + + fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { + Ok(()) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..51aed83 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,30 @@ +#[macro_use] extern crate rust_util; + +mod cmd; +mod cmd_default; +mod cmd_usage; + +use clap::App; +use cmd::{Command, CommandError}; + +fn main() -> CommandError { + let commands: Vec> = vec![ + Box::new(cmd_usage::CommandImpl) + ]; + let mut app = App::new(env!("CARGO_PKG_NAME")) + .version(env!("CARGO_PKG_VERSION")) + .about(env!("CARGO_PKG_DESCRIPTION")); + app = cmd_default::CommandImpl::process_command(app); + for command in &commands { + app = app.subcommand(command.subcommand()); + } + + let matches = app.get_matches(); + for command in &commands { + if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) { + return command.run(&matches, sub_cmd_matches); + } + } + + cmd_default::CommandImpl::run(&matches) +}