add verbose in newcliapp

This commit is contained in:
2020-06-14 00:22:05 +08:00
parent 9b050fd13b
commit 778bc279e4
4 changed files with 24 additions and 2 deletions

View File

@@ -25,6 +25,8 @@ impl Command for CommandNewCliApp {
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let is_verbose = sub_arg_matches.is_present("verbose");
if is_file_exists(CARGO_TOML_FILE) || is_file_exists("src") {
println!("[ERROR] File exists: {}, or dir exists: src/", CARGO_TOML_FILE);
return Ok(());
@@ -33,13 +35,20 @@ impl Command for CommandNewCliApp {
let app_name = sub_arg_matches.value_of("APP_NAME").expect("APP_NAME is required!");
let cargo_toml = CARGO_TOML.replace("{{NAME}}", app_name);
if is_verbose { println!("[INFO] Create dir: src/"); }
fs::create_dir("src/")?;
if is_verbose { println!("[INFO] Create file: src/main.rs"); }
fs::write("src/main.rs", MAIN_RS.as_bytes())?;
if is_verbose { println!("[INFO] Create file: src/cmd.rs"); }
fs::write("src/cmd.rs", CMD_RS.as_bytes())?;
if is_verbose { println!("[INFO] Create file: src/cmd_default.rs"); }
fs::write("src/cmd_default.rs", CMD_DEFAULT_RS.as_bytes())?;
if is_verbose { println!("[INFO] Create file: src/cmd_sample.rs"); }
fs::write("src/cmd_sample.rs", CMD_SAMPLE_RS.as_bytes())?;
if is_verbose { println!("[INFO] Create file: Cargo.toml"); }
fs::write(CARGO_TOML_FILE, cargo_toml.as_bytes())?;
println!("[INFO] Project created!");
Ok(())
}
}