add newcliapp
This commit is contained in:
49
src/cmd_newcliapp.rs
Normal file
49
src/cmd_newcliapp.rs
Normal file
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user