diff --git a/src/Carto.toml_template b/src/Carto.toml_template new file mode 100644 index 0000000..0d11f61 --- /dev/null +++ b/src/Carto.toml_template @@ -0,0 +1,10 @@ +[package] +name = "{{NAME}}" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "2.33.1" diff --git a/src/cmd_default.rs b/src/cmd_default.rs index fd5c96f..abf98a7 100644 --- a/src/cmd_default.rs +++ b/src/cmd_default.rs @@ -16,7 +16,8 @@ impl CommandDefault { pub fn run(arg_matches: &ArgMatches) -> CommandError { let verbose_count = arg_matches.occurrences_of("verbose"); - println!("Verbose count: {}", verbose_count); + println!("[INFO] Verbose count: {}", verbose_count); + println!("[INFO] This is default command cli ..."); // TODO ... Ok(()) } diff --git a/src/cmd_newcliapp.rs b/src/cmd_newcliapp.rs new file mode 100644 index 0000000..0efc8f5 --- /dev/null +++ b/src/cmd_newcliapp.rs @@ -0,0 +1,49 @@ +use std::fs::{ self, File, }; +use clap::{ Arg, ArgMatches, SubCommand, App, }; +use crate::cmd::{ Command, CommandError, }; + +pub struct CommandNewCliApp; + +const CARGO_TOML_FILE: &str = "Carto.toml"; + +const MAIN_RS: &str = include_str!("main.rs_template"); +const CMD_RS: &str = include_str!("cmd.rs"); +const CMD_DEFAULT_RS: &str = include_str!("cmd_default.rs"); +const CMD_SAMPLE_RS: &str = include_str!("cmd_sample.rs"); +const CARGO_TOML: &str = include_str!("Carto.toml_template"); + +impl Command for CommandNewCliApp { + + fn subcommand<'a>(&self) -> App<'a, 'a> { + SubCommand::with_name(self.name()).about("New cli app") + .arg(Arg::with_name("APP_NAME").required(true).index(1).help("App name")) + .arg(Arg::with_name("verbose").long("verbose").short("V").help("Verbose")) + } + + fn name(&self) -> &str { + "newcliapp" + } + + fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { + if is_file_exists(CARGO_TOML_FILE) || is_file_exists("src") { + println!("[ERROR] File exists: {}, or dir exists: src/", CARGO_TOML_FILE); + return Ok(()); + } + + let app_name = sub_arg_matches.value_of("APP_NAME").expect("APP_NAME is required!"); + let cargo_toml = CARGO_TOML.replace("{{NAME}}", app_name); + + fs::create_dir("src/")?; + fs::write("src/main.rs", MAIN_RS.as_bytes())?; + fs::write("src/cmd.rs", CMD_RS.as_bytes())?; + fs::write("src/cmd_default.rs", CMD_DEFAULT_RS.as_bytes())?; + fs::write("src/cmd_sample.rs", CMD_SAMPLE_RS.as_bytes())?; + fs::write(CARGO_TOML_FILE, cargo_toml.as_bytes())?; + + Ok(()) + } +} + +fn is_file_exists(file_name: &str) -> bool { + File::open(file_name).is_ok() +} diff --git a/src/main.rs b/src/main.rs index ec50fe9..d8b09df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,17 @@ use clap::App; mod cmd; mod cmd_sample; mod cmd_default; +mod cmd_newcliapp; use cmd::{ Command, CommandError, }; use cmd_sample::CommandSample; use cmd_default::CommandDefault; +use cmd_newcliapp::CommandNewCliApp; fn main() -> CommandError { - let commands = vec![ - CommandSample{}, + let commands: Vec> = vec![ + Box::new(CommandSample{}), + Box::new(CommandNewCliApp{}), ]; let mut app = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION")) diff --git a/src/main.rs_template b/src/main.rs_template new file mode 100644 index 0000000..294e817 --- /dev/null +++ b/src/main.rs_template @@ -0,0 +1,31 @@ +use clap::App; + +mod cmd; +mod cmd_sample; +mod cmd_default; + +use cmd::{ Command, CommandError, }; +use cmd_sample::CommandSample; +use cmd_default::CommandDefault; + +fn main() -> CommandError { + let commands = vec![ + CommandSample{}, + ]; + let mut app = App::new(env!("CARGO_PKG_NAME")) + .version(env!("CARGO_PKG_VERSION")) + .about(env!("CARGO_PKG_DESCRIPTION")); + app = CommandDefault::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); + } + } + + CommandDefault::run(&matches) +}