feat: init commit
This commit is contained in:
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "commit-msg"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
|
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"
|
||||||
|
|
||||||
13
src/cmd.rs
Normal file
13
src/cmd.rs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
19
src/cmd_default.rs
Normal file
19
src/cmd_default.rs
Normal file
@@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/cmd_usage.rs
Normal file
17
src/cmd_usage.rs
Normal file
@@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/main.rs
Normal file
30
src/main.rs
Normal file
@@ -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<Box<dyn Command>> = 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user