ripgrep: add --pre flag

The preprocessor flag accepts a command program and executes this
program for every input file that is searched. Instead of searching the
file directly, ripgrep will instead search the stdout contents of the
program.

Closes #978, Closes #981
This commit is contained in:
Charles Blake
2018-07-13 09:54:51 -04:00
committed by Andrew Gallant
parent 1d09d4d31b
commit 231456c409
9 changed files with 211 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
use std::fs::File;
use std::io;
use std::path::Path;
use std::path::{Path, PathBuf};
use encoding_rs::Encoding;
use grep::Grep;
@@ -10,6 +10,7 @@ use termcolor::WriteColor;
use decoder::DecodeReader;
use decompressor::{self, DecompressionReader};
use preprocessor::PreprocessorReader;
use pathutil::strip_prefix;
use printer::Printer;
use search_buffer::BufferSearcher;
@@ -45,6 +46,7 @@ struct Options {
no_messages: bool,
quiet: bool,
text: bool,
preprocessor: Option<PathBuf>,
search_zip_files: bool
}
@@ -68,6 +70,7 @@ impl Default for Options {
quiet: false,
text: false,
search_zip_files: false,
preprocessor: None,
}
}
}
@@ -222,6 +225,12 @@ impl WorkerBuilder {
self.opts.search_zip_files = yes;
self
}
/// If non-empty, search output of preprocessor run on each file
pub fn preprocessor(mut self, command: Option<PathBuf>) -> Self {
self.opts.preprocessor = command;
self
}
}
/// Worker is responsible for executing searches on file paths, while choosing
@@ -250,7 +259,18 @@ impl Worker {
}
Work::DirEntry(dent) => {
let mut path = dent.path();
if self.opts.search_zip_files
if self.opts.preprocessor.is_some() {
let cmd = self.opts.preprocessor.clone().unwrap();
match PreprocessorReader::from_cmd_path(cmd, path) {
Ok(reader) => self.search(printer, path, reader),
Err(err) => {
if !self.opts.no_messages {
eprintln!("{}", err);
}
return 0;
}
}
} else if self.opts.search_zip_files
&& decompressor::is_compressed(path)
{
match DecompressionReader::from_path(path) {