nushell/src/commands/config.rs

115 lines
2.9 KiB
Rust
Raw Normal View History

use crate::prelude::*;
2019-08-02 21:15:07 +02:00
use crate::commands::StaticCommand;
use crate::errors::ShellError;
2019-07-24 00:22:11 +02:00
use crate::object::{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};
use std::iter::FromIterator;
pub struct Config;
2019-08-02 21:15:07 +02:00
#[derive(Deserialize)]
pub struct ConfigArgs {
set: Option<(Spanned<String>, Spanned<Value>)>,
get: Option<Spanned<String>>,
clear: Spanned<bool>,
remove: Option<Spanned<String>>,
path: Spanned<bool>,
}
2019-07-24 00:22:11 +02:00
2019-08-02 21:15:07 +02:00
impl StaticCommand for Config {
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-08-02 21:15:07 +02:00
fn run(
&self,
args: CommandArgs,
registry: &registry::CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, config)?.run()
}
}
2019-08-02 21:15:07 +02:00
pub fn config(
ConfigArgs {
set,
get,
clear,
remove,
path,
}: ConfigArgs,
RunnableContext { name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let mut result = crate::object::config::config(name)?;
if let Some(v) = get {
let key = v.to_string();
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-08-02 21:15:07 +02:00
if let Some((key, value)) = set {
result.insert(key.to_string(), value.clone());
2019-08-02 21:15:07 +02:00
config::write_config(&result)?;
2019-08-02 21:15:07 +02:00
return Ok(stream![Spanned::from_item(
Value::Object(result.into()),
value.span()
)]
.from_input_stream());
}
2019-08-02 21:15:07 +02:00
if let Spanned { item: true, span } = clear {
result.clear();
config::write_config(&result)?;
2019-07-08 18:44:53 +02:00
return Ok(
2019-08-02 21:15:07 +02:00
stream![Spanned::from_item(Value::Object(result.into()), span)].from_input_stream(),
2019-07-08 18:44:53 +02:00
);
}
2019-08-02 21:15:07 +02:00
if let Spanned { item: true, span } = path {
let path = config::config_path()?;
return Ok(
stream![Value::Primitive(Primitive::Path(path)).spanned(span)].from_input_stream(),
);
}
if let Some(v) = remove {
let key = v.to_string();
if result.contains_key(&key) {
result.remove(&key);
} else {
return Err(ShellError::string(&format!(
"{} does not exist in config",
key
)));
}
2019-08-02 21:15:07 +02:00
let obj = VecDeque::from_iter(vec![Value::Object(result.into()).spanned(v.span())]);
return Ok(obj.from_input_stream());
}
2019-08-02 21:15:07 +02:00
return Ok(vec![Value::Object(result.into()).spanned(name)].into());
}