#![crate_type = "bin"]
#![crate_name = "mkdir"]
static UTIL: Prog = Prog { name: "mkdir", vers: "0.1.0", yr: "2015" };
extern crate pgetopts;
extern crate rpf;
use pgetopts::{Options};
use rpf::{Status,Styled,Prog};
use std::fs;
use std::env;
use std::path::{PathBuf};
fn mk_dir(dir: Vec<String>, verbose: bool) {
for item in dir {
match fs::create_dir(PathBuf::from(&item)) {
Ok(_) => {
if verbose {
println!("{}: created directory '{}'", UTIL.name, item);
}
},
Err(e) => {
Status::Error.err(UTIL.name, e.to_string());
}
};
}
}
fn mk_dir_p(dir: Vec<String>, verbose: bool) {
for item in dir {
match fs::create_dir_all(PathBuf::from(&item)) {
Ok(_) => {
if verbose {
println!("{}: created directory '{}'", UTIL.name, item);
}
},
Err(e) => {
Status::Error.err(UTIL.name, e.to_string());
}
};
}
}
fn print_usage(opts: Options) {
print!("{}: {} {}... {}...\n\
Create DIRECTORY(ies) if they do not already exist.", "Usage".bold(),
UTIL.name.bold(), "[OPTION]".underline(), "DIRECTORY".underline());
println!("{}", opts.options());
}
fn main() {
let args: Vec<String> = env::args().collect();
let mut opts = Options::new();
opts.optflag("v", "verbose", "Print the name of each created directory");
opts.optflag("p", "parents", "No error if existing, make parent directories as needed");
opts.optflag("h", "help", "Print help information");
opts.optflag("", "version", "Print version information");
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => {
Status::OptError.err(UTIL.name, f.to_string());
panic!(f);
}
};
let verb = matches.opt_present("v");
if matches.opt_present("p") {
mk_dir_p(matches.free, verb);
} else if matches.opt_present("h") {
print_usage(opts);
} else if matches.opt_present("version") {
UTIL.copyright("Copyright (C) 2015 core-utils developers\n\
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
This is free software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n\n",
vec!["Alberto Corona"]);
} else if matches.free.is_empty() {
UTIL.prog_try();
} else {
mk_dir(matches.free, verb);
}
}