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
77
78
79
80
#[macro_use]
extern crate log;
pub mod commands;
pub mod error;
pub mod types;
use clap::Parser;
use colored::Colorize;
use commands::*;
use crossbundle_tools::types::{Config, Shell, Verbosity};
use std::path::PathBuf;
#[derive(Parser, Clone, Debug)]
#[clap(author, about, version)]
pub struct Opts {
#[clap(short, long)]
pub current_dir: Option<PathBuf>,
#[clap(short, long, parse(from_occurrences))]
pub verbose: u32,
#[clap(short, long)]
pub quiet: bool,
#[clap(subcommand)]
pub cmd: Commands,
}
impl Opts {
pub fn get_verbosity(&self) -> Verbosity {
if self.quiet {
Verbosity::Quiet
} else {
match self.verbose {
0 => Verbosity::Normal,
1 => Verbosity::Verbose,
_ => {
pretty_env_logger::formatted_builder()
.filter_level(log::LevelFilter::Trace)
.init();
Verbosity::Verbose
}
}
}
}
pub fn get_current_dir(&self) -> PathBuf {
self.current_dir
.clone()
.unwrap_or_else(|| std::env::current_dir().unwrap())
}
}
pub fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
let opts = Opts::parse();
let mut shell = Shell::new();
shell.set_verbosity(opts.get_verbosity());
let config = Config::new(shell, opts.get_current_dir());
opts.cmd.handle_command(&config)?;
Ok(())
}
pub fn handle_errors(run: impl FnOnce() -> std::result::Result<(), Box<dyn std::error::Error>>) {
if let Err(error) = run() {
eprintln!("{}: {}", "error".red().bold(), error);
handle_error_source(error.source());
std::process::exit(1);
};
}
fn handle_error_source(source: Option<&(dyn std::error::Error + 'static)>) {
if let Some(error) = source {
eprintln!("{}: {}", "caused by".red().bold(), error);
handle_error_source(error.source());
}
}