Merge pull request #3472 from devinalvaro/add-print-current-config

Add option to print current config to stdout
This commit is contained in:
Seiichi Uchida 2019-03-29 17:35:02 +09:00 committed by GitHub
commit b4b80b6395
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,10 @@ enum Operation {
ConfigOutputDefault {
path: Option<String>,
},
/// Output current config (as if formatting to a file) to stdout
ConfigOutputCurrent {
path: Option<String>,
},
/// No file specified, read from stdin
Stdin {
input: String,
@ -103,8 +107,9 @@ fn make_opts() -> Options {
"",
"print-config",
"Dumps a default or minimal config to PATH. A minimal config is the \
subset of the current config file used for formatting the current program.",
"[minimal|default] PATH",
subset of the current config file used for formatting the current program. \
`current` writes to stdout current config as if formatting the file at PATH.",
"[default|minimal|current] PATH",
);
if is_nightly {
@ -182,6 +187,21 @@ fn execute(opts: &Options) -> Result<i32, failure::Error> {
}
Ok(0)
}
Operation::ConfigOutputCurrent { path } => {
let path = match path {
Some(path) => path,
None => return Err(format_err!("PATH required for `--print-config current`")),
};
let file = PathBuf::from(path);
let file = file.canonicalize().unwrap_or(file);
let (config, _) = load_config(Some(file.parent().unwrap()), Some(options.clone()))?;
let toml = config.all_options().to_toml().map_err(err_msg)?;
io::stdout().write_all(toml.as_bytes())?;
Ok(0)
}
Operation::Stdin { input } => format_string(input, options),
Operation::Format {
files,
@ -379,6 +399,8 @@ fn determine_operation(matches: &Matches) -> Result<Operation, ErrorKind> {
let path = matches.free.get(0).cloned();
if kind == "default" {
return Ok(Operation::ConfigOutputDefault { path });
} else if kind == "current" {
return Ok(Operation::ConfigOutputCurrent { path });
} else if kind == "minimal" {
minimal_config_path = path;
if minimal_config_path.is_none() {