sapling/cmds/configlint.rs

96 lines
2.7 KiB
Rust
Raw Normal View History

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License found in the LICENSE file in the root
* directory of this source tree.
*/
use itertools::Itertools;
use std::collections::BTreeMap;
use cmdlib::args;
Delete failure_ext::err_msg in favor of Error::msg, format_err, bail Summary: I had considered whether to include a top level err_msg function in anyhow and decided not to. For one thing, err_msg is commonly used as a variable name. See for example: - https://github.com/facebookexperimental/mononoke/blob/0f20a3744da71fe2d3c9a7d4bcfec128c5d2b94a/hooks/src/lua_hook.rs#L970 - https://github.com/facebookexperimental/mononoke/blob/0f20a3744da71fe2d3c9a7d4bcfec128c5d2b94a/lfs_server/src/middleware/scuba.rs#L266 - https://github.com/facebookexperimental/mononoke/blob/0f20a3744da71fe2d3c9a7d4bcfec128c5d2b94a/mercurial/bundles/src/part_header.rs#L273 - https://our.intern.facebook.com/intern/diffusion/FBS/browse/master/fbcode/common/rust/shed/async_unit/src/lib.rs?commit=52b2e6293b09407fabad27d738753d54abc1a373&lines=36 - many of the results in https://github.com/search?l=Rust&q=err_msg&type=Code Separately, in our codebase err_msg is needed almost only in code dealing with futures combinators, and this use case will be superseded by things like `bail!` after we pick up async/await syntax. Things like: ``` return Err(Error::msg("Inconsistent data: externally stored Tree")) .into_future() .boxify(); ``` As such, this codemod cuts down our use of failure_ext::err_msg in anticipation of removing it from failure_ext and dropping dependencies on failure_ext to use anyhow directly. - `return Err(err_msg("..."))` ⟶ `bail!("...")` - `err_msg(format!("...", ...))` ⟶ `format_err!("...", ...)` - `err_msg("...")` ⟶ `Error::msg("...")` Reviewed By: jsgf Differential Revision: D18845292 fbshipit-source-id: 1bea32c2fc97ef9af8025617fc294680257974d3
2019-12-06 23:51:47 +03:00
use failure_ext::{bail, Result};
fn main() -> Result<()> {
let matches = args::MononokeApp::new("Lint Mononoke config files")
.with_advanced_args_hidden()
.build()
.version("0.0.0")
.about("Check Mononoke server configs for syntax and sanity.")
.args_from_usage(
r#"
-q --quiet 'Only print errors'
-v --verbose 'Dump content of configs'
"#,
)
.get_matches();
let quiet = matches.is_present("quiet");
let verbose = matches.is_present("verbose");
// Most of the work is done here - this validates that the files are present,
// are correctly formed, and have the right fields (not too many, not too few).
let configs = match args::read_configs(&matches) {
Err(err) => {
eprintln!("Error loading configs: {:#?}", err);
return Err(err);
}
Ok(configs) => configs,
};
if verbose {
println!("Configs:\n{:#?}", configs)
}
// Keep track of what repo ids we've seen
let mut repoids = BTreeMap::<_, Vec<_>>::new();
// Have we seen something suspect?
let mut bad = false;
for (name, config) in &configs.repos {
let (isbad, locality) = match (
config.storage_config.dbconfig.is_local(),
config.storage_config.blobstore.is_local(),
) {
(true, true) => (false, "local"),
(false, false) => (false, "remote"),
(true, false) => (true, "MIXED - local DB, remote blobstore"),
(false, true) => (true, "MIXED - remote DB, local blobstore"),
};
bad |= isbad;
repoids
.entry(config.repoid)
.and_modify(|names| names.push(name.as_str()))
.or_insert(vec![name.as_str()]);
if isbad || !quiet {
println!(
"Repo {}: {} - enabled: {:?} locality: {}",
config.repoid, name, config.enabled, locality
);
}
}
for (id, names) in repoids {
assert!(!names.is_empty());
if names.len() > 1 {
eprintln!(
"ERROR: Repo Id {} used for repos: {}",
id,
names.into_iter().join(", ")
);
bad = true;
}
}
if bad {
Delete failure_ext::err_msg in favor of Error::msg, format_err, bail Summary: I had considered whether to include a top level err_msg function in anyhow and decided not to. For one thing, err_msg is commonly used as a variable name. See for example: - https://github.com/facebookexperimental/mononoke/blob/0f20a3744da71fe2d3c9a7d4bcfec128c5d2b94a/hooks/src/lua_hook.rs#L970 - https://github.com/facebookexperimental/mononoke/blob/0f20a3744da71fe2d3c9a7d4bcfec128c5d2b94a/lfs_server/src/middleware/scuba.rs#L266 - https://github.com/facebookexperimental/mononoke/blob/0f20a3744da71fe2d3c9a7d4bcfec128c5d2b94a/mercurial/bundles/src/part_header.rs#L273 - https://our.intern.facebook.com/intern/diffusion/FBS/browse/master/fbcode/common/rust/shed/async_unit/src/lib.rs?commit=52b2e6293b09407fabad27d738753d54abc1a373&lines=36 - many of the results in https://github.com/search?l=Rust&q=err_msg&type=Code Separately, in our codebase err_msg is needed almost only in code dealing with futures combinators, and this use case will be superseded by things like `bail!` after we pick up async/await syntax. Things like: ``` return Err(Error::msg("Inconsistent data: externally stored Tree")) .into_future() .boxify(); ``` As such, this codemod cuts down our use of failure_ext::err_msg in anticipation of removing it from failure_ext and dropping dependencies on failure_ext to use anyhow directly. - `return Err(err_msg("..."))` ⟶ `bail!("...")` - `err_msg(format!("...", ...))` ⟶ `format_err!("...", ...)` - `err_msg("...")` ⟶ `Error::msg("...")` Reviewed By: jsgf Differential Revision: D18845292 fbshipit-source-id: 1bea32c2fc97ef9af8025617fc294680257974d3
2019-12-06 23:51:47 +03:00
bail!("Anomaly detected")
} else {
Ok(())
}
}