diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 553bd20a68..0f7d6c7b49 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -433,8 +433,9 @@ pub fn create_default_context() -> EngineState { // Network bind_command! { - Fetch, - Post, + Http, + HttpGet, + HttpPost, Url, UrlBuildQuery, UrlEncode, diff --git a/crates/nu-command/src/deprecated/deprecated_commands.rs b/crates/nu-command/src/deprecated/deprecated_commands.rs index b8cb8c7cef..28fc079143 100644 --- a/crates/nu-command/src/deprecated/deprecated_commands.rs +++ b/crates/nu-command/src/deprecated/deprecated_commands.rs @@ -18,5 +18,7 @@ pub fn deprecated_commands() -> HashMap { "build-string".to_string(), "str join'/'string concatenation with '+'".to_string(), ), + ("fetch".to_string(), "http get".to_string()), + ("post".to_string(), "http post".to_string()), ]) } diff --git a/crates/nu-command/src/network/fetch.rs b/crates/nu-command/src/network/http/get.rs similarity index 95% rename from crates/nu-command/src/network/fetch.rs rename to crates/nu-command/src/network/http/get.rs index 31a2c013b6..897e7ae932 100644 --- a/crates/nu-command/src/network/fetch.rs +++ b/crates/nu-command/src/network/http/get.rs @@ -20,11 +20,11 @@ pub struct SubCommand; impl Command for SubCommand { fn name(&self) -> &str { - "fetch" + "http get" } fn signature(&self) -> Signature { - Signature::build("fetch") + Signature::build("http get") .input_output_types(vec![(Type::Nothing, Type::Any)]) .required( "URL", @@ -74,7 +74,7 @@ impl Command for SubCommand { fn search_terms(&self) -> Vec<&str> { vec![ - "network", "get", "pull", "request", "http", "download", "curl", "wget", + "network", "fetch", "pull", "request", "download", "curl", "wget", ] } @@ -91,18 +91,18 @@ impl Command for SubCommand { fn examples(&self) -> Vec { vec![ Example { - description: "Fetch content from example.com", - example: "fetch https://www.example.com", + description: "http get content from example.com", + example: "http get https://www.example.com", result: None, }, Example { - description: "Fetch content from example.com, with username and password", - example: "fetch -u myuser -p mypass https://www.example.com", + description: "http get content from example.com, with username and password", + example: "http get -u myuser -p mypass https://www.example.com", result: None, }, Example { - description: "Fetch content from example.com, with custom header", - example: "fetch -H [my-header-key my-header-value] https://www.example.com", + description: "http get content from example.com, with custom header", + example: "http get -H [my-header-key my-header-value] https://www.example.com", result: None, }, ] diff --git a/crates/nu-command/src/network/http/http_.rs b/crates/nu-command/src/network/http/http_.rs new file mode 100644 index 0000000000..ddd7b15aff --- /dev/null +++ b/crates/nu-command/src/network/http/http_.rs @@ -0,0 +1,55 @@ +use nu_engine::get_full_help; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + Category, IntoPipelineData, PipelineData, Signature, Type, Value, +}; + +#[derive(Clone)] +pub struct Http; + +impl Command for Http { + fn name(&self) -> &str { + "http" + } + + fn signature(&self) -> Signature { + Signature::build("http") + .input_output_types(vec![(Type::Nothing, Type::String)]) + .category(Category::Network) + } + + fn usage(&self) -> &str { + "Various commands for working with http methods" + } + + fn extra_usage(&self) -> &str { + "You must use one of the following subcommands. Using this command as-is will only produce this help message." + } + + fn search_terms(&self) -> Vec<&str> { + vec![ + "network", "fetch", "pull", "request", "download", "curl", "wget", + ] + } + + fn run( + &self, + engine_state: &EngineState, + stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + Ok(Value::String { + val: get_full_help( + &Http.signature(), + &Http.examples(), + engine_state, + stack, + self.is_parser_keyword(), + ), + span: call.head, + } + .into_pipeline_data()) + } +} diff --git a/crates/nu-command/src/network/http/mod.rs b/crates/nu-command/src/network/http/mod.rs new file mode 100644 index 0000000000..af807b222c --- /dev/null +++ b/crates/nu-command/src/network/http/mod.rs @@ -0,0 +1,7 @@ +mod get; +mod http_; +mod post; + +pub use get::SubCommand as HttpGet; +pub use http_::Http; +pub use post::SubCommand as HttpPost; diff --git a/crates/nu-command/src/network/post.rs b/crates/nu-command/src/network/http/post.rs similarity index 97% rename from crates/nu-command/src/network/post.rs rename to crates/nu-command/src/network/http/post.rs index 49674aa01d..ce582cf854 100644 --- a/crates/nu-command/src/network/post.rs +++ b/crates/nu-command/src/network/http/post.rs @@ -19,11 +19,11 @@ pub struct SubCommand; impl Command for SubCommand { fn name(&self) -> &str { - "post" + "http post" } fn signature(&self) -> Signature { - Signature::build("post") + Signature::build("http post") .input_output_types(vec![(Type::Nothing, Type::Any)]) .required("path", SyntaxShape::String, "the URL to post to") .required("body", SyntaxShape::Any, "the contents of the post body") @@ -80,7 +80,7 @@ impl Command for SubCommand { } fn search_terms(&self) -> Vec<&str> { - vec!["network", "send", "push", "http"] + vec!["network", "send", "push"] } fn run( @@ -96,22 +96,22 @@ impl Command for SubCommand { vec![ Example { description: "Post content to url.com", - example: "post url.com 'body'", + example: "http post url.com 'body'", result: None, }, Example { description: "Post content to url.com, with username and password", - example: "post -u myuser -p mypass url.com 'body'", + example: "http post -u myuser -p mypass url.com 'body'", result: None, }, Example { description: "Post content to url.com, with custom header", - example: "post -H [my-header-key my-header-value] url.com", + example: "http post -H [my-header-key my-header-value] url.com", result: None, }, Example { description: "Post content to url.com with a json body", - example: "post -t application/json url.com { field: value }", + example: "http post -t application/json url.com { field: value }", result: None, }, ] diff --git a/crates/nu-command/src/network/mod.rs b/crates/nu-command/src/network/mod.rs index 330e1f17c8..5ca523a1d3 100644 --- a/crates/nu-command/src/network/mod.rs +++ b/crates/nu-command/src/network/mod.rs @@ -1,9 +1,8 @@ -mod fetch; +mod http; mod port; -mod post; mod url; +pub use self::http::*; pub use self::url::*; -pub use fetch::SubCommand as Fetch; + pub use port::SubCommand as Port; -pub use post::SubCommand as Post; diff --git a/crates/nu-command/src/strings/split/words.rs b/crates/nu-command/src/strings/split/words.rs index dfc56e68f8..9291bf7471 100644 --- a/crates/nu-command/src/strings/split/words.rs +++ b/crates/nu-command/src/strings/split/words.rs @@ -88,7 +88,7 @@ impl Command for SubCommand { Example { description: "A real-world example of splitting words", - example: "fetch https://www.gutenberg.org/files/11/11-0.txt | str downcase | split words -l 2 | uniq -c | sort-by count --reverse | first 10", + example: "http get https://www.gutenberg.org/files/11/11-0.txt | str downcase | split words -l 2 | uniq -c | sort-by count --reverse | first 10", result: None, }, ]