nu-cli refactor moving commands into their own crate nu-command (#2910)

* move commands, futures.rs, script.rs, utils

* move over maybe_print_errors

* add nu_command crate references to nu_cli

* in commands.rs open up to pub mod from pub(crate)

* nu-cli, nu-command, and nu tests are now passing

* cargo fmt

* clean up nu-cli/src/prelude.rs

* code cleanup

* for some reason lex.rs was not formatted, may be causing my error

* remove mod completion from lib.rs which was not being used along with quickcheck macros

* add in allow unused imports

* comment out one failing external test; comment out one failing internal test

* revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests

* Update Cargo.toml

Extend the optional features to nu-command

Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
Michael Angerman
2021-01-11 20:59:53 -08:00
committed by GitHub
parent 7d07881d96
commit d06f457b2a
374 changed files with 434 additions and 99 deletions

View File

@@ -0,0 +1,40 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
pub struct Url;
#[async_trait]
impl WholeStreamCommand for Url {
fn name(&self) -> &str {
"url"
}
fn signature(&self) -> Signature {
Signature::build("url")
}
fn usage(&self) -> &str {
"Apply url function"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(get_help(&Url, &args.scope)).into_value(Tag::unknown()),
)))
}
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::Url;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(Url {})?)
}
}

View File

@@ -0,0 +1,55 @@
use url::Url;
use super::{operate, DefaultArguments};
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct UrlHost;
#[async_trait]
impl WholeStreamCommand for UrlHost {
fn name(&self) -> &str {
"url host"
}
fn signature(&self) -> Signature {
Signature::build("url host")
.rest(SyntaxShape::ColumnPath, "optionally operate by column path")
}
fn usage(&self) -> &str {
"gets the host of a url"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DefaultArguments { rest }, input) = args.process().await?;
operate(input, rest, &host).await
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get host of a url",
example: "echo 'http://www.example.com/foo/bar' | url host",
result: Some(vec![Value::from("www.example.com")]),
}]
}
}
fn host(url: &Url) -> &str {
url.host_str().unwrap_or("")
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::UrlHost;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(UrlHost {})?)
}
}

View File

@@ -0,0 +1,71 @@
mod command;
mod host;
mod path;
mod query;
mod scheme;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ColumnPath, Primitive, ReturnSuccess, ShellTypeName, UntaggedValue, Value};
use url::Url;
pub use command::Url as UrlCommand;
pub use host::UrlHost;
pub use path::UrlPath;
pub use query::UrlQuery;
pub use scheme::UrlScheme;
#[derive(Deserialize)]
struct DefaultArguments {
rest: Vec<ColumnPath>,
}
fn handle_value<F>(action: &F, v: &Value) -> Result<Value, ShellError>
where
F: Fn(&Url) -> &str + Send + 'static,
{
let a = |url| UntaggedValue::string(action(url));
let v = match &v.value {
UntaggedValue::Primitive(Primitive::String(s)) => match Url::parse(s) {
Ok(url) => a(&url).into_value(v.tag()),
Err(_) => UntaggedValue::string("").into_value(v.tag()),
},
other => {
let got = format!("got {}", other.type_name());
return Err(ShellError::labeled_error(
"value is not a string",
got,
v.tag().span,
));
}
};
Ok(v)
}
async fn operate<F>(
input: crate::InputStream,
paths: Vec<ColumnPath>,
action: &'static F,
) -> Result<OutputStream, ShellError>
where
F: Fn(&Url) -> &str + Send + Sync + 'static,
{
Ok(input
.map(move |v| {
if paths.is_empty() {
ReturnSuccess::value(handle_value(&action, &v)?)
} else {
let mut ret = v;
for path in &paths {
ret = ret.swap_data_by_column_path(
path,
Box::new(move |old| handle_value(&action, &old)),
)?;
}
ReturnSuccess::value(ret)
}
})
.to_output_stream())
}

View File

@@ -0,0 +1,58 @@
use url::Url;
use super::{operate, DefaultArguments};
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct UrlPath;
#[async_trait]
impl WholeStreamCommand for UrlPath {
fn name(&self) -> &str {
"url path"
}
fn signature(&self) -> Signature {
Signature::build("url path")
.rest(SyntaxShape::ColumnPath, "optionally operate by column path")
}
fn usage(&self) -> &str {
"gets the path of a url"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DefaultArguments { rest }, input) = args.process().await?;
operate(input, rest, &Url::path).await
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Get path of a url",
example: "echo 'http://www.example.com/foo/bar' | url path",
result: Some(vec![Value::from("/foo/bar")]),
},
Example {
description: "A trailing slash will be reflected in the path",
example: "echo 'http://www.example.com' | url path",
result: Some(vec![Value::from("/")]),
},
]
}
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::UrlPath;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(UrlPath {})?)
}
}

View File

@@ -0,0 +1,62 @@
use url::Url;
use super::{operate, DefaultArguments};
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct UrlQuery;
#[async_trait]
impl WholeStreamCommand for UrlQuery {
fn name(&self) -> &str {
"url query"
}
fn signature(&self) -> Signature {
Signature::build("url query")
.rest(SyntaxShape::ColumnPath, "optionally operate by column path")
}
fn usage(&self) -> &str {
"gets the query of a url"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DefaultArguments { rest }, input) = args.process().await?;
operate(input, rest, &query).await
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Get query of a url",
example: "echo 'http://www.example.com/?foo=bar&baz=quux' | url query",
result: Some(vec![Value::from("foo=bar&baz=quux")]),
},
Example {
description: "No query gives the empty string",
example: "echo 'http://www.example.com/' | url query",
result: Some(vec![Value::from("")]),
},
]
}
}
fn query(url: &Url) -> &str {
url.query().unwrap_or("")
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::UrlQuery;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(UrlQuery {})?)
}
}

View File

@@ -0,0 +1,57 @@
use url::Url;
use super::{operate, DefaultArguments};
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct UrlScheme;
#[async_trait]
impl WholeStreamCommand for UrlScheme {
fn name(&self) -> &str {
"url scheme"
}
fn signature(&self) -> Signature {
Signature::build("url scheme").rest(SyntaxShape::ColumnPath, "optionally operate by path")
}
fn usage(&self) -> &str {
"gets the scheme (eg http, file) of a url"
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DefaultArguments { rest }, input) = args.process().await?;
operate(input, rest, &Url::scheme).await
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Get scheme of a url",
example: "echo 'http://www.example.com' | url scheme",
result: Some(vec![Value::from("http")]),
},
Example {
description: "You get an empty string if there is no scheme",
example: "echo 'test' | url scheme",
result: Some(vec![Value::from("")]),
},
]
}
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::UrlScheme;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(UrlScheme {})?)
}
}