2019-07-03 22:31:15 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-06-01 07:50:16 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-09-05 18:23:42 +02:00
|
|
|
use crate::data::{config, Value};
|
2019-07-15 23:16:27 +02:00
|
|
|
use crate::parser::hir::SyntaxType;
|
2019-08-02 21:15:07 +02:00
|
|
|
use crate::parser::registry::{self};
|
2019-07-03 22:31:15 +02:00
|
|
|
use std::iter::FromIterator;
|
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-08-09 06:51:21 +02:00
|
|
|
set: Option<(Tagged<String>, Tagged<Value>)>,
|
|
|
|
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")
|
|
|
|
.named("set", SyntaxType::Any)
|
|
|
|
.named("get", SyntaxType::Any)
|
|
|
|
.named("remove", SyntaxType::Any)
|
|
|
|
.switch("clear")
|
|
|
|
.switch("path")
|
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 {
|
|
|
|
set,
|
|
|
|
get,
|
|
|
|
clear,
|
|
|
|
remove,
|
|
|
|
path,
|
|
|
|
}: ConfigArgs,
|
|
|
|
RunnableContext { name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-09-05 18:23:42 +02:00
|
|
|
let mut result = crate::data::config::config(name)?;
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
if let Some(v) = get {
|
|
|
|
let key = v.to_string();
|
2019-06-01 07:50:16 +02:00
|
|
|
let value = result
|
|
|
|
.get(&key)
|
|
|
|
.ok_or_else(|| ShellError::string(&format!("Missing key {} in config", key)))?;
|
|
|
|
|
|
|
|
return Ok(
|
2019-07-09 06:31:26 +02:00
|
|
|
stream![value.clone()].into(), // futures::stream::once(futures::future::ready(ReturnSuccess::Value(value.clone()))).into(),
|
2019-06-01 07:50:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
if let Some((key, value)) = set {
|
|
|
|
result.insert(key.to_string(), value.clone());
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
config::write_config(&result)?;
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
return Ok(stream![Tagged::from_simple_spanned_item(
|
2019-09-05 18:23:42 +02:00
|
|
|
Value::Row(result.into()),
|
2019-08-02 21:15:07 +02:00
|
|
|
value.span()
|
|
|
|
)]
|
|
|
|
.from_input_stream());
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
if let Tagged {
|
|
|
|
item: true,
|
|
|
|
tag: Tag { span, .. },
|
|
|
|
} = clear
|
|
|
|
{
|
2019-06-01 07:50:16 +02:00
|
|
|
result.clear();
|
|
|
|
|
|
|
|
config::write_config(&result)?;
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
return Ok(stream![Tagged::from_simple_spanned_item(
|
2019-09-05 18:23:42 +02:00
|
|
|
Value::Row(result.into()),
|
2019-08-09 06:51:21 +02:00
|
|
|
span
|
|
|
|
)]
|
|
|
|
.from_input_stream());
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
if let Tagged {
|
|
|
|
item: true,
|
|
|
|
tag: Tag { span, .. },
|
|
|
|
} = path
|
|
|
|
{
|
2019-08-02 21:15:07 +02:00
|
|
|
let path = config::config_path()?;
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
return Ok(stream![Tagged::from_simple_spanned_item(
|
|
|
|
Value::Primitive(Primitive::Path(path)),
|
|
|
|
span
|
2019-08-05 10:54:29 +02:00
|
|
|
)]
|
|
|
|
.from_input_stream());
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(v) = remove {
|
|
|
|
let key = v.to_string();
|
2019-06-01 07:50:16 +02:00
|
|
|
|
|
|
|
if result.contains_key(&key) {
|
|
|
|
result.remove(&key);
|
2019-08-25 06:51:12 +02:00
|
|
|
config::write_config(&result)?;
|
2019-06-01 07:50:16 +02:00
|
|
|
} else {
|
|
|
|
return Err(ShellError::string(&format!(
|
|
|
|
"{} does not exist in config",
|
|
|
|
key
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:23:42 +02:00
|
|
|
let obj = VecDeque::from_iter(vec![Value::Row(result.into()).simple_spanned(v.span())]);
|
2019-07-03 22:31:15 +02:00
|
|
|
return Ok(obj.from_input_stream());
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 18:23:42 +02:00
|
|
|
return Ok(vec![Value::Row(result.into()).simple_spanned(name)].into());
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|