#![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::config;
use crate::expand_path;
use anyhow::anyhow;
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use log::warn;
use std::{
env,
path::{Path, PathBuf},
};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[arg(short, long, default_value_t = false)]
pub init: bool,
#[arg(long, default_value_t = false, conflicts_with("init"))]
pub stdin: bool,
#[arg(long, conflicts_with("stdin"), conflicts_with("init"))]
pub no_cache: bool,
#[arg(short, long, default_value_t = false)]
pub clear_cache: bool,
#[arg(
long,
default_value_t = false,
conflicts_with("stdin"),
conflicts_with("init")
)]
pub fail_on_change: bool,
#[arg(long, default_value_t = false)]
pub allow_missing_formatter: bool,
#[clap(flatten)]
pub verbose: Verbosity<InfoLevel>,
#[arg(short = 'C', default_value = ".", value_parser = parse_path)]
pub work_dir: PathBuf,
#[arg(long, env = "PRJ_ROOT", default_value = ".", value_parser = parse_path)]
pub tree_root: Option<PathBuf>,
#[arg(long, value_parser = parse_path)]
pub config_file: Option<PathBuf>,
#[arg()]
pub paths: Vec<PathBuf>,
#[arg(short, long)]
pub formatters: Option<Vec<String>>,
}
fn current_dir() -> anyhow::Result<PathBuf> {
env::var("PWD")
.map(PathBuf::from)
.or_else(|_| {
warn!("PWD environment variable not set, if current directory is a symlink it will be dereferenced");
env::current_dir()
})
.map_err(anyhow::Error::new)
}
fn parse_path(s: &str) -> anyhow::Result<PathBuf> {
let cwd = match current_dir() {
Ok(dir) => dir,
Err(err) => return Err(anyhow!("{}", err)),
};
assert!(cwd.is_absolute());
let path = Path::new(s);
Ok(expand_path(path, &cwd))
}
pub fn cli_from_args() -> anyhow::Result<Cli> {
let mut cli = Cli::parse();
match cli.config_file {
None => {
cli.config_file = config::lookup(&cli.work_dir);
}
Some(_) => {
if cli.tree_root.is_none() {
return Err(anyhow!(
"If --config-file is set, --tree-root must also be set"
));
}
}
}
Ok(cli)
}
pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
if cli.init {
init_cmd(&cli.work_dir)?
} else {
match &cli.config_file {
None => {
return Err(anyhow!(
"{} could not be found in {} and up. Use the --init option to create one or specify --config-file if it is in a non-standard location.",
config::FILENAME,
cli.work_dir.display(),
));
}
Some(config_file) => {
if cli.stdin {
format_stdin_cmd(
&cli.tree_root,
&cli.work_dir,
config_file,
&cli.paths,
&cli.formatters,
)?
} else {
format_cmd(
&cli.tree_root,
&cli.work_dir,
config_file,
&cli.paths,
cli.no_cache,
cli.clear_cache,
cli.fail_on_change,
cli.allow_missing_formatter,
&cli.formatters,
)?
}
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
#[ignore = "std::env::set_var: should not be run in parallel."]
fn current_dir_prefers_pwd_env_var() {
use crate::command::current_dir;
use std::env;
use std::path::PathBuf;
let expected_pwd = "/tmp";
let prev_pwd = env::var("PWD").unwrap();
env::set_var("PWD", expected_pwd);
let result = current_dir().unwrap();
env::set_var("PWD", prev_pwd);
assert_eq!(result, PathBuf::from(expected_pwd));
}
#[test]
#[ignore = "std::env::set_var: should not be run in parallel."]
fn current_dir_uses_dereferenced_path_when_pwd_env_var_not_set() {
use crate::command::current_dir;
use std::env;
let expected_pwd = env::current_dir().unwrap();
let prev_pwd = env::var("PWD").unwrap();
env::remove_var("PWD");
let result = current_dir().unwrap();
env::set_var("PWD", prev_pwd);
assert_eq!(result, expected_pwd);
}
}