ripgrep-all/src/args.rs

291 lines
10 KiB
Rust
Raw Normal View History

2020-06-08 21:11:43 +00:00
use crate::project_dirs;
use anyhow::*;
2020-06-08 21:11:43 +00:00
use derive_more::FromStr;
2019-06-07 19:46:03 +00:00
use log::*;
2020-06-08 21:11:43 +00:00
use schemars::JsonSchema;
2019-06-07 19:46:03 +00:00
use serde::{Deserialize, Serialize};
use std::ffi::OsString;
2020-06-08 21:11:43 +00:00
use std::{fs::File, io::Write, iter::IntoIterator, str::FromStr};
2019-06-07 19:46:03 +00:00
use structopt::StructOpt;
#[derive(Debug, Deserialize, Serialize)]
struct ReadableBytesCount(i64);
2020-06-08 21:11:43 +00:00
fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}
#[derive(JsonSchema, Debug, Serialize, Deserialize, Copy, Clone, PartialEq, FromStr)]
pub struct CacheCompressionLevel(pub i32);
impl ToString for CacheCompressionLevel {
fn to_string(&self) -> String {
self.0.to_string()
}
}
2020-06-08 21:11:43 +00:00
impl Default for CacheCompressionLevel {
fn default() -> Self {
CacheCompressionLevel(12)
}
}
#[derive(JsonSchema, Debug, Serialize, Deserialize, Copy, Clone, PartialEq, FromStr)]
pub struct MaxArchiveRecursion(pub i32);
2020-06-08 21:11:43 +00:00
impl ToString for MaxArchiveRecursion {
fn to_string(&self) -> String {
self.0.to_string()
}
}
impl Default for MaxArchiveRecursion {
fn default() -> Self {
MaxArchiveRecursion(4)
}
2019-06-07 19:46:03 +00:00
}
2020-06-08 21:11:43 +00:00
#[derive(JsonSchema, Debug, Serialize, Deserialize, Copy, Clone, PartialEq)]
pub struct CacheMaxBlobLen(pub usize);
impl ToString for CacheMaxBlobLen {
fn to_string(&self) -> String {
self.0.to_string()
}
}
impl Default for CacheMaxBlobLen {
fn default() -> Self {
CacheMaxBlobLen(2000000)
}
2019-06-07 21:04:18 +00:00
}
2020-06-08 21:11:43 +00:00
impl FromStr for CacheMaxBlobLen {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let suffix = s.chars().last();
if let Some(suffix) = suffix {
Ok(CacheMaxBlobLen(match suffix {
'k' | 'M' | 'G' => usize::from_str(s.trim_end_matches(suffix))
.with_context(|| format!("Could not parse int"))
.map(|e| {
e * match suffix {
'k' => 1000,
'M' => 1000_000,
'G' => 1000_000_000,
_ => panic!("impossible"),
}
}),
_ => usize::from_str(s).with_context(|| format!("Could not parse int")),
}?))
} else {
Err(format_err!("empty byte input"))
}
}
}
2019-06-07 21:04:18 +00:00
2020-06-08 21:11:43 +00:00
#[derive(StructOpt, Debug, Deserialize, Serialize, JsonSchema, Default)]
2019-06-15 09:00:45 +00:00
#[structopt(
2020-05-19 09:10:11 +00:00
name = "ripgrep-all",
2019-06-15 09:00:45 +00:00
rename_all = "kebab-case",
2020-05-19 09:10:11 +00:00
about = env!("CARGO_PKG_DESCRIPTION"),
author = env!("CARGO_PKG_HOMEPAGE"),
2019-06-15 09:00:45 +00:00
// TODO: long_about does not seem to work to only show this on short help
2020-06-06 13:01:53 +00:00
after_help = "-h shows a concise overview, --help shows more detail and advanced options.\n\nAll other options not shown here are passed directly to rg, especially [PATTERN] and [PATH ...]",
usage = "rga [RGA OPTIONS] [RG OPTIONS] PATTERN [PATH ...]"
2019-06-15 09:00:45 +00:00
)]
2020-06-08 21:11:43 +00:00
/// # rga configuration
///
/// this is kind of a "polyglot" struct, since it serves three functions
///
/// 1. describing the command line arguments using structopt+clap
/// 2. describing the config file format (output as JSON schema via schemars)
pub struct RgaConfig {
2019-06-07 19:46:03 +00:00
#[serde(default, skip_serializing_if = "is_default")]
2019-06-12 20:55:18 +00:00
#[structopt(long = "--rga-no-cache")]
/// Disable caching of results
///
/// By default, rga caches the extracted text, if it is small enough,
2020-06-06 13:01:53 +00:00
/// to a database in ~/.cache/rga on Linux,
/// ~/Library/Caches/rga on macOS,
/// or C:\Users\username\AppData\Local\rga on Windows.
2019-06-12 20:55:18 +00:00
/// This way, repeated searches on the same set of files will be much faster.
/// If you pass this flag, all caching will be disabled.
2019-06-11 11:34:04 +00:00
pub no_cache: bool,
2019-06-07 19:46:03 +00:00
#[serde(default, skip_serializing_if = "is_default")]
2019-06-12 20:55:18 +00:00
#[structopt(long = "--rga-accurate")]
/// Use more accurate but slower matching by mime type
///
/// By default, rga will match files using file extensions.
/// Some programs, such as sqlite3, don't care about the file extension at all,
/// so users sometimes use any or no extension at all. With this flag, rga
/// will try to detect the mime type of input files using the magic bytes
/// (similar to the `file` utility), and use that to choose the adapter.
2019-06-13 13:18:14 +00:00
/// Detection is only done on the first 8KiB of the file, since we can't always seek on the input (in archives).
2019-06-11 11:34:04 +00:00
pub accurate: bool,
#[serde(default, skip_serializing_if = "is_default")]
#[structopt(
long = "--rga-adapters",
2019-06-07 19:46:03 +00:00
require_equals = true,
2019-06-12 20:55:18 +00:00
require_delimiter = true
2019-06-07 19:46:03 +00:00
)]
2019-06-12 20:55:18 +00:00
/// Change which adapters to use and in which priority order (descending)
///
/// "foo,bar" means use only adapters foo and bar.
/// "-bar,baz" means use all default adapters except for bar and baz.
/// "+bar,baz" means use all default adapters and also bar and baz.
2019-06-11 11:34:04 +00:00
pub adapters: Vec<String>,
2019-06-07 21:04:18 +00:00
2020-06-08 21:11:43 +00:00
#[serde(default, skip_serializing_if = "is_default")]
2019-06-15 09:00:45 +00:00
#[structopt(
2020-06-08 21:11:43 +00:00
default_value,
2019-06-15 09:00:45 +00:00
long = "--rga-cache-max-blob-len",
hidden_short_help = true,
require_equals = true,
2020-06-08 21:11:43 +00:00
// parse(try_from_str = parse_readable_bytes_str)
2019-06-15 09:00:45 +00:00
)]
2019-06-12 20:55:18 +00:00
/// Max compressed size to cache
///
/// Longest byte length (after compression) to store in cache. Longer adapter outputs will not be cached and recomputed every time. Allowed suffixes: k M G
2020-06-08 21:11:43 +00:00
pub cache_max_blob_len: CacheMaxBlobLen,
2019-06-07 21:04:18 +00:00
2020-06-08 21:11:43 +00:00
#[serde(default, skip_serializing_if = "is_default")]
2019-06-07 21:04:18 +00:00
#[structopt(
2020-06-08 21:11:43 +00:00
default_value,
2019-06-11 11:34:04 +00:00
long = "--rga-cache-compression-level",
2019-06-12 20:55:18 +00:00
hidden_short_help = true,
2019-06-07 21:04:18 +00:00
require_equals = true,
2019-06-12 20:55:18 +00:00
help = ""
2019-06-07 21:04:18 +00:00
)]
2019-06-12 20:55:18 +00:00
/// ZSTD compression level to apply to adapter outputs before storing in cache db
///
/// Ranges from 1 - 22
2020-06-08 21:11:43 +00:00
pub cache_compression_level: CacheCompressionLevel,
2019-06-07 21:04:18 +00:00
2020-06-08 21:11:43 +00:00
#[serde(default, skip_serializing_if = "is_default")]
2019-06-07 21:04:18 +00:00
#[structopt(
2020-06-08 21:11:43 +00:00
default_value,
2019-06-11 11:34:04 +00:00
long = "--rga-max-archive-recursion",
2019-06-07 21:04:18 +00:00
require_equals = true,
2019-06-15 09:00:45 +00:00
hidden_short_help = true
2019-06-07 21:04:18 +00:00
)]
2020-06-08 21:11:43 +00:00
/// Maximum nestedness of archives to recurse into
pub max_archive_recursion: MaxArchiveRecursion,
2019-06-07 21:04:18 +00:00
2020-06-08 12:00:18 +00:00
#[serde(skip)]
#[structopt(long = "--rga-fzf-path", require_equals = true, hidden = true)]
/// same as passing path directly, except if argument is empty
/// kinda hacky, but if no file is found, fzf calls rga with empty string as path, which causes No such file or directory from rg. So filter those cases and return specially
pub fzf_path: Option<String>,
2020-06-08 21:11:43 +00:00
// these arguments are basically "subcommands" that stop the process, so don't serialize them
2019-06-07 21:04:18 +00:00
#[serde(skip)]
2019-06-11 11:34:04 +00:00
#[structopt(long = "--rga-list-adapters", help = "List all known adapters")]
pub list_adapters: bool,
2019-06-07 19:46:03 +00:00
2020-06-08 21:11:43 +00:00
#[serde(skip)]
#[structopt(
long = "--rga-print-config-schema",
help = "Print the JSON Schema of the configuration file"
)]
pub print_config_schema: bool,
2019-06-07 19:46:03 +00:00
#[serde(skip)]
#[structopt(long, help = "Show help for ripgrep itself")]
pub rg_help: bool,
#[serde(skip)]
#[structopt(long, help = "Show version of ripgrep itself")]
pub rg_version: bool,
2020-06-08 21:11:43 +00:00
#[serde(rename = "$schema", default = "default_schema_path")]
#[structopt(skip)]
pub _schema_key: String,
}
fn default_schema_path() -> String {
"./config.schema.json".to_string()
2019-06-07 19:46:03 +00:00
}
static RGA_CONFIG: &str = "RGA_CONFIG";
2020-06-08 21:11:43 +00:00
pub fn parse_args<I>(args: I) -> Result<RgaConfig>
2019-06-07 19:46:03 +00:00
where
I: IntoIterator,
I::Item: Into<OsString> + Clone,
{
2020-06-08 21:11:43 +00:00
let proj = project_dirs()?;
let config_dir = proj.config_dir();
if config_dir.join("config.json").exists() {
// todo: read config
} else {
std::fs::create_dir_all(config_dir)?;
let mut schemafile = File::create(config_dir.join("config.schema.json"))?;
schemafile
.write(serde_json::to_string_pretty(&schemars::schema_for!(RgaConfig))?.as_bytes())?;
let mut configfile = File::create(config_dir.join("config.json"))?;
let mut v = serde_json::to_value(&RgaConfig::default())?;
match &mut v {
serde_json::Value::Object(o) => {
o["$schema"] = serde_json::Value::String("./config.schema.json".to_string())
}
_ => panic!("impos"),
}
configfile.write(serde_json::to_string_pretty(&v)?.as_bytes())?;
}
2019-06-07 19:46:03 +00:00
match std::env::var(RGA_CONFIG) {
Ok(val) => {
2019-06-12 15:23:30 +00:00
debug!(
"Loading args from env {}={}, ignoring cmd args",
RGA_CONFIG, val
);
2019-06-07 19:46:03 +00:00
Ok(serde_json::from_str(&val)?)
}
Err(_) => {
2020-06-08 21:11:43 +00:00
let matches = RgaConfig::from_iter(args);
2019-06-07 19:46:03 +00:00
let serialized_config = serde_json::to_string(&matches)?;
std::env::set_var(RGA_CONFIG, &serialized_config);
debug!("{}={}", RGA_CONFIG, serialized_config);
Ok(matches)
}
}
}
2019-06-11 11:43:01 +00:00
/// Split arguments into the ones we care about and the ones rg cares about
2020-06-08 21:11:43 +00:00
pub fn split_args() -> Result<(RgaConfig, Vec<OsString>)> {
let mut app = RgaConfig::clap();
2019-06-11 11:43:01 +00:00
app.p.create_help_and_version();
let mut firstarg = true;
// debug!("{:#?}", app.p.flags);
let (our_args, mut passthrough_args): (Vec<OsString>, Vec<OsString>) = std::env::args_os()
.partition(|os_arg| {
if firstarg {
// hacky, but .enumerate() would be ugly because partition is too simplistic
firstarg = false;
return true;
}
if let Some(arg) = os_arg.to_str() {
arg.starts_with("--rga-")
|| arg.starts_with("--rg-")
|| arg == "--help"
|| arg == "-h"
|| arg == "--version"
|| arg == "-V"
} else {
// args that are not unicode can only be filenames, pass them to rg
false
}
});
debug!("our_args: {:?}", our_args);
let matches = parse_args(our_args)?;
if matches.rg_help {
passthrough_args.insert(0, "--help".into());
}
if matches.rg_version {
passthrough_args.insert(0, "--version".into());
}
debug!("passthrough_args: {:?}", passthrough_args);
Ok((matches, passthrough_args))
}