| 1 |
//! Configuration model inspection and manipulation. |
| 2 |
|
| 3 |
use std::collections::BTreeMap; |
| 4 |
|
| 5 |
use anyhow::{Context, Result}; |
| 6 |
|
| 7 |
use toml_edit::{ |
| 8 |
Item, |
| 9 |
Value, |
| 10 |
}; |
| 11 |
|
| 12 |
use crate::toml_edit_tree::{ |
| 13 |
Error, |
| 14 |
Node, |
| 15 |
Path, |
| 16 |
PathComponent, |
| 17 |
TraversalError, |
| 18 |
}; |
| 19 |
|
| 20 |
use crate::{ |
| 21 |
Sq, |
| 22 |
cli::config, |
| 23 |
config::ConfigFile, |
| 24 |
}; |
| 25 |
|
| 26 |
mod inspect; |
| 27 |
|
| 28 |
pub fn dispatch(sq: Sq, cmd: config::Command) |
| 29 |
-> Result<()> |
| 30 |
{ |
| 31 |
match cmd.subcommand { |
| 32 |
config::Subcommands::Get(c) => get(sq, c), |
| 33 |
config::Subcommands::Template(c) => template(sq, c), |
| 34 |
config::Subcommands::Inspect(c) => inspect::dispatch(sq, c), |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/// Implements `sq config get`. |
| 39 |
fn get(sq: Sq, cmd: config::get::Command) -> Result<()> { |
| 40 |
let path = if let Some(name) = &cmd.name { |
| 41 |
name.parse()? |
| 42 |
} else { |
| 43 |
Path::empty() |
| 44 |
}; |
| 45 |
|
| 46 |
// We do two lookups, first in the configuration file, then in the |
| 47 |
// default configuration, and collate the results. |
| 48 |
let mut acc = Default::default(); |
| 49 |
|
| 50 |
// First, look in the configuration. |
| 51 |
let config = sq.config_file.effective_configuration(&sq)?; |
| 52 |
let r0 = Node::traverse(&*config.as_item() as _, &path) |
| 53 |
.map_err(Into::into) |
| 54 |
.and_then( |
| 55 |
|node| collect(&mut acc, path.clone(), node, &|_, _| true)); |
| 56 |
|
| 57 |
// Then, look in the default configuration. But, we are careful |
| 58 |
// to filter out anything overridden in the actual configuration. |
| 59 |
let default = ConfigFile::default_config(sq.home.as_ref())?; |
| 60 |
let r1 = Node::traverse(&*default.as_item() as _, &path) |
| 61 |
.map_err(Into::into) |
| 62 |
.and_then( |
| 63 |
|node| collect(&mut acc, path, node, &|p, n| { |
| 64 |
// Is this a leaf node? |
| 65 |
let leaf = n.as_atomic_value().is_some() |
| 66 |
|| n.as_array().is_some(); |
| 67 |
|
| 68 |
// Is this set in the config file? |
| 69 |
let is_configured = |
| 70 |
config.as_item().traverse(p).is_ok(); |
| 71 |
|
| 72 |
// Show or continue traversing if this is either an |
| 73 |
// intermediate node, or the node is absent in the |
| 74 |
// configuration. |
| 75 |
! leaf || ! is_configured |
| 76 |
})); |
| 77 |
|
| 78 |
// One of the lookups must be successful. |
| 79 |
r0.or(r1)?; |
| 80 |
|
| 81 |
// Display sorted and deduplicated results. |
| 82 |
for (k, v) in acc { |
| 83 |
println!("{} = {}", k, v); |
| 84 |
} |
| 85 |
|
| 86 |
Ok(()) |
| 87 |
} |
| 88 |
|
| 89 |
/// Collects all nodes below `node` at `path` matching `filter` into |
| 90 |
/// `acc`. |
| 91 |
pub fn collect<F>(acc: &mut BTreeMap<String, String>, |
| 92 |
mut path: Path, node: &dyn Node, filter: &F) |
| 93 |
-> Result<Path> |
| 94 |
where |
| 95 |
F: for<'a> Fn(&'a Path, &'a dyn Node) -> bool, |
| 96 |
{ |
| 97 |
if ! (filter)(&path, node) { |
| 98 |
return Ok(path); |
| 99 |
} |
| 100 |
|
| 101 |
if let Some(v) = node.as_atomic_value() { |
| 102 |
acc.insert(path.to_string(), |
| 103 |
v.clone().decorated("", "").to_string()); |
| 104 |
} else { |
| 105 |
for (k, v) in node.iter() { |
| 106 |
path.push(k); |
| 107 |
path = collect(acc, path, v, filter)?; |
| 108 |
path.pop(); |
| 109 |
} |
| 110 |
} |
| 111 |
Ok(path) |
| 112 |
} |
| 113 |
|
| 114 |
/// Implements `sq config set`. |
| 115 |
/// |
| 116 |
/// XXX: Currently, we don't expose this due to problems with |
| 117 |
/// toml-edit. Notably, toml-edit doesn't handle comments well, |
| 118 |
/// attaching them as decor to nodes in the document tree. |
| 119 |
/// Manipulating the document tree may delete or otherwise disturb the |
| 120 |
/// comments in a way that badly distorts the semantics. |
| 121 |
#[allow(dead_code)] |
| 122 |
fn set(mut sq: Sq, cmd: config::set::Command) -> Result<()> { |
| 123 |
let mut path: Path = cmd.name.parse()?; |
| 124 |
if path.is_empty() { |
| 125 |
return Err(anyhow::anyhow!("NAME must not be empty")); |
| 126 |
}; |
| 127 |
let last = path.pop().expect("path is not empty"); |
| 128 |
|
| 129 |
// XXX: Workaround for clap bug, see src/cli/config.rs. |
| 130 |
if cmd.value.is_none() && ! cmd.delete { |
| 131 |
return Err(anyhow::anyhow!("Either VALUE or --delete must be given")); |
| 132 |
} |
| 133 |
|
| 134 |
let mut config = std::mem::take(&mut sq.config_file); |
| 135 |
let doc = config.as_item_mut(); |
| 136 |
if let Some(value) = &cmd.value { |
| 137 |
let value: Value = value.parse().unwrap_or_else(|_| value.into()); |
| 138 |
|
| 139 |
// Like Node::traverse_mut, but we also create intermediate |
| 140 |
// nodes on demand. |
| 141 |
let mut node: &mut dyn Node = doc as _; |
| 142 |
|
| 143 |
for (i, pc) in path.iter().cloned().enumerate() { |
| 144 |
let type_name = node.type_name(); |
| 145 |
if let Err(TraversalError::KeyNotFound(_, _)) = |
| 146 |
node.get_mut(&pc) |
| 147 |
.map_err(|e| e.with_context(&path, i, type_name)) |
| 148 |
{ |
| 149 |
match path.get(i + 1).unwrap_or(&last) { |
| 150 |
PathComponent::Symbol(_) => { |
| 151 |
// Prefer to insert a non-inline table if |
| 152 |
// possible. |
| 153 |
if let Some(t) = node.as_table_mut() { |
| 154 |
t.insert(pc.as_symbol()?, |
| 155 |
Item::Table(Default::default())); |
| 156 |
} else { |
| 157 |
node.set(&pc, |
| 158 |
Value::InlineTable(Default::default()))?; |
| 159 |
} |
| 160 |
}, |
| 161 |
PathComponent::Index(_) => |
| 162 |
node.set(&pc, Value::Array(Default::default()))?, |
| 163 |
}; |
| 164 |
} |
| 165 |
|
| 166 |
node = node.get_mut(&pc) |
| 167 |
.map_err(|e| e.with_context(&path, i, type_name))?; |
| 168 |
} |
| 169 |
|
| 170 |
if cmd.add { |
| 171 |
if let Err(Error::KeyNotFound(_)) = node.get_mut(&last) { |
| 172 |
// The node doesn't exist, see if it exists in the |
| 173 |
// default configuration. |
| 174 |
let default = ConfigFile::default_config(sq.home.as_ref())?; |
| 175 |
let v = Node::traverse(&*default.as_item() as _, &path).ok() |
| 176 |
.and_then(|n| n.get(&last).ok()) |
| 177 |
.and_then(|n| n.as_array()) |
| 178 |
.map(|a| Value::Array(a.clone())) |
| 179 |
.unwrap_or(Value::Array(Default::default())); |
| 180 |
|
| 181 |
node.set(&last, v)?; |
| 182 |
} |
| 183 |
|
| 184 |
let type_name = node.type_name(); |
| 185 |
node = node.get_mut(&last) |
| 186 |
.map_err(|e| e.with_context(&path, path.len(), type_name))?; |
| 187 |
|
| 188 |
let type_name = node.type_name(); |
| 189 |
path.push(last); |
| 190 |
if let Some(a) = node.as_array_mut() { |
| 191 |
a.push(value); |
| 192 |
} else { |
| 193 |
return Err(anyhow::anyhow!("Tried to add an element to {}, \ |
| 194 |
but this is a {} not an array", |
| 195 |
path, type_name)); |
| 196 |
} |
| 197 |
} else { |
| 198 |
node.set(&last, value)?; |
| 199 |
} |
| 200 |
} else { |
| 201 |
assert!(cmd.delete); |
| 202 |
let node = Node::traverse_mut(&mut *doc as _, &path)?; |
| 203 |
node.remove(&last)?; |
| 204 |
} |
| 205 |
|
| 206 |
// Verify the configuration. |
| 207 |
config.verify() |
| 208 |
.with_context(|| format!("Failed to {} {:?}", |
| 209 |
if cmd.delete { "delete" } else { "set" }, |
| 210 |
cmd.name))?; |
| 211 |
|
| 212 |
// The updated config verified, now persist it. |
| 213 |
config.persist( |
| 214 |
sq.home.as_ref().ok_or(anyhow::anyhow!("No home directory given"))?)?; |
| 215 |
|
| 216 |
Ok(()) |
| 217 |
} |
| 218 |
|
| 219 |
/// Implements `sq config template`. |
| 220 |
fn template(sq: Sq, cmd: config::template::Command) -> Result<()> { |
| 221 |
let mut sink = cmd.output.create_safe(&sq)?; |
| 222 |
ConfigFile::default_template(sq.home.as_ref())? |
| 223 |
.dump(&mut sink)?; |
| 224 |
|
| 225 |
Ok(()) |
| 226 |
} |