Merge pull request #909 from jonathandturner/config_set_into
Add support for config --set_into
This commit is contained in:
commit
503e521820
|
@ -4,7 +4,6 @@ use crate::errors::ShellError;
|
||||||
use crate::parser::hir::SyntaxShape;
|
use crate::parser::hir::SyntaxShape;
|
||||||
use crate::parser::registry::{self};
|
use crate::parser::registry::{self};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use std::iter::FromIterator;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub struct Config;
|
pub struct Config;
|
||||||
|
@ -13,6 +12,7 @@ pub struct Config;
|
||||||
pub struct ConfigArgs {
|
pub struct ConfigArgs {
|
||||||
load: Option<Tagged<PathBuf>>,
|
load: Option<Tagged<PathBuf>>,
|
||||||
set: Option<(Tagged<String>, Tagged<Value>)>,
|
set: Option<(Tagged<String>, Tagged<Value>)>,
|
||||||
|
set_into: Option<Tagged<String>>,
|
||||||
get: Option<Tagged<String>>,
|
get: Option<Tagged<String>>,
|
||||||
clear: Tagged<bool>,
|
clear: Tagged<bool>,
|
||||||
remove: Option<Tagged<String>>,
|
remove: Option<Tagged<String>>,
|
||||||
|
@ -31,7 +31,16 @@ impl WholeStreamCommand for Config {
|
||||||
SyntaxShape::Path,
|
SyntaxShape::Path,
|
||||||
"load the config from the path give",
|
"load the config from the path give",
|
||||||
)
|
)
|
||||||
.named("set", SyntaxShape::Any, "set a value in the config")
|
.named(
|
||||||
|
"set",
|
||||||
|
SyntaxShape::Any,
|
||||||
|
"set a value in the config, eg) --set [key value]",
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"set_into",
|
||||||
|
SyntaxShape::Member,
|
||||||
|
"sets a variable from values in the pipeline",
|
||||||
|
)
|
||||||
.named("get", SyntaxShape::Any, "get a value from the config")
|
.named("get", SyntaxShape::Any, "get a value from the config")
|
||||||
.named("remove", SyntaxShape::Any, "remove a value from the config")
|
.named("remove", SyntaxShape::Any, "remove a value from the config")
|
||||||
.switch("clear", "clear the config")
|
.switch("clear", "clear the config")
|
||||||
|
@ -55,85 +64,110 @@ pub fn config(
|
||||||
ConfigArgs {
|
ConfigArgs {
|
||||||
load,
|
load,
|
||||||
set,
|
set,
|
||||||
|
set_into,
|
||||||
get,
|
get,
|
||||||
clear,
|
clear,
|
||||||
remove,
|
remove,
|
||||||
path,
|
path,
|
||||||
}: ConfigArgs,
|
}: ConfigArgs,
|
||||||
RunnableContext { name, .. }: RunnableContext,
|
RunnableContext { name, input, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let name_span = name.clone();
|
let name_span = name.clone();
|
||||||
|
|
||||||
let configuration = if let Some(supplied) = load {
|
let stream = async_stream! {
|
||||||
Some(supplied.item().clone())
|
let configuration = if let Some(supplied) = load {
|
||||||
} else {
|
Some(supplied.item().clone())
|
||||||
None
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut result = crate::data::config::read(name_span, &configuration)?;
|
||||||
|
|
||||||
|
if let Some(v) = get {
|
||||||
|
let key = v.to_string();
|
||||||
|
let value = result
|
||||||
|
.get(&key)
|
||||||
|
.ok_or_else(|| ShellError::labeled_error("Missing key in config", "key", v.tag()))?;
|
||||||
|
|
||||||
|
match value {
|
||||||
|
Tagged {
|
||||||
|
item: Value::Table(list),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
for l in list {
|
||||||
|
yield ReturnSuccess::value(l.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x => yield ReturnSuccess::value(x.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if let Some((key, value)) = set {
|
||||||
|
result.insert(key.to_string(), value.clone());
|
||||||
|
|
||||||
|
config::write(&result, &configuration)?;
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(value.tag()));
|
||||||
|
}
|
||||||
|
else if let Some(v) = set_into {
|
||||||
|
let rows: Vec<Tagged<Value>> = input.values.collect().await;
|
||||||
|
let key = v.to_string();
|
||||||
|
|
||||||
|
if rows.len() == 0 {
|
||||||
|
yield Err(ShellError::labeled_error("No values given for set_into", "needs value(s) from pipeline", v.tag()));
|
||||||
|
} else if rows.len() == 1 {
|
||||||
|
// A single value
|
||||||
|
let value = &rows[0];
|
||||||
|
|
||||||
|
result.insert(key.to_string(), value.clone());
|
||||||
|
|
||||||
|
config::write(&result, &configuration)?;
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(name));
|
||||||
|
} else {
|
||||||
|
// Take in the pipeline as a table
|
||||||
|
let value = Value::Table(rows).tagged(name.clone());
|
||||||
|
|
||||||
|
result.insert(key.to_string(), value.clone());
|
||||||
|
|
||||||
|
config::write(&result, &configuration)?;
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if let Tagged { item: true, tag } = clear {
|
||||||
|
result.clear();
|
||||||
|
|
||||||
|
config::write(&result, &configuration)?;
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(tag));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if let Tagged { item: true, tag } = path {
|
||||||
|
let path = config::default_path_for(&configuration)?;
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(Value::Primitive(Primitive::Path(path)).tagged(tag));
|
||||||
|
}
|
||||||
|
else if let Some(v) = remove {
|
||||||
|
let key = v.to_string();
|
||||||
|
|
||||||
|
if result.contains_key(&key) {
|
||||||
|
result.swap_remove(&key);
|
||||||
|
config::write(&result, &configuration).unwrap();
|
||||||
|
} else {
|
||||||
|
yield Err(ShellError::labeled_error(
|
||||||
|
"Key does not exist in config",
|
||||||
|
"key",
|
||||||
|
v.tag(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(v.tag()));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut result = crate::data::config::read(name_span, &configuration)?;
|
Ok(stream.to_output_stream())
|
||||||
|
|
||||||
if let Some(v) = get {
|
|
||||||
let key = v.to_string();
|
|
||||||
let value = result
|
|
||||||
.get(&key)
|
|
||||||
.ok_or_else(|| ShellError::labeled_error("Missing key in config", "key", v.tag()))?;
|
|
||||||
|
|
||||||
let mut results = VecDeque::new();
|
|
||||||
|
|
||||||
match value {
|
|
||||||
Tagged {
|
|
||||||
item: Value::Table(list),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
for l in list {
|
|
||||||
results.push_back(ReturnSuccess::value(l.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
x => results.push_back(ReturnSuccess::value(x.clone())),
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(results.to_output_stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some((key, value)) = set {
|
|
||||||
result.insert(key.to_string(), value.clone());
|
|
||||||
|
|
||||||
config::write(&result, &configuration)?;
|
|
||||||
|
|
||||||
return Ok(stream![Value::Row(result.into()).tagged(value.tag())].from_input_stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Tagged { item: true, tag } = clear {
|
|
||||||
result.clear();
|
|
||||||
|
|
||||||
config::write(&result, &configuration)?;
|
|
||||||
|
|
||||||
return Ok(stream![Value::Row(result.into()).tagged(tag)].from_input_stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Tagged { item: true, tag } = path {
|
|
||||||
let path = config::default_path_for(&configuration)?;
|
|
||||||
|
|
||||||
return Ok(stream![Value::Primitive(Primitive::Path(path)).tagged(tag)].from_input_stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(v) = remove {
|
|
||||||
let key = v.to_string();
|
|
||||||
|
|
||||||
if result.contains_key(&key) {
|
|
||||||
result.swap_remove(&key);
|
|
||||||
config::write(&result, &configuration)?;
|
|
||||||
} else {
|
|
||||||
return Err(ShellError::labeled_error(
|
|
||||||
"Key does not exist in config",
|
|
||||||
"key",
|
|
||||||
v.tag(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let obj = VecDeque::from_iter(vec![Value::Row(result.into()).tagged(v.tag())]);
|
|
||||||
return Ok(obj.from_input_stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(vec![Value::Row(result.into()).tagged(name)].into());
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user