Relax groups and blocks to output at pipeline level (#3643)

* Relax groups and blocks to output at pipeline level

* Fix up tests and add ignore command
This commit is contained in:
JT
2021-06-18 13:04:51 +12:00
committed by GitHub
parent d9d956e54f
commit fe5055cf29
11 changed files with 177 additions and 107 deletions

View File

@ -70,6 +70,7 @@ pub(crate) mod help;
pub(crate) mod histogram;
pub(crate) mod history;
pub(crate) mod if_;
pub(crate) mod ignore;
pub(crate) mod insert;
pub(crate) mod into;
pub(crate) mod keep;
@ -229,6 +230,7 @@ pub(crate) use headers::Headers;
pub(crate) use help::Help;
pub(crate) use histogram::Histogram;
pub(crate) use history::History;
pub(crate) use ignore::Ignore;
pub(crate) use insert::Command as Insert;
pub(crate) use keep::{Keep, KeepUntil, KeepWhile};
pub(crate) use last::Last;

View File

@ -14,7 +14,9 @@ impl WholeStreamCommand for AutoenvTrust {
}
fn signature(&self) -> Signature {
Signature::build("autoenv trust").optional("dir", SyntaxShape::String, "Directory to allow")
Signature::build("autoenv trust")
.optional("dir", SyntaxShape::String, "Directory to allow")
.switch("quiet", "Don't output success message", Some('q'))
}
fn usage(&self) -> &str {
@ -23,9 +25,8 @@ impl WholeStreamCommand for AutoenvTrust {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let ctx = &args.context;
let file_to_trust = match args.call_info.evaluate(&ctx)?.args.nth(0) {
let file_to_trust = match args.opt(0)? {
Some(Value {
value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _,
@ -40,6 +41,7 @@ impl WholeStreamCommand for AutoenvTrust {
dir
}
};
let quiet = args.has_flag("quiet");
let content = std::fs::read(&file_to_trust)?;
@ -55,9 +57,13 @@ impl WholeStreamCommand for AutoenvTrust {
})?;
fs::write(config_path, tomlstr).expect("Couldn't write to toml file");
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env trusted!").into_value(tag),
)))
if quiet {
Ok(ActionStream::empty())
} else {
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env trusted!").into_value(tag),
)))
}
}
fn is_binary(&self) -> bool {
false

View File

@ -14,11 +14,9 @@ impl WholeStreamCommand for AutoenvUnTrust {
}
fn signature(&self) -> Signature {
Signature::build("autoenv untrust").optional(
"dir",
SyntaxShape::String,
"Directory to disallow",
)
Signature::build("autoenv untrust")
.optional("dir", SyntaxShape::String, "Directory to disallow")
.switch("quiet", "Don't output success message", Some('q'))
}
fn usage(&self) -> &str {
@ -27,8 +25,7 @@ impl WholeStreamCommand for AutoenvUnTrust {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let ctx = &args.context;
let file_to_untrust = match args.call_info.evaluate(&ctx)?.args.nth(0) {
let file_to_untrust = match args.opt(0)? {
Some(Value {
value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _,
@ -43,6 +40,7 @@ impl WholeStreamCommand for AutoenvUnTrust {
dir
}
};
let quiet = args.has_flag("quiet");
let config_path = config::default_path_for(&Some(PathBuf::from("nu-env.toml")))?;
@ -79,9 +77,13 @@ impl WholeStreamCommand for AutoenvUnTrust {
})?;
fs::write(config_path, tomlstr).expect("Couldn't write to toml file");
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env untrusted!").into_value(tag),
)))
if quiet {
Ok(ActionStream::empty())
} else {
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env untrusted!").into_value(tag),
)))
}
}
fn is_binary(&self) -> bool {
false

View File

@ -263,7 +263,10 @@ fn spawn(
"Received unexpected type from pipeline ({})",
unsupported.type_name()
),
"expected a string",
format!(
"expected a string, got {} as input",
unsupported.type_name()
),
stdin_name_tag.clone(),
)),
tag: stdin_name_tag,

View File

@ -17,6 +17,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(Def),
whole_stream_command(Source),
whole_stream_command(Alias),
whole_stream_command(Ignore),
// System/file operations
whole_stream_command(Exec),
whole_stream_command(Pwd),

View File

@ -0,0 +1,49 @@
extern crate unicode_segmentation;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::Signature;
pub struct Ignore;
impl WholeStreamCommand for Ignore {
fn name(&self) -> &str {
"ignore"
}
fn signature(&self) -> Signature {
Signature::build("ignore")
}
fn usage(&self) -> &str {
"Ignore the output of the previous command in the pipeline"
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let _: Vec<_> = args.input.collect();
Ok(OutputStream::empty())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "echo done | ignore",
example: r#"echo "There are seven words in this sentence" | size"#,
result: None,
}]
}
}
#[cfg(test)]
mod tests {
use super::Ignore;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(Ignore {})
}
}

View File

@ -111,8 +111,13 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
let block = parse_line(sample_pipeline.example, &ctx)?;
if let Some(expected) = &sample_pipeline.result {
let start = std::time::Instant::now();
let result = evaluate_block(block, &mut ctx)?;
println!("input: {}", sample_pipeline.example);
println!("result: {:?}", result);
println!("done: {:?}", start.elapsed());
ctx.with_errors(|reasons| reasons.iter().cloned().take(1).next())
.map_or(Ok(()), Err)?;

View File

@ -19,7 +19,7 @@ fn clearing_config_clears_config() {
)]);
assert_that!(
nu.pipeline("config clear; config get skip_welcome_message"),
nu.pipeline("config clear | ignore; config get skip_welcome_message"),
says().stdout("")
);
let config_contents = std::fs::read_to_string(file).expect("Could not read file");
@ -63,7 +63,7 @@ fn config_set_sets_value() {
assert_that!(
//Clears config
nu.pipeline("config set key value; config get key"),
nu.pipeline("config set key value | ignore; config get key"),
says().stdout("value")
);
let config_contents = std::fs::read_to_string(file).expect("Could not read file");
@ -86,7 +86,7 @@ fn config_set_into_sets_value() {
assert_that!(
//Clears config
nu.pipeline("echo value | config set_into key; config get key"),
nu.pipeline("echo value | config set_into key | ignore; config get key"),
says().stdout("value")
);
let config_contents = std::fs::read_to_string(file).expect("Could not read file");
@ -109,7 +109,7 @@ fn config_rm_removes_value() {
)]);
assert_that!(
nu.pipeline("config remove key; config get key"),
nu.pipeline("config remove key | ignore; config get key"),
says().stdout("")
);
let config_contents = std::fs::read_to_string(file).expect("Could not read file");