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
81
82
83
84
85
86
87
88
89
90
91
92
#![allow(clippy::redundant_closure)]
mod format;
mod format_stdin;
mod init;
use self::format::format_cmd;
use self::format_stdin::format_stdin_cmd;
use self::init::init_cmd;
use crate::expand_path;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct Cli {
#[structopt(long = "init")]
pub init: bool,
#[structopt(long = "stdin", conflicts_with("init"))]
pub stdin: bool,
#[structopt(long = "clear-cache", conflicts_with("stdin"), conflicts_with("init"))]
pub clear_cache: bool,
#[structopt(
long = "fail-on-change",
conflicts_with("stdin"),
conflicts_with("init")
)]
pub fail_on_change: bool,
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
pub verbosity: u8,
#[structopt(long = "quiet", short = "q")]
pub quiet: bool,
#[structopt(short = "C", default_value = ".")]
pub work_dir: PathBuf,
#[structopt(long = "tree-root")]
pub tree_root: Option<PathBuf>,
#[structopt()]
pub paths: Vec<PathBuf>,
}
pub fn cli_from_args() -> anyhow::Result<Cli> {
let mut cli = Cli::from_args();
let cwd = std::env::current_dir()?;
assert!(cwd.is_absolute());
cli.work_dir = expand_path(&cli.work_dir, &cwd);
if let Some(tree_root) = cli.tree_root {
cli.tree_root = Some(expand_path(&tree_root, &cwd));
}
Ok(cli)
}
pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
if cli.init {
init_cmd(&cli.work_dir)?
} else if cli.stdin {
format_stdin_cmd(&cli.tree_root, &cli.work_dir, &cli.paths)?
} else {
format_cmd(
&cli.tree_root,
&cli.work_dir,
&cli.paths,
cli.clear_cache,
cli.fail_on_change,
)?
}
Ok(())
}