use std::collections::HashMap;
use std::{fmt, fs, path::PathBuf};
use etcetera::{choose_base_strategy, BaseStrategy};
use serde::Deserialize;
pub fn get_config() -> color_eyre::Result<ConfigFile> {
let config_file = config_dir().join("config.toml");
let mut config = if config_file.exists() {
parse_config_file(&config_file)?
} else {
ConfigFile::default()
};
let ws_dir = find_workspace();
let ws_config_file1 = ws_dir.join("jlf.toml");
let ws_config_file2 = ws_dir.join(".jlf.toml");
let ws_config = if ws_config_file1.exists() {
Some(parse_config_file(&ws_config_file1)?)
} else if ws_config_file2.exists() {
Some(parse_config_file(&ws_config_file2)?)
} else {
None
};
if let Some(ws_config) = ws_config {
config.merge(ws_config);
}
Ok(config)
}
fn parse_config_file(path: &std::path::Path) -> color_eyre::Result<ConfigFile> {
let raw = fs::read_to_string(path)
.map_err(|e| color_eyre::eyre::eyre!("failed to read config `{}`: {e}", path.display()))?;
toml::from_str(&raw).map_err(|e| {
color_eyre::eyre::eyre!("invalid config `{}`:\n\n{e}", path.display())
})
}
enum Content<'a> {
Template(&'a str),
Field { path: &'a str, mods: Option<&'a str> },
Columns(Vec<&'a str>),
}
fn is_field_path(s: &str) -> bool {
!s.is_empty()
&& s.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b'|' | b'-'))
}
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Recipe {
pub out: Option<String>,
pub filter: Option<String>,
pub redact: Option<String>,
pub format: Option<String>,
pub escape: Option<String>,
pub count: Option<String>,
pub stats: Option<String>,
pub top: Option<String>,
pub uniq: Option<String>,
pub by: Option<String>,
pub n: Option<usize>,
pub base: Option<String>,
#[serde(default)]
pub compact: Option<Box<Recipe>>,
#[serde(default)]
pub no_color: Option<Box<Recipe>>,
#[serde(default)]
pub strict: Option<Box<Recipe>>,
}
impl Recipe {
fn content(&self) -> Option<Content<'_>> {
let raw = self.out.as_deref()?;
let trimmed = raw.trim();
if raw.contains('$') || trimmed.is_empty() {
return Some(Content::Template(raw));
}
let colon = trimmed.find(':');
let comma = trimmed.find(',');
Some(match (colon, comma) {
(Some(c), maybe) if maybe.is_none_or(|m| c < m) && is_field_path(trimmed[..c].trim()) => {
Content::Field {
path: trimmed[..c].trim(),
mods: Some(trimmed[c + 1..].trim()),
}
}
(_, Some(_)) => Content::Columns(
trimmed
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.collect(),
),
_ if is_field_path(trimmed) => Content::Field {
path: trimmed,
mods: None,
},
_ => Content::Template(raw),
})
}
fn inline_body(&self) -> Option<String> {
Some(match self.content()? {
Content::Template(t) => t.to_owned(),
Content::Field { path, mods } => match mods {
Some(m) => format!("${{?{path}:{m}}}"),
None => format!("${{?{path}}}"),
},
Content::Columns(cols) => cols
.iter()
.map(|c| format!("${{?{c}}}"))
.collect::<Vec<_>>()
.join(" "),
})
}
fn accessor(&self) -> Option<String> {
match self.content()? {
Content::Field { path, .. } => Some(path.to_owned()),
_ => None,
}
}
fn preset_template(&self) -> Option<String> {
if self.is_format() {
return None;
}
match self.content()? {
Content::Template(t) => Some(t.to_owned()),
Content::Field { path, mods } => Some(match mods {
Some(m) => format!("${{{path}:{m}}}"),
None => format!("${{{path}}}"),
}),
Content::Columns(_) => None,
}
}
fn preset_columns(&self) -> Option<String> {
match self.content()? {
Content::Columns(cols) => Some(cols.join(",")),
_ => None,
}
}
fn overlay(&mut self, other: &Recipe) {
macro_rules! take {
($($f:ident),*) => { $( if other.$f.is_some() { self.$f = other.$f.clone(); } )* };
}
take!(out, filter, redact, format,
escape, count, stats, top, uniq, by, n, base);
}
fn override_for(&self, flag: &str) -> Option<&Recipe> {
match flag {
"compact" => self.compact.as_deref(),
"no_color" => self.no_color.as_deref(),
"strict" => self.strict.as_deref(),
_ => None,
}
}
fn is_format(&self) -> bool {
self.escape.is_some()
|| self
.out
.as_deref()
.is_some_and(|b| b.contains("$rows(") || b.contains("$cols("))
}
}
#[derive(Debug, Default, Deserialize)]
pub struct ConfigFile {
#[serde(default)]
pub config: Config,
#[serde(default, deserialize_with = "de_map_to_list")]
pub variables: Option<Vec<(String, String)>>,
#[serde(default, rename = "format")]
pub formats: HashMap<String, FormatDef>,
#[serde(default, rename = "preset")]
pub presets: HashMap<String, PresetDef>,
#[serde(default)]
pub recipes: HashMap<String, String>,
#[serde(default, rename = "recipe")]
pub recipe: HashMap<String, Recipe>,
#[serde(skip)]
pub field_aliases: HashMap<String, String>,
}
pub fn builtin_formats() -> HashMap<String, FormatDef> {
HashMap::from([
(
"csv".to_owned(),
FormatDef {
escape: None,
body: "$cols( $key ),*\n$rows( $cols( ${value:csv} ),* )*".to_owned(),
},
),
(
"tsv".to_owned(),
FormatDef {
escape: None,
body: "$cols( $key )\t*\n$rows( $cols( ${value:tsv} )\t* )*".to_owned(),
},
),
(
"md".to_owned(),
FormatDef {
escape: None,
body: "| $cols( $key )\" | \"* |\n| $cols( --- )\" | \"* |\n$rows( | $cols( ${value:md} )\" | \"* | )*".to_owned(),
},
),
])
}
#[derive(Debug, Default, Clone, Deserialize)]
pub struct FormatDef {
pub escape: Option<String>,
pub body: String,
}
#[derive(Debug, Default, Clone, Deserialize)]
pub struct PresetDef {
#[serde(rename = "where")]
pub filter: Option<String>,
pub template: Option<String>,
pub fields: Option<String>,
pub redact: Option<String>,
pub compact: Option<bool>,
pub format: Option<String>,
pub count: Option<String>,
pub stats: Option<String>,
pub top: Option<String>,
pub uniq: Option<String>,
pub by: Option<String>,
pub n: Option<usize>,
}
pub fn default_variables() -> Vec<(String, String)> {
[
(
"output",
"${@timestamp}${@level}${@message}$config(compact => )$else(\\n)${@data}",
),
("timestamp", "${?timestamp:dimmed} "),
(
"level",
concat!(
"$match(lvl|level|severity\n",
r#" $when("ERROR"|"error" => ${value:fg=red})"#,
"\n",
r#" $when("WARN"|"warn" => ${value:fg=yellow})"#,
"\n",
r#" $when("INFO"|"info" => ${value:fg=cyan})"#,
"\n",
r#" $when("DEBUG"|"debug" => ${value:fg=green})"#,
"\n",
r#" $when("TRACE"|"trace" => ${value:fg=cyan,dimmed})"#,
"\n",
r#" $else(${value})"#,
"\n) ",
),
),
("message", "${?message|msg|body|fields.message}"),
("data", "${?..:json}"),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect()
}
fn de_map_to_list<'de, D>(de: D) -> Result<Option<Vec<(String, String)>>, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = Option<Vec<(String, String)>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "A hex encoded OpId")
}
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_map(Visitor)
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(None)
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut list = map.size_hint().map(Vec::with_capacity).unwrap_or_default();
while let Some((k, v)) = map.next_entry()? {
list.push((k, v));
}
Ok(Some(list))
}
}
de.deserialize_any(Visitor)
}
#[derive(Debug, Default, Deserialize)]
pub struct Config {
pub format: Option<String>,
pub compact: Option<bool>,
pub no_color: Option<bool>,
pub strict: Option<bool>,
}
impl ConfigFile {
fn merge(&mut self, other: Self) {
let Self {
config: config2,
variables: variables2,
formats: formats2,
presets: presets2,
recipes: recipes2,
recipe: recipe2,
field_aliases: _,
} = other;
if let Some(format) = config2.format {
self.config.format = Some(format);
}
if let Some(compact) = config2.compact {
self.config.compact = Some(compact);
}
if let Some(no_color) = config2.no_color {
self.config.no_color = Some(no_color);
}
if let Some(strict) = config2.strict {
self.config.strict = Some(strict);
}
self.formats.extend(formats2);
self.presets.extend(presets2);
self.recipes.extend(recipes2);
self.recipe.extend(recipe2);
match (&mut self.variables, variables2) {
(_, None) => (),
(v1, Some(v2)) => {
if let Some(v1) = v1 {
for (k2, v2) in v2 {
let v = v1.iter_mut().find_map(|(k, v)| (k == &k2).then_some(v));
if let Some(v) = v {
*v = v2;
} else {
v1.push((k2, v2));
}
}
} else {
*v1 = Some(v2);
}
}
}
}
pub fn resolve_recipes(&mut self, active: &[&str]) {
for (name, def) in builtin_formats() {
self.formats.entry(name.clone()).or_insert(def);
self.presets.entry(name.clone()).or_insert_with(|| PresetDef {
format: Some(name),
..Default::default()
});
}
let shorthand: Vec<(String, String)> = self.recipes.drain().collect();
for (name, body) in shorthand {
self.set_variable(name.clone(), body.clone());
self.presets
.entry(name)
.or_insert_with(|| PresetDef { template: Some(body), ..Default::default() });
}
let recipes: Vec<(String, Recipe)> = self.recipe.clone().into_iter().collect();
for (name, _) in &recipes {
let resolved = self.resolve_one(name, active, &mut Vec::new());
self.install_recipe(name, &resolved);
}
self.recipe.clear();
}
fn resolve_one(&self, name: &str, active: &[&str], stack: &mut Vec<String>) -> Recipe {
let Some(raw) = self.recipe.get(name) else {
return Recipe::default();
};
if stack.iter().any(|n| n == name) {
return raw.clone(); }
stack.push(name.to_owned());
let mut resolved = Recipe::default();
if let Some(base) = &raw.base {
let base_name = base.strip_prefix('@').unwrap_or(base);
resolved = self.resolve_one(base_name, active, stack);
}
resolved.overlay(raw);
for cond in active {
if let Some(ov) = raw.override_for(cond) {
resolved.overlay(ov);
}
}
stack.pop();
resolved
}
fn install_recipe(&mut self, name: &str, r: &Recipe) {
if let Some(accessor) = r.accessor() {
self.field_aliases.insert(name.to_owned(), accessor);
}
if let Some(body) = r.inline_body() {
self.set_variable(name.to_owned(), body);
}
if r.is_format() {
self.formats.insert(
name.to_owned(),
FormatDef {
escape: r.escape.clone(),
body: r.inline_body().unwrap_or_default(),
},
);
}
let preset = PresetDef {
filter: r.filter.clone(),
template: r.preset_template(),
fields: r.preset_columns(),
redact: r.redact.clone(),
compact: None,
format: r
.format
.clone()
.or_else(|| r.is_format().then(|| name.to_owned())),
count: r.count.clone(),
stats: r.stats.clone(),
top: r.top.clone(),
uniq: r.uniq.clone(),
by: r.by.clone(),
n: r.n,
};
self.presets.insert(name.to_owned(), preset);
}
fn set_variable(&mut self, key: String, value: String) {
let vars = self.variables.get_or_insert_with(Vec::new);
match vars.iter_mut().find(|(k, _)| *k == key) {
Some((_, v)) => *v = value,
None => vars.push((key, value)),
}
}
}
fn config_dir() -> PathBuf {
let strategy = choose_base_strategy().expect("Unable to find the config directory!");
let mut path = strategy.config_dir();
path.push("jlf");
path
}
fn find_workspace() -> PathBuf {
let current_dir = current_working_dir();
for ancestor in current_dir.ancestors() {
if ancestor.join(".git").exists()
|| ancestor.join("jlf.toml").exists()
|| ancestor.join(".jlf.toml").exists()
|| ancestor.join(".svn").exists()
|| ancestor.join(".jj").exists()
{
return ancestor.to_owned();
}
}
current_dir
}
fn current_working_dir() -> PathBuf {
let mut cwd = std::env::current_dir().expect("Couldn't determine current working directory");
let pwd = std::env::var_os("PWD");
#[cfg(windows)]
let pwd = pwd.or_else(|| std::env::var_os("CD"));
if let Some(pwd) = pwd.map(PathBuf::from) {
if pwd.canonicalize().ok().as_ref() == Some(&cwd) {
cwd = pwd;
}
}
cwd
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(s: &str) -> ConfigFile {
toml::from_str(s).unwrap()
}
fn var<'a>(c: &'a ConfigFile, name: &str) -> Option<&'a str> {
c.variables
.as_ref()?
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| v.as_str())
}
#[test]
fn shorthand_recipe_becomes_variable_and_preset() {
let mut c = parse("[recipes]\noneline = \"${level} ${msg}\"\n");
c.resolve_recipes(&[]);
assert_eq!(var(&c, "oneline"), Some("${level} ${msg}"));
assert_eq!(c.presets["oneline"].template.as_deref(), Some("${level} ${msg}"));
}
#[test]
fn single_field_out_becomes_value_variable() {
let mut c = parse("[recipe.host]\nout = \"host|hostname:dimmed\"\n");
c.resolve_recipes(&[]);
assert_eq!(var(&c, "host"), Some("${?host|hostname:dimmed}"));
}
#[test]
fn out_key_classified_by_shape() {
let mut field = parse("[recipe.ts]\nout = \"timestamp|ts:dimmed,red\"\n");
field.resolve_recipes(&[]);
assert_eq!(var(&field, "ts"), Some("${?timestamp|ts:dimmed,red}"));
assert_eq!(field.field_aliases.get("ts").map(String::as_str), Some("timestamp|ts"));
assert_eq!(field.presets["ts"].template.as_deref(), Some("${timestamp|ts:dimmed,red}"));
let mut cols = parse("[recipe.req]\nout = \"timestamp,level,message\"\n");
cols.resolve_recipes(&[]);
assert_eq!(var(&cols, "req"), Some("${?timestamp} ${?level} ${?message}"));
assert_eq!(cols.presets["req"].fields.as_deref(), Some("timestamp,level,message"));
assert_eq!(cols.presets["req"].template, None);
assert!(!cols.field_aliases.contains_key("req"));
let mut tpl = parse("[recipe.lvl]\nout = \"$match(level $else(${value}))\"\n");
tpl.resolve_recipes(&[]);
assert!(!tpl.field_aliases.contains_key("lvl"));
assert_eq!(tpl.presets["lvl"].template.as_deref(), Some("$match(level $else(${value}))"));
}
#[test]
fn filter_body_recipe_becomes_preset() {
let mut c = parse(
"[recipe.errors]\nfilter = \"level=error\"\nout = \"${ts} ${msg}\"\n",
);
c.resolve_recipes(&[]);
let p = &c.presets["errors"];
assert_eq!(p.filter.as_deref(), Some("level=error"));
assert_eq!(p.template.as_deref(), Some("${ts} ${msg}"));
}
#[test]
fn format_recipe_becomes_format() {
let mut c = parse(
"[recipe.report]\nescape = \"html\"\nout = \"<table>\\n$rows( <r>${msg}</r> )*</table>\"\n",
);
c.resolve_recipes(&[]);
let f = &c.formats["report"];
assert_eq!(f.escape.as_deref(), Some("html"));
assert_eq!(f.body, "<table>\n$rows( <r>${msg}</r> )*</table>");
assert_eq!(c.presets["report"].format.as_deref(), Some("report"));
}
#[test]
fn conditional_override_applies_when_active() {
let toml = "[recipe.sep]\nout = \"\\n\"\n[recipe.sep.compact]\nout = \" \"\n";
let mut normal = parse(toml);
normal.resolve_recipes(&[]);
assert_eq!(var(&normal, "sep"), Some("\n"));
let mut compact = parse(toml);
compact.resolve_recipes(&["compact"]);
assert_eq!(var(&compact, "sep"), Some(" "));
}
#[test]
fn base_inheritance_overlays_keys() {
let mut c = parse(
"[recipe.base]\nout = \"${a}\"\nfilter = \"x=1\"\n[recipe.child]\nbase = \"@base\"\nout = \"${b}\"\n",
);
c.resolve_recipes(&[]);
assert_eq!(c.presets["child"].template.as_deref(), Some("${b}"));
assert_eq!(c.presets["child"].filter.as_deref(), Some("x=1"));
}
}