#![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(())
}