2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::data::config;
|
2019-09-10 12:08:01 +02:00
|
|
|
use crate::prelude::*;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tagged;
|
2019-09-10 12:08:01 +02:00
|
|
|
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-11-21 15:33:14 +01:00
|
|
|
set: Option<(Tagged<String>, 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
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
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,
|
2020-05-13 04:21:57 +02:00
|
|
|
"load the config from the path given",
|
2020-02-12 03:24:31 +01:00
|
|
|
Some('l'),
|
2019-10-28 06:15:35 +01:00
|
|
|
)
|
2019-11-03 00:43:15 +01:00
|
|
|
.named(
|
|
|
|
"set",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"set a value in the config, eg) --set [key value]",
|
2020-02-12 03:24:31 +01:00
|
|
|
Some('s'),
|
2019-11-03 00:43:15 +01:00
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"set_into",
|
2020-04-06 09:16:14 +02:00
|
|
|
SyntaxShape::String,
|
2019-11-03 00:43:15 +01:00
|
|
|
"sets a variable from values in the pipeline",
|
2020-02-12 03:24:31 +01:00
|
|
|
Some('i'),
|
2019-11-03 00:43:15 +01:00
|
|
|
)
|
2020-02-12 03:24:31 +01:00
|
|
|
.named(
|
|
|
|
"get",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"get a value from the config",
|
|
|
|
Some('g'),
|
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"remove",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"remove a value from the config",
|
|
|
|
Some('r'),
|
|
|
|
)
|
|
|
|
.switch("clear", "clear the config", Some('c'))
|
|
|
|
.switch("path", "return the path to the config file", Some('p'))
|
2019-06-01 07:50:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Configuration management."
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2019-08-02 21:15:07 +02:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
registry: &CommandRegistry,
|
2019-08-02 21:15:07 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
config(args, registry)
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2020-05-12 07:17:17 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
2020-05-12 07:17:17 +02:00
|
|
|
Example {
|
|
|
|
description: "See all config values",
|
|
|
|
example: "config",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Set completion_mode to circular",
|
|
|
|
example: "config --set [completion_mode circular]",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Store the contents of the pipeline as a path",
|
|
|
|
example: "echo ['/usr/bin' '/bin'] | config --set_into path",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Get the current startup commands",
|
|
|
|
example: "config --get startup",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Remove the startup commands",
|
|
|
|
example: "config --remove startup",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Clear the config (be careful!)",
|
|
|
|
example: "config --clear",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Get the path to the current config file",
|
|
|
|
example: "config --path",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-06-01 07:50:16 +02:00
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
pub fn config(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let name_span = args.call_info.name_tag.clone();
|
|
|
|
let name = args.call_info.name_tag.clone();
|
|
|
|
let registry = registry.clone();
|
2019-09-10 12:08:01 +02:00
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
let stream = async_stream! {
|
2020-05-16 05:18:24 +02:00
|
|
|
let (ConfigArgs {
|
|
|
|
load,
|
|
|
|
set,
|
|
|
|
set_into,
|
|
|
|
get,
|
|
|
|
clear,
|
|
|
|
remove,
|
|
|
|
path,
|
|
|
|
}, mut input) = args.process(®istry).await?;
|
|
|
|
|
2019-11-03 00:22:30 +01:00
|
|
|
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 {
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Table(list),
|
2019-11-03 00:22:30 +01:00
|
|
|
..
|
|
|
|
} => {
|
|
|
|
for l in list {
|
2019-11-21 15:33:14 +01:00
|
|
|
let value = l.clone();
|
2019-11-03 00:22:30 +01:00
|
|
|
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-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Row(result.into()).into_value(&value.tag));
|
2019-11-03 00:22:30 +01:00
|
|
|
}
|
2019-11-03 00:43:15 +01:00
|
|
|
else if let Some(v) = set_into {
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
let rows: Vec<Value> = input.collect().await;
|
2019-11-03 00:43:15 +01:00
|
|
|
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)?;
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Row(result.into()).into_value(name));
|
2019-11-03 00:43:15 +01:00
|
|
|
} else {
|
|
|
|
// Take in the pipeline as a table
|
2019-11-21 15:33:14 +01:00
|
|
|
let value = UntaggedValue::Table(rows).into_value(name.clone());
|
2019-11-03 00:43:15 +01:00
|
|
|
|
|
|
|
result.insert(key.to_string(), value.clone());
|
|
|
|
|
|
|
|
config::write(&result, &configuration)?;
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Row(result.into()).into_value(name));
|
2019-11-03 00:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
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-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Row(result.into()).into_value(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-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Primitive(Primitive::Path(path)).into_value(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);
|
2020-01-02 06:24:41 +01:00
|
|
|
config::write(&result, &configuration)?
|
2019-11-03 00:22:30 +01:00
|
|
|
} 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-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Row(result.into()).into_value(v.tag()));
|
2019-11-03 00:22:30 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-11-21 15:33:14 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::Row(result.into()).into_value(name));
|
2019-11-03 00:22:30 +01:00
|
|
|
}
|
|
|
|
};
|
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
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Config;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Config {})
|
|
|
|
}
|
|
|
|
}
|