1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use clap::Parser;
use rustutils_runnable::Runnable;
use std::error::Error;
use std::fs::create_dir;
use std::io;
use std::path::{Path, PathBuf};
#[derive(Parser, Clone, Debug)]
#[clap(author, version, about)]
pub struct Mkdir {
#[clap(long, short)]
pub parents: bool,
#[clap(long, short)]
pub verbose: bool,
#[clap(required = true)]
pub directory: Vec<PathBuf>,
}
impl Mkdir {
pub fn run(&self) -> Result<(), io::Error> {
for directory in &self.directory {
if self.parents {
self.create_parents(directory)?
} else {
self.create_directory(directory)?
}
}
Ok(())
}
pub fn create_parents(&self, path: &Path) -> Result<(), io::Error> {
if let Some(parent) = path.parent() {
self.create_parents(parent)?;
}
self.create_directory(path)?;
Ok(())
}
pub fn create_directory(&self, dir: &Path) -> Result<(), io::Error> {
if self.verbose {
eprintln!("Mkdir: Creating directory {dir:?}");
}
create_dir(dir)?;
Ok(())
}
}
impl Runnable for Mkdir {
fn run(&self) -> Result<(), Box<dyn Error>> {
self.run().map_err(|e| Box::new(e) as Box<dyn Error>)
}
}