From b473e652570b294237cae795fb2b673366f1a86d Mon Sep 17 00:00:00 2001 From: Devin Alvaro Date: Tue, 26 Mar 2019 06:47:18 +0700 Subject: [PATCH] Add `--print-config current` --- src/bin/main.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index dc1e64a27bb1..551b422e06cb 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -55,6 +55,10 @@ enum Operation { ConfigOutputDefault { path: Option, }, + /// Output current config (as if formatting to a file) to stdout + ConfigOutputCurrent { + path: Option, + }, /// 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 { } 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 { 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() {