2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 18:23:42 +02:00
|
|
|
use crate::data::{config, Value};
|
2019-09-09 19:10:52 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-09-14 18:30:24 +02:00
|
|
|
use crate::parser::hir::SyntaxShape;
|
2019-08-02 21:15:07 +02:00
|
|
|
use crate::parser::registry::{self};
|
2019-09-10 12:08:01 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
use std::path::PathBuf;
|
2019-06-01 07:50:16 +02:00
|
|
|
|
|
|
|
pub struct Config;
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ConfigArgs {
|
2019-09-10 12:08:01 +02:00
|
|
|
load: Option<Tagged<PathBuf>>,
|
2019-08-09 06:51:21 +02:00
|
|
|
set: Option<(Tagged<String>, Tagged<Value>)>,
|
2019-11-03 00:43:15 +01:00
|
|
|
set_into: Option<Tagged<String>>,
|
2019-08-09 06:51:21 +02:00
|
|
|
get: Option<Tagged<String>>,
|
|
|
|
clear: Tagged<bool>,
|
|
|
|
remove: Option<Tagged<String>>,
|
|
|
|
path: Tagged<bool>,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl WholeStreamCommand for Config {
|
2019-06-01 07:50:16 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"config"
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("config")
|
2019-10-28 06:15:35 +01:00
|
|
|
.named(
|
|
|
|
"load",
|
|
|
|
SyntaxShape::Path,
|
|
|
|
"load the config from the path give",
|
|
|
|
)
|
2019-11-03 00:43:15 +01:00
|
|
|
.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",
|
|
|
|
)
|
2019-10-28 06:15:35 +01:00
|
|
|
.named("get", SyntaxShape::Any, "get a value from the config")
|
|
|
|
.named("remove", SyntaxShape::Any, "remove a value from the config")
|
|
|
|
.switch("clear", "clear the config")
|
|
|
|
.switch("path", "return the path to the config file")
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Configuration management."
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, config)?.run()
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn config(
|
|
|
|
ConfigArgs {
|
2019-09-10 12:08:01 +02:00
|
|
|
load,
|
2019-08-02 21:15:07 +02:00
|
|
|
set,
|
2019-11-03 00:43:15 +01:00
|
|
|
set_into,
|
2019-08-02 21:15:07 +02:00
|
|
|
get,
|
|
|
|
clear,
|
|
|
|
remove,
|
|
|
|
path,
|
|
|
|
}: ConfigArgs,
|
2019-11-03 00:43:15 +01:00
|
|
|
RunnableContext { name, input, .. }: RunnableContext,
|
2019-08-02 21:15:07 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-10-13 06:12:43 +02:00
|
|
|
let name_span = name.clone();
|
2019-09-10 12:08:01 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
let stream = async_stream! {
|
|
|
|
let configuration = if let Some(supplied) = load {
|
|
|
|
Some(supplied.item().clone())
|
|
|
|
} 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());
|
|
|
|
}
|
2019-09-10 12:08:01 +02:00
|
|
|
}
|
2019-11-03 00:22:30 +01:00
|
|
|
x => yield ReturnSuccess::value(x.clone()),
|
2019-09-10 12:08:01 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 00:22:30 +01:00
|
|
|
else if let Some((key, value)) = set {
|
|
|
|
result.insert(key.to_string(), value.clone());
|
2019-09-10 12:08:01 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
config::write(&result, &configuration)?;
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(value.tag()));
|
|
|
|
}
|
2019-11-03 00:43:15 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 00:22:30 +01:00
|
|
|
else if let Tagged { item: true, tag } = clear {
|
|
|
|
result.clear();
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
config::write(&result, &configuration)?;
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(tag));
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if let Tagged { item: true, tag } = path {
|
|
|
|
let path = config::default_path_for(&configuration)?;
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
yield ReturnSuccess::value(Value::Primitive(Primitive::Path(path)).tagged(tag));
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|
2019-11-03 00:22:30 +01:00
|
|
|
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(),
|
|
|
|
));
|
|
|
|
}
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(v.tag()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(name));
|
|
|
|
}
|
|
|
|
};
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
Ok(stream.to_output_stream())
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|