feat: hard fail on missing tools (#189)

This commit is contained in:
BMG 2022-12-01 17:15:44 +00:00 committed by GitHub
parent 1375bff247
commit b06edc3c8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 11 deletions

View File

@ -47,15 +47,15 @@ USAGE:
treefmt [FLAGS] [OPTIONS] [paths]...
FLAGS:
--clear-cache Reset the evaluation cache. Use in case the cache is not precise enough
--fail-on-change Exit with error if any changes were made. Useful for CI
-h, --help Prints help information
--init Create a new treefmt.toml
--no-cache Ignore the evaluation cache entirely. Useful for CI
-q, --quiet No output printed to stderr
--stdin Format the content passed in stdin
-V, --version Prints version information
-v, --verbose Log verbosity is based off the number of v used
--allow-missing-formatter Do not exit with error if a configured formatter is missing
--clear-cache Clear the evaluation cache. Use in case the cache is not precise enough
--fail-on-change Exit with error if any changes were made. Useful for CI
-h, --help Prints help information
--init Create a new treefmt.toml
-q, --quiet No output printed to stderr
--stdin Format the content passed in stdin
-V, --version Prints version information
-v, --verbose Log verbosity is based off the number of v used
OPTIONS:
--config-file <config-file> Run with the specified config file, which is not required to be in the tree to be

View File

@ -12,6 +12,7 @@ pub fn format_cmd(
no_cache: bool,
clear_cache: bool,
fail_on_change: bool,
allow_missing_formatter: bool,
selected_formatters: &Option<Vec<String>>,
) -> anyhow::Result<()> {
let proj_dirs = match ProjectDirs::from("com", "NumTide", "treefmt") {
@ -57,6 +58,7 @@ pub fn format_cmd(
no_cache,
clear_cache,
fail_on_change,
allow_missing_formatter,
selected_formatters,
)?;

View File

@ -41,6 +41,10 @@ pub struct Cli {
)]
pub fail_on_change: bool,
/// Do not exit with error if a configured formatter is missing
#[structopt(long = "allow-missing-formatter")]
pub allow_missing_formatter: bool,
/// Log verbosity is based off the number of v used
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
pub verbosity: u8,
@ -133,6 +137,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
cli.no_cache,
cli.clear_cache,
cli.fail_on_change,
cli.allow_missing_formatter,
&cli.formatters,
)?
}

View File

@ -29,6 +29,7 @@ pub fn run_treefmt(
no_cache: bool,
clear_cache: bool,
fail_on_change: bool,
allow_missing_formatter: bool,
selected_formatters: &Option<Vec<String>>,
) -> anyhow::Result<()> {
assert!(tree_root.is_absolute());
@ -82,10 +83,13 @@ pub fn run_treefmt(
timed_debug("load config");
// Load all the formatter instances from the config. Ignore the ones that failed.
// Load all the formatter instances from the config.
let mut expected_count = 0;
let formatters = project_config.formatter.into_iter().fold(
BTreeMap::new(),
|mut sum, (name, mut fmt_config)| {
expected_count += 1;
fmt_config.excludes.extend_from_slice(&global_excludes);
match Formatter::from_config(tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => match selected_formatters {
@ -98,7 +102,13 @@ pub fn run_treefmt(
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}
},
Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
Err(err) => {
if allow_missing_formatter {
error!("Ignoring formatter #{} due to error: {}", name, err)
} else {
error!("Failed to load formatter #{} due to error: {}", name, err)
}
}
};
sum
},
@ -106,6 +116,11 @@ pub fn run_treefmt(
timed_debug("load formatters");
// Check the number of configured formatters matches the number of formatters loaded
if !(allow_missing_formatter || formatters.len() == expected_count) {
return Err(anyhow!("One or more formatters are missing"));
}
// Load the eval cache
let mut cache = if no_cache || clear_cache {
// Start with an empty cache