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;

/// Command line parser.
#[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,
  /// Project name. Defaults to target directory name
  #[structopt(short = "n", long = "name")]
  name: Option<String>,
  /// Specify if a library template should be generated
  #[structopt(short = "l", long = "lib")]
  lib: bool,
  /// Target directory
  #[structopt(default_value = ".")]
  dir: String,
}

impl Cli {
  /// Initialize a logger.
  #[inline]
  pub fn log(&self, name: &str) -> ::Result<()> {
    self
      .logger
      .log(self.verbosity.log_level(), name)
      .context(::ErrorKind::Log)?;
    Ok(())
  }

  /// Access the dir. Checks if it's a directory on disk.
  ///
  /// TODO: read the `cargo.toml` instead.
  #[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)
  }

  /// Access the directory name.
  #[inline]
  pub fn name(&self) -> ::Result<String> {
    if let Some(name) = &self.name {
      Ok(name.clone())
    } else {
      self.name_from_dir()
    }
  }

  /// Access the lib flag.
  #[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())
  }
}