Skip to content

Commit

Permalink
config: add persistent configuration
Browse files Browse the repository at this point in the history
This commit adds support for reading configuration files that change
ripgrep's default behavior. The format of the configuration file is an
"rc" style and is very simple. It is defined by two rules:

  1. Every line is a shell argument, after trimming ASCII whitespace.
  2. Lines starting with '#' (optionally preceded by any amount of
     ASCII whitespace) are ignored.

ripgrep will look for a single configuration file if and only if the
RIPGREP_CONFIG_PATH environment variable is set and is non-empty.
ripgrep will parse shell arguments from this file on startup and will
behave as if the arguments in this file were prepended to any explicit
arguments given to ripgrep on the command line.

For example, if your ripgreprc file contained a single line:

    --smart-case

then the following command

    RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo

would behave identically to the following command

    rg --smart-case foo

This commit also adds a new flag, --no-config, that when present will
suppress any and all support for configuration. This includes any future
support for auto-loading configuration files from pre-determined paths
(which this commit does not add).

Conflicts between configuration files and explicit arguments are handled
exactly like conflicts in the same command line invocation. That is,
this command:

    RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo --case-sensitive

is exactly equivalent to

    rg --smart-case foo --case-sensitive

in which case, the --case-sensitive flag would override the --smart-case
flag.

Closes #196
  • Loading branch information
BurntSushi committed Feb 4, 2018
1 parent d83bab4 commit c57d0fb
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 5 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,50 @@ extensions.
The syntax supported is
[documented as part of Rust's regex library](https://doc.rust-lang.org/regex/regex/index.html#syntax).

### Configuration files

ripgrep supports reading configuration files that change ripgrep's default
behavior. The format of the configuration file is an "rc" style and is very
simple. It is defined by two rules:

1. Every line is a shell argument, after trimming ASCII whitespace.
2. Lines starting with '#' (optionally preceded by any amount of
ASCII whitespace) are ignored.

ripgrep will look for a single configuration file if and only if the
`RIPGREP_CONFIG_PATH` environment variable is set and is non-empty. ripgrep
will parse shell arguments from this file on startup and will behave as if
the arguments in this file were prepended to any explicit arguments given to
ripgrep on the command line.

For example, if your ripgreprc file contained a single line:

--smart-case

then the following command

RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo

would behave identically to the following command

rg --smart-case foo

ripgrep also provides a flag, --no-config, that when present will suppress
any and all support for configuration. This includes any future support for
auto-loading configuration files from pre-determined paths.

Conflicts between configuration files and explicit arguments are handled
exactly like conflicts in the same command line invocation. That is, this
command:

RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo --case-sensitive

is exactly equivalent to

rg --smart-case foo --case-sensitive

in which case, the --case-sensitive flag would override the --smart-case flag.

### Shell completions

Shell completion files are included in the release tarball for Bash, Fish, Zsh
Expand Down
1 change: 1 addition & 0 deletions complete/_rg
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ _rg() {
'(--mmap --no-mmap)--mmap[search using memory maps when possible]'
'(-H --with-filename --no-filename)--no-filename[suppress all file names]'
"(-p --heading --pretty --vimgrep)--no-heading[don't group matches by file name]"
"--no-config[don't load configuration files]"
"(--no-ignore-parent)--no-ignore[don't respect ignore files]"
"--no-ignore-parent[don't respect ignore files in parent directories]"
"--no-ignore-vcs[don't respect version control ignore files]"
Expand Down
82 changes: 82 additions & 0 deletions doc/rg.1
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ context related options.)
.RS
.RE
.TP
.B \-\-no\-config
Never read configuration files.
When this flag is present, ripgrep will not respect the
RIPGREP_CONFIG_PATH environment variable.
.RS
.PP
If ripgrep ever grows a feature to automatically read configuration
files in pre\-defined locations, then this flag will also disable that
behavior as well.
.RE
.TP
.B \-\-no\-messages
Suppress all error messages.
.RS
Expand Down Expand Up @@ -597,6 +608,77 @@ ripgrep.
Note that this must be passed to every invocation of rg.
.RS
.RE
.SH CONFIGURATION FILES
.PP
ripgrep supports reading configuration files that change ripgrep\[aq]s
default behavior.
The format of the configuration file is an "rc" style and is very
simple.
It is defined by two rules:
.IP
.nf
\f[C]
1.\ Every\ line\ is\ a\ shell\ argument,\ after\ trimming\ ASCII\ whitespace.
2.\ Lines\ starting\ with\ \[aq]#\[aq]\ (optionally\ preceded\ by\ any\ amount\ of
\ \ \ ASCII\ whitespace)\ are\ ignored.
\f[]
.fi
.PP
ripgrep will look for a single configuration file if and only if the
RIPGREP_CONFIG_PATH environment variable is set and is non\-empty.
ripgrep will parse shell arguments from this file on startup and will
behave as if the arguments in this file were prepended to any explicit
arguments given to ripgrep on the command line.
.PP
For example, if your ripgreprc file contained a single line:
.IP
.nf
\f[C]
\-\-smart\-case
\f[]
.fi
.PP
then the following command
.IP
.nf
\f[C]
RIPGREP_CONFIG_PATH=wherever/.ripgreprc\ rg\ foo
\f[]
.fi
.PP
would behave identically to the following command
.IP
.nf
\f[C]
rg\ \-\-smart\-case\ foo
\f[]
.fi
.PP
ripgrep also provides a flag, \-\-no\-config, that when present will
suppress any and all support for configuration.
This includes any future support for auto\-loading configuration files
from pre\-determined paths.
.PP
Conflicts between configuration files and explicit arguments are handled
exactly like conflicts in the same command line invocation.
That is, this command:
.IP
.nf
\f[C]
RIPGREP_CONFIG_PATH=wherever/.ripgreprc\ rg\ foo\ \-\-case\-sensitive
\f[]
.fi
.PP
is exactly equivalent to
.IP
.nf
\f[C]
rg\ \-\-smart\-case\ foo\ \-\-case\-sensitive
\f[]
.fi
.PP
in which case, the \-\-case\-sensitive flag would override the
\-\-smart\-case flag.
.SH SHELL COMPLETION
.PP
Shell completion files are included in the release tarball for Bash,
Expand Down
53 changes: 53 additions & 0 deletions doc/rg.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ Project home page: https://github.com/BurntSushi/ripgrep
when ripgrep thinks it will be faster. (Note that mmap searching
doesn't currently support the various context related options.)

--no-config
: Never read configuration files. When this flag is present, ripgrep will not
respect the RIPGREP_CONFIG_PATH environment variable.

If ripgrep ever grows a feature to automatically read configuration files
in pre-defined locations, then this flag will also disable that behavior as
well.

--no-messages
: Suppress all error messages.

Expand Down Expand Up @@ -392,6 +400,51 @@ Project home page: https://github.com/BurntSushi/ripgrep
the default type definitions that are found inside of ripgrep. Note
that this must be passed to every invocation of rg.

# CONFIGURATION FILES

ripgrep supports reading configuration files that change
ripgrep's default behavior. The format of the configuration file is an
"rc" style and is very simple. It is defined by two rules:

1. Every line is a shell argument, after trimming ASCII whitespace.
2. Lines starting with '#' (optionally preceded by any amount of
ASCII whitespace) are ignored.

ripgrep will look for a single configuration file if and only if the
RIPGREP_CONFIG_PATH environment variable is set and is non-empty.
ripgrep will parse shell arguments from this file on startup and will
behave as if the arguments in this file were prepended to any explicit
arguments given to ripgrep on the command line.

For example, if your ripgreprc file contained a single line:

--smart-case

then the following command

RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo

would behave identically to the following command

rg --smart-case foo

ripgrep also provides a flag, --no-config, that when present will suppress
any and all support for configuration. This includes any future support
for auto-loading configuration files from pre-determined paths.

Conflicts between configuration files and explicit arguments are handled
exactly like conflicts in the same command line invocation. That is,
this command:

RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo --case-sensitive

is exactly equivalent to

rg --smart-case foo --case-sensitive

in which case, the --case-sensitive flag would override the --smart-case
flag.

# SHELL COMPLETION

Shell completion files are included in the release tarball for Bash, Fish, Zsh
Expand Down
23 changes: 21 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Note that ripgrep may abort unexpectedly when using default settings if it
searches a file that is simultaneously truncated. This behavior can be avoided
by passing the --no-mmap flag.
ripgrep supports configuration files. Set RIPGREP_CONFIG_PATH to a
configuration file. The file can specify one shell argument per line. Lines
starting with '#' are ignored. For more details, see the man page or the
README.
Project home page: https://github.com/BurntSushi/ripgrep
Use -h for short descriptions and --help for more details.";
Expand Down Expand Up @@ -513,6 +518,7 @@ fn all_args_and_flags() -> Vec<RGArg> {
flag_max_filesize(&mut args);
flag_maxdepth(&mut args);
flag_mmap(&mut args);
flag_no_config(&mut args);
flag_no_ignore(&mut args);
flag_no_ignore_parent(&mut args);
flag_no_ignore_vcs(&mut args);
Expand Down Expand Up @@ -1113,6 +1119,20 @@ This flag overrides --mmap.
args.push(arg);
}

fn flag_no_config(args: &mut Vec<RGArg>) {
const SHORT: &str = "Never read configuration files.";
const LONG: &str = long!("\
Never read configuration files. When this flag is present, ripgrep will not
respect the RIPGREP_CONFIG_PATH environment variable.
If ripgrep ever grows a feature to automatically read configuration files in
pre-defined locations, then this flag will also disable that behavior as well.
");
let arg = RGArg::switch("no-config")
.help(SHORT).long_help(LONG);
args.push(arg);
}

fn flag_no_ignore(args: &mut Vec<RGArg>) {
const SHORT: &str = "Don't respect ignore files.";
const LONG: &str = long!("\
Expand Down Expand Up @@ -1182,8 +1202,7 @@ part on a separate output line.
}

fn flag_path_separator(args: &mut Vec<RGArg>) {
const SHORT: &str =
"Set the path separator to use when printing file paths.";
const SHORT: &str = "Set the path separator.";
const LONG: &str = long!("\
Set the path separator to use when printing file paths. This defaults to your
platform's path separator, which is / on Unix and \\ on Windows. This flag is
Expand Down
47 changes: 45 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use printer::{ColorSpecs, Printer};
use unescape::unescape;
use worker::{Worker, WorkerBuilder};

use config;
use logger::Logger;
use Result;

Expand Down Expand Up @@ -88,17 +89,59 @@ impl Args {
///
/// Also, initialize a global logger.
pub fn parse() -> Result<Args> {
let matches = app::app().get_matches();
// We parse the args given on CLI. This does not include args from
// the config. We use the CLI args as an initial configuration while
// trying to parse config files. If a config file exists and has
// arguments, then we re-parse argv, otherwise we just use the matches
// we have here.
let early_matches = ArgMatches(app::app().get_matches());

if let Err(err) = Logger::init() {
errored!("failed to initialize logger: {}", err);
}
if early_matches.is_present("debug") {
log::set_max_level(log::LevelFilter::Debug);
} else {
log::set_max_level(log::LevelFilter::Warn);
}

let matches = Args::matches(early_matches);
// The logging level may have changed if we brought in additional
// arguments from a configuration file, so recheck it and set the log
// level as appropriate.
if matches.is_present("debug") {
log::set_max_level(log::LevelFilter::Debug);
} else {
log::set_max_level(log::LevelFilter::Warn);
}
ArgMatches(matches).to_args()
matches.to_args()
}

/// Run clap and return the matches. If clap determines a problem with the
/// user provided arguments (or if --help or --version are given), then an
/// error/usage/version will be printed and the process will exit.
///
/// If there are no additional arguments from the environment (e.g., a
/// config file), then the given matches are returned as is.
fn matches(early_matches: ArgMatches<'static>) -> ArgMatches<'static> {
// If the end user says no config, then respect it.
if early_matches.is_present("no-config") {
debug!("not reading config files because --no-config is present");
return early_matches;
}
// If the user wants ripgrep to use a config file, then parse args
// from that first.
let mut args = config::args(early_matches.is_present("no-messages"));
if args.is_empty() {
return early_matches;
}
let mut cliargs = env::args_os();
if let Some(bin) = cliargs.next() {
args.insert(0, bin);
}
args.extend(cliargs);
debug!("final argv: {:?}", args);
ArgMatches(app::app().get_matches_from(args))
}

/// Returns true if ripgrep should print the files it will search and exit
Expand Down
Loading

0 comments on commit c57d0fb

Please sign in to comment.