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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use clap_flags;
use failure::ResultExt;
use std::io;
use std::path::PathBuf;
use structopt;
#[derive(Debug, StructOpt)]
#[structopt(
about = "Cross compilation template generator",
raw(setting = "structopt::clap::AppSettings::ColoredHelp")
)]
pub struct Cli {
#[structopt(flatten)]
logger: clap_flags::Log,
#[structopt(flatten)]
verbosity: clap_flags::Verbosity,
#[structopt(short = "n", long = "name")]
name: Option<String>,
#[structopt(short = "l", long = "lib")]
lib: bool,
#[structopt(default_value = ".")]
dir: String,
}
impl Cli {
#[inline]
pub fn log(&self, name: &str) -> ::Result<()> {
self
.logger
.log(self.verbosity.log_level(), name)
.context(::ErrorKind::Log)?;
Ok(())
}
#[inline]
pub fn dir(&self) -> ::Result<PathBuf> {
let path: PathBuf = self.dir.clone().into();
if !path.is_dir() {
let err = io::Error::new(io::ErrorKind::InvalidInput, "");
Err(::ErrorKind::Io(err))?;
}
let path = path.canonicalize().context(::ErrorKind::Other)?;
Ok(path)
}
#[inline]
pub fn name(&self) -> ::Result<String> {
if let Some(name) = &self.name {
Ok(name.clone())
} else {
self.name_from_dir()
}
}
#[inline]
pub fn lib(&self) -> bool {
self.lib
}
fn name_from_dir(&self) -> ::Result<String> {
let dir = self.dir().context(::ErrorKind::Other)?;
let dir = dir.iter().last().ok_or_else(|| ::ErrorKind::Other)?;
let dir = dir.to_str().ok_or_else(|| ::ErrorKind::Other)?;
Ok(dir.to_string())
}
}