2015-05-01 11:43:36 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2015-10-22 16:35:42 -05:00
|
|
|
|
2015-05-03 17:12:39 -05:00
|
|
|
#![cfg(not(test))]
|
2015-05-01 11:43:36 -05:00
|
|
|
|
2016-04-05 23:34:29 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
extern crate log;
|
2015-05-01 11:43:36 -05:00
|
|
|
extern crate rustfmt;
|
2015-07-31 18:21:44 -05:00
|
|
|
extern crate toml;
|
2015-10-22 16:34:05 -05:00
|
|
|
extern crate env_logger;
|
2015-10-22 16:38:16 -05:00
|
|
|
extern crate getopts;
|
2015-05-01 11:43:36 -05:00
|
|
|
|
2016-04-14 18:51:50 -05:00
|
|
|
use rustfmt::{run, Input, Summary};
|
2017-05-17 23:37:29 -05:00
|
|
|
use rustfmt::file_lines::FileLines;
|
|
|
|
use rustfmt::config::{Config, WriteMode};
|
2015-05-01 11:43:36 -05:00
|
|
|
|
2016-04-09 15:15:36 -05:00
|
|
|
use std::{env, error};
|
2015-10-22 16:35:42 -05:00
|
|
|
use std::fs::{self, File};
|
2016-01-31 09:21:48 -06:00
|
|
|
use std::io::{self, ErrorKind, Read, Write};
|
2015-10-16 03:08:47 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-05-17 23:37:29 -05:00
|
|
|
use std::str::FromStr;
|
2015-10-22 16:38:16 -05:00
|
|
|
|
2015-11-15 00:41:41 -06:00
|
|
|
use getopts::{Matches, Options};
|
2015-05-23 00:02:59 -05:00
|
|
|
|
2016-06-12 03:38:03 -05:00
|
|
|
// Include git commit hash and worktree status; contents are like
|
|
|
|
// const COMMIT_HASH: Option<&'static str> = Some("c31a366");
|
|
|
|
// const WORKTREE_CLEAN: Option<bool> = Some(false);
|
|
|
|
// with `None` if running git failed, eg if it is not installed.
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/git_info.rs"));
|
|
|
|
|
2016-04-09 15:15:36 -05:00
|
|
|
type FmtError = Box<error::Error + Send + Sync>;
|
|
|
|
type FmtResult<T> = std::result::Result<T, FmtError>;
|
2016-03-08 23:52:58 -06:00
|
|
|
|
2015-10-24 08:30:26 -05:00
|
|
|
/// Rustfmt operations.
|
|
|
|
enum Operation {
|
2015-11-12 19:08:57 -06:00
|
|
|
/// Format files and their child modules.
|
2016-02-25 15:27:37 -06:00
|
|
|
Format {
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
config_path: Option<PathBuf>,
|
|
|
|
},
|
2015-10-24 08:30:26 -05:00
|
|
|
/// Print the help message.
|
|
|
|
Help,
|
2015-11-15 21:37:08 -06:00
|
|
|
// Print version information
|
|
|
|
Version,
|
2015-11-07 18:03:25 -06:00
|
|
|
/// Print detailed configuration help.
|
|
|
|
ConfigHelp,
|
2015-11-02 13:45:45 -06:00
|
|
|
/// No file specified, read from stdin
|
2016-02-25 15:27:37 -06:00
|
|
|
Stdin {
|
|
|
|
input: String,
|
|
|
|
config_path: Option<PathBuf>,
|
|
|
|
},
|
2015-10-24 08:30:26 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:34:15 -05:00
|
|
|
/// Parsed command line options.
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
struct CliOptions {
|
|
|
|
skip_children: bool,
|
|
|
|
verbose: bool,
|
2017-05-17 23:37:29 -05:00
|
|
|
write_mode: Option<WriteMode>,
|
|
|
|
file_lines: FileLines, // Default is all lines in all files.
|
2016-04-16 15:34:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliOptions {
|
|
|
|
fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
|
|
|
|
let mut options = CliOptions::default();
|
|
|
|
options.skip_children = matches.opt_present("skip-children");
|
|
|
|
options.verbose = matches.opt_present("verbose");
|
|
|
|
|
2017-05-17 23:37:29 -05:00
|
|
|
if let Some(ref write_mode) = matches.opt_str("write-mode") {
|
|
|
|
if let Ok(write_mode) = WriteMode::from_str(write_mode) {
|
|
|
|
options.write_mode = Some(write_mode);
|
|
|
|
} else {
|
|
|
|
return Err(FmtError::from(format!("Invalid write-mode: {}", write_mode)));
|
|
|
|
}
|
2016-04-16 15:34:15 -05:00
|
|
|
}
|
|
|
|
|
2017-05-17 23:37:29 -05:00
|
|
|
if let Some(ref file_lines) = matches.opt_str("file-lines") {
|
|
|
|
options.file_lines = file_lines.parse()?;
|
2016-04-10 12:03:54 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:34:15 -05:00
|
|
|
Ok(options)
|
|
|
|
}
|
|
|
|
|
2017-05-17 23:37:29 -05:00
|
|
|
fn apply_to(self, config: &mut Config) {
|
|
|
|
config.set().skip_children(self.skip_children);
|
|
|
|
config.set().verbose(self.verbose);
|
|
|
|
config.set().file_lines(self.file_lines);
|
|
|
|
if let Some(write_mode) = self.write_mode {
|
|
|
|
config.set().write_mode(write_mode);
|
2016-04-16 15:34:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 02:44:03 -05:00
|
|
|
const CONFIG_FILE_NAMES: [&'static str; 2] = [".rustfmt.toml", "rustfmt.toml"];
|
|
|
|
|
2016-02-01 11:55:12 -06:00
|
|
|
/// Try to find a project file in the given directory and its parents. Returns the path of a the
|
|
|
|
/// nearest project file if one exists, or `None` if no project file was found.
|
2016-04-09 15:15:36 -05:00
|
|
|
fn lookup_project_file(dir: &Path) -> FmtResult<Option<PathBuf>> {
|
2016-01-31 09:21:48 -06:00
|
|
|
let mut current = if dir.is_relative() {
|
2017-05-07 23:13:49 -05:00
|
|
|
env::current_dir()?.join(dir)
|
2015-10-16 03:08:47 -05:00
|
|
|
} else {
|
2016-01-31 09:21:48 -06:00
|
|
|
dir.to_path_buf()
|
2015-10-16 03:08:47 -05:00
|
|
|
};
|
|
|
|
|
2017-05-07 23:13:49 -05:00
|
|
|
current = fs::canonicalize(current)?;
|
2015-10-16 03:08:47 -05:00
|
|
|
|
2015-07-31 18:21:44 -05:00
|
|
|
loop {
|
2016-07-31 16:32:35 -05:00
|
|
|
for config_file_name in &CONFIG_FILE_NAMES {
|
|
|
|
let config_file = current.join(config_file_name);
|
|
|
|
match fs::metadata(&config_file) {
|
|
|
|
// Only return if it's a file to handle the unlikely situation of a directory named
|
|
|
|
// `rustfmt.toml`.
|
|
|
|
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
|
|
|
|
// Return the error if it's something other than `NotFound`; otherwise we didn't
|
|
|
|
// find the project file yet, and continue searching.
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() != ErrorKind::NotFound {
|
|
|
|
return Err(FmtError::from(e));
|
|
|
|
}
|
2016-01-31 09:21:48 -06:00
|
|
|
}
|
2016-07-31 16:32:35 -05:00
|
|
|
_ => {}
|
2016-01-31 00:48:14 -06:00
|
|
|
}
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
2015-11-09 14:41:25 -06:00
|
|
|
|
|
|
|
// If the current directory has no parent, we're done searching.
|
|
|
|
if !current.pop() {
|
2016-01-31 09:21:48 -06:00
|
|
|
return Ok(None);
|
2015-11-09 14:41:25 -06:00
|
|
|
}
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 02:44:03 -05:00
|
|
|
fn open_config_file(file_path: &Path) -> FmtResult<(Config, Option<PathBuf>)> {
|
2017-05-07 23:13:49 -05:00
|
|
|
let mut file = File::open(&file_path)?;
|
2017-05-07 02:44:03 -05:00
|
|
|
let mut toml = String::new();
|
2017-05-07 23:13:49 -05:00
|
|
|
file.read_to_string(&mut toml)?;
|
2017-05-07 02:44:03 -05:00
|
|
|
match Config::from_toml(&toml) {
|
|
|
|
Ok(cfg) => Ok((cfg, Some(file_path.to_path_buf()))),
|
|
|
|
Err(err) => Err(FmtError::from(err)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 09:21:48 -06:00
|
|
|
/// Resolve the config for input in `dir`.
|
|
|
|
///
|
|
|
|
/// Returns the `Config` to use, and the path of the project file if there was
|
|
|
|
/// one.
|
2016-04-09 15:15:36 -05:00
|
|
|
fn resolve_config(dir: &Path) -> FmtResult<(Config, Option<PathBuf>)> {
|
2017-05-07 23:13:49 -05:00
|
|
|
let path = lookup_project_file(dir)?;
|
2016-01-31 09:21:48 -06:00
|
|
|
if path.is_none() {
|
|
|
|
return Ok((Config::default(), None));
|
|
|
|
}
|
2017-05-07 02:44:03 -05:00
|
|
|
open_config_file(&path.unwrap())
|
2015-07-31 18:21:44 -05:00
|
|
|
}
|
|
|
|
|
2016-01-22 04:43:54 -06:00
|
|
|
/// read the given config file path recursively if present else read the project file path
|
|
|
|
fn match_cli_path_or_file(config_path: Option<PathBuf>,
|
|
|
|
input_file: &Path)
|
2016-04-09 15:15:36 -05:00
|
|
|
-> FmtResult<(Config, Option<PathBuf>)> {
|
2016-01-22 04:43:54 -06:00
|
|
|
|
|
|
|
if let Some(config_file) = config_path {
|
2017-05-07 23:13:49 -05:00
|
|
|
let (toml, path) = open_config_file(config_file.as_ref())?;
|
2016-01-22 04:43:54 -06:00
|
|
|
if path.is_some() {
|
|
|
|
return Ok((toml, path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve_config(input_file)
|
|
|
|
}
|
|
|
|
|
2016-04-10 16:57:44 -05:00
|
|
|
fn make_opts() -> Options {
|
2015-10-24 08:30:26 -05:00
|
|
|
let mut opts = Options::new();
|
|
|
|
opts.optflag("h", "help", "show this message");
|
2015-11-16 07:52:14 -06:00
|
|
|
opts.optflag("V", "version", "show version information");
|
2017-02-12 17:50:10 -06:00
|
|
|
opts.optflag("v", "verbose", "print verbose output");
|
2015-10-24 08:30:26 -05:00
|
|
|
opts.optopt("",
|
|
|
|
"write-mode",
|
2015-11-03 02:16:33 -06:00
|
|
|
"mode to write in (not usable when piping from stdin)",
|
2015-12-27 22:13:32 -06:00
|
|
|
"[replace|overwrite|display|diff|coverage|checkstyle]");
|
2015-12-23 08:25:49 -06:00
|
|
|
opts.optflag("", "skip-children", "don't reformat child modules");
|
2015-10-24 08:30:26 -05:00
|
|
|
|
2015-11-07 18:03:25 -06:00
|
|
|
opts.optflag("",
|
|
|
|
"config-help",
|
|
|
|
"show details of rustfmt configuration options");
|
2016-01-22 04:43:54 -06:00
|
|
|
opts.optopt("",
|
|
|
|
"config-path",
|
|
|
|
"Recursively searches the given path for the rustfmt.toml config file. If not \
|
|
|
|
found reverts to the input file path",
|
|
|
|
"[Path for the configuration file]");
|
2016-04-10 12:03:54 -05:00
|
|
|
opts.optopt("",
|
|
|
|
"file-lines",
|
|
|
|
"Format specified line ranges. See README for more detail on the JSON format.",
|
|
|
|
"JSON");
|
2015-11-07 18:03:25 -06:00
|
|
|
|
2016-04-10 16:57:44 -05:00
|
|
|
opts
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:51:50 -05:00
|
|
|
fn execute(opts: &Options) -> FmtResult<Summary> {
|
2017-05-07 23:13:49 -05:00
|
|
|
let matches = opts.parse(env::args().skip(1))?;
|
2016-04-10 16:57:44 -05:00
|
|
|
|
2017-05-07 23:13:49 -05:00
|
|
|
match determine_operation(&matches)? {
|
2015-10-24 08:30:26 -05:00
|
|
|
Operation::Help => {
|
2016-08-23 09:14:45 -05:00
|
|
|
print_usage(opts, "");
|
2017-04-18 11:23:57 -05:00
|
|
|
Summary::print_exit_codes();
|
2016-04-14 18:51:50 -05:00
|
|
|
Ok(Summary::new())
|
2015-10-24 08:30:26 -05:00
|
|
|
}
|
2015-11-15 21:37:08 -06:00
|
|
|
Operation::Version => {
|
|
|
|
print_version();
|
2016-04-14 18:51:50 -05:00
|
|
|
Ok(Summary::new())
|
2015-11-15 21:37:08 -06:00
|
|
|
}
|
2015-11-07 18:03:25 -06:00
|
|
|
Operation::ConfigHelp => {
|
|
|
|
Config::print_docs();
|
2016-04-14 18:51:50 -05:00
|
|
|
Ok(Summary::new())
|
2015-11-07 18:03:25 -06:00
|
|
|
}
|
2016-02-25 15:27:37 -06:00
|
|
|
Operation::Stdin { input, config_path } => {
|
2015-11-02 13:45:45 -06:00
|
|
|
// try to read config from local directory
|
2017-03-31 10:45:02 -05:00
|
|
|
let (mut config, _) = match_cli_path_or_file(config_path,
|
|
|
|
&env::current_dir().unwrap())?;
|
2015-11-02 13:45:45 -06:00
|
|
|
|
2016-02-05 14:59:41 -06:00
|
|
|
// write_mode is always Plain for Stdin.
|
2017-05-17 23:37:29 -05:00
|
|
|
config.set().write_mode(WriteMode::Plain);
|
2016-02-05 14:59:41 -06:00
|
|
|
|
2017-03-30 18:16:06 -05:00
|
|
|
// parse file_lines
|
|
|
|
if let Some(ref file_lines) = matches.opt_str("file-lines") {
|
2017-05-17 23:37:29 -05:00
|
|
|
config.set().file_lines(file_lines.parse()?);
|
2017-05-16 03:47:09 -05:00
|
|
|
for f in config.file_lines().files() {
|
2017-03-31 12:34:53 -05:00
|
|
|
if f != "stdin" {
|
|
|
|
println!("Warning: Extra file listed in file_lines option '{}'", f);
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 18:16:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:51:50 -05:00
|
|
|
Ok(run(Input::Text(input), &config))
|
2015-11-02 13:45:45 -06:00
|
|
|
}
|
2017-03-30 18:16:06 -05:00
|
|
|
Operation::Format { files, config_path } => {
|
2017-05-07 23:13:49 -05:00
|
|
|
let options = CliOptions::from_matches(&matches)?;
|
2016-04-10 12:03:54 -05:00
|
|
|
|
2017-05-17 23:37:29 -05:00
|
|
|
for f in options.file_lines.files() {
|
|
|
|
if !files.contains(&PathBuf::from(f)) {
|
|
|
|
println!("Warning: Extra file listed in file_lines option '{}'", f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 04:43:54 -06:00
|
|
|
let mut config = Config::default();
|
|
|
|
let mut path = None;
|
|
|
|
// Load the config path file if provided
|
|
|
|
if let Some(config_file) = config_path {
|
2017-05-07 02:44:03 -05:00
|
|
|
let (cfg_tmp, path_tmp) = open_config_file(config_file.as_ref())?;
|
2016-01-22 04:43:54 -06:00
|
|
|
config = cfg_tmp;
|
|
|
|
path = path_tmp;
|
|
|
|
};
|
2017-02-12 17:50:10 -06:00
|
|
|
|
|
|
|
if options.verbose {
|
|
|
|
if let Some(path) = path.as_ref() {
|
|
|
|
println!("Using rustfmt config file {}", path.display());
|
|
|
|
}
|
2016-01-22 04:43:54 -06:00
|
|
|
}
|
2016-04-14 18:51:50 -05:00
|
|
|
|
|
|
|
let mut error_summary = Summary::new();
|
2015-11-12 18:13:25 -06:00
|
|
|
for file in files {
|
2017-04-26 03:29:56 -05:00
|
|
|
if !file.exists() {
|
|
|
|
println!("Error: file `{}` does not exist", file.to_str().unwrap());
|
|
|
|
error_summary.add_operational_error();
|
|
|
|
} else if file.is_dir() {
|
|
|
|
println!("Error: `{}` is a directory", file.to_str().unwrap());
|
|
|
|
error_summary.add_operational_error();
|
|
|
|
} else {
|
|
|
|
// Check the file directory if the config-path could not be read or not provided
|
|
|
|
if path.is_none() {
|
|
|
|
let (config_tmp, path_tmp) = resolve_config(file.parent().unwrap())?;
|
|
|
|
if options.verbose {
|
|
|
|
if let Some(path) = path_tmp.as_ref() {
|
|
|
|
println!("Using rustfmt config file {} for {}",
|
|
|
|
path.display(),
|
|
|
|
file.display());
|
|
|
|
}
|
2017-02-12 17:50:10 -06:00
|
|
|
}
|
2017-04-26 03:29:56 -05:00
|
|
|
config = config_tmp;
|
2016-01-22 04:43:54 -06:00
|
|
|
}
|
2015-11-12 18:13:25 -06:00
|
|
|
|
2017-05-17 23:37:29 -05:00
|
|
|
options.clone().apply_to(&mut config);
|
2017-04-26 03:29:56 -05:00
|
|
|
error_summary.add(run(Input::File(file), &config));
|
|
|
|
}
|
2015-11-12 18:13:25 -06:00
|
|
|
}
|
2016-04-14 18:51:50 -05:00
|
|
|
Ok(error_summary)
|
2015-10-24 08:30:26 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-01 11:43:36 -05:00
|
|
|
}
|
2015-08-18 14:10:30 -05:00
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
fn main() {
|
2015-10-22 16:34:05 -05:00
|
|
|
let _ = env_logger::init();
|
2015-10-24 08:30:26 -05:00
|
|
|
|
2016-04-09 15:15:36 -05:00
|
|
|
let opts = make_opts();
|
|
|
|
|
|
|
|
let exit_code = match execute(&opts) {
|
2016-04-14 18:51:50 -05:00
|
|
|
Ok(summary) => {
|
|
|
|
if summary.has_operational_errors() {
|
|
|
|
1
|
|
|
|
} else if summary.has_parsing_errors() {
|
|
|
|
2
|
|
|
|
} else if summary.has_formatting_errors() {
|
|
|
|
3
|
2016-06-20 13:42:29 -05:00
|
|
|
} else if summary.has_diff {
|
|
|
|
// should only happen in diff mode
|
|
|
|
4
|
2016-04-14 18:51:50 -05:00
|
|
|
} else {
|
|
|
|
assert!(summary.has_no_errors());
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
2016-04-09 15:15:36 -05:00
|
|
|
Err(e) => {
|
|
|
|
print_usage(&opts, &e.to_string());
|
|
|
|
1
|
|
|
|
}
|
|
|
|
};
|
2015-10-24 08:30:26 -05:00
|
|
|
// Make sure standard output is flushed before we exit.
|
2015-08-28 03:28:28 -05:00
|
|
|
std::io::stdout().flush().unwrap();
|
2015-10-24 08:30:26 -05:00
|
|
|
|
2015-08-28 03:28:28 -05:00
|
|
|
// Exit with given exit code.
|
|
|
|
//
|
|
|
|
// NOTE: This immediately terminates the process without doing any cleanup,
|
|
|
|
// so make sure to finish all necessary cleanup before this is called.
|
|
|
|
std::process::exit(exit_code);
|
2015-08-26 17:02:06 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:38:16 -05:00
|
|
|
fn print_usage(opts: &Options, reason: &str) {
|
2017-05-07 02:44:03 -05:00
|
|
|
let reason = format!("{}\n\nusage: {} [options] <file>...",
|
2015-10-22 16:38:16 -05:00
|
|
|
reason,
|
2017-03-27 17:25:59 -05:00
|
|
|
env::args_os().next().unwrap().to_string_lossy());
|
2015-10-22 16:38:16 -05:00
|
|
|
println!("{}", opts.usage(&reason));
|
2015-08-28 03:28:28 -05:00
|
|
|
}
|
|
|
|
|
2015-11-15 21:37:08 -06:00
|
|
|
fn print_version() {
|
2016-06-12 03:38:03 -05:00
|
|
|
println!("{} ({}{})",
|
|
|
|
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
|
|
|
|
COMMIT_HASH.unwrap_or("git commit unavailable"),
|
|
|
|
match WORKTREE_CLEAN {
|
|
|
|
Some(false) => " worktree dirty",
|
|
|
|
_ => "",
|
|
|
|
});
|
2015-11-15 21:37:08 -06:00
|
|
|
}
|
|
|
|
|
2016-04-09 15:15:36 -05:00
|
|
|
fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
|
2015-10-22 16:38:16 -05:00
|
|
|
if matches.opt_present("h") {
|
2016-04-09 15:15:36 -05:00
|
|
|
return Ok(Operation::Help);
|
2015-10-22 16:38:16 -05:00
|
|
|
}
|
|
|
|
|
2015-11-07 18:03:25 -06:00
|
|
|
if matches.opt_present("config-help") {
|
2016-04-09 15:15:36 -05:00
|
|
|
return Ok(Operation::ConfigHelp);
|
2015-11-07 18:03:25 -06:00
|
|
|
}
|
|
|
|
|
2015-11-15 21:37:08 -06:00
|
|
|
if matches.opt_present("version") {
|
2016-04-09 15:15:36 -05:00
|
|
|
return Ok(Operation::Version);
|
2015-11-15 21:37:08 -06:00
|
|
|
}
|
|
|
|
|
2017-05-07 02:44:03 -05:00
|
|
|
let config_path_not_found = |path: &str| -> FmtResult<Operation> {
|
|
|
|
Err(FmtError::from(format!("Error: unable to find a config file for the given path: `{}`",
|
|
|
|
path)))
|
|
|
|
};
|
|
|
|
|
2016-01-22 04:43:54 -06:00
|
|
|
// Read the config_path and convert to parent dir if a file is provided.
|
2017-05-07 02:44:03 -05:00
|
|
|
// If a config file cannot be found from the given path, return error.
|
|
|
|
let config_path: Option<PathBuf> = match matches.opt_str("config-path").map(PathBuf::from) {
|
|
|
|
Some(ref path) if !path.exists() => return config_path_not_found(path.to_str().unwrap()),
|
|
|
|
Some(ref path) if path.is_dir() => {
|
|
|
|
let mut config_file_path = None;
|
|
|
|
for config_file_name in &CONFIG_FILE_NAMES {
|
|
|
|
let temp_path = path.join(config_file_name);
|
|
|
|
if temp_path.is_file() {
|
|
|
|
config_file_path = Some(temp_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config_file_path.is_some() {
|
|
|
|
config_file_path
|
|
|
|
} else {
|
|
|
|
return config_path_not_found(path.to_str().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path @ _ => path,
|
|
|
|
};
|
2016-01-22 04:43:54 -06:00
|
|
|
|
2017-03-30 18:16:06 -05:00
|
|
|
// if no file argument is supplied, read from stdin
|
|
|
|
if matches.free.is_empty() {
|
2015-11-02 13:45:45 -06:00
|
|
|
let mut buffer = String::new();
|
2017-05-07 23:13:49 -05:00
|
|
|
io::stdin().read_to_string(&mut buffer)?;
|
2015-11-02 13:45:45 -06:00
|
|
|
|
2016-04-09 15:15:36 -05:00
|
|
|
return Ok(Operation::Stdin {
|
2017-02-20 21:59:14 -06:00
|
|
|
input: buffer,
|
|
|
|
config_path: config_path,
|
|
|
|
});
|
2015-11-02 13:45:45 -06:00
|
|
|
}
|
|
|
|
|
2017-04-09 16:25:12 -05:00
|
|
|
let files: Vec<_> = matches
|
|
|
|
.free
|
|
|
|
.iter()
|
|
|
|
.map(|s| {
|
|
|
|
let p = PathBuf::from(s);
|
|
|
|
// we will do comparison later, so here tries to canonicalize first
|
|
|
|
// to get the expected behavior.
|
|
|
|
p.canonicalize().unwrap_or(p)
|
|
|
|
})
|
|
|
|
.collect();
|
2015-11-12 18:13:25 -06:00
|
|
|
|
2016-04-09 15:15:36 -05:00
|
|
|
Ok(Operation::Format {
|
2017-02-20 21:59:14 -06:00
|
|
|
files: files,
|
|
|
|
config_path: config_path,
|
|
|
|
})
|
2015-08-18 14:10:30 -05:00
|
|
|
}
|