From cde56741fbf841c1ba37eb91afd3202dcc62f9b8 Mon Sep 17 00:00:00 2001 From: KazukiY <78019968+Kazuki-Ya@users.noreply.github.com> Date: Sat, 21 Jan 2023 03:38:30 +0900 Subject: [PATCH] `fetch` -> `http get` and `post` -> `http post` (#7796) # Updated description by @rgwood This PR changes `fetch` to `http get` and `post` to `http post`. `fetch` and `post` are now deprecated. [I surveyed people on Discord](https://discord.com/channels/601130461678272522/601130461678272524/1065706282566307910) and users strongly approved of this change. # Original Description This PR is related to #2741 and my first pull request in rust :) Implemented a new http mod to better http support and alias `fetch` and `post` commands to `http get` and `http post` respectively. # User-Facing Changes Users will be able to use HTTP method via http command, for example ``` shell > http get "https://www.example.com" ... ``` --- crates/nu-command/src/default_context.rs | 5 +- .../src/deprecated/deprecated_commands.rs | 2 + .../src/network/{fetch.rs => http/get.rs} | 18 +++--- crates/nu-command/src/network/http/http_.rs | 55 +++++++++++++++++++ crates/nu-command/src/network/http/mod.rs | 7 +++ .../nu-command/src/network/{ => http}/post.rs | 14 ++--- crates/nu-command/src/network/mod.rs | 7 +-- crates/nu-command/src/strings/split/words.rs | 2 +- 8 files changed, 87 insertions(+), 23 deletions(-) rename crates/nu-command/src/network/{fetch.rs => http/get.rs} (95%) create mode 100644 crates/nu-command/src/network/http/http_.rs create mode 100644 crates/nu-command/src/network/http/mod.rs rename crates/nu-command/src/network/{ => http}/post.rs (97%) diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 553bd20a6..0f7d6c7b4 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 b8cb8c7ce..28fc07914 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 31a2c013b..897e7ae93 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 000000000..ddd7b15af --- /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 000000000..af807b222 --- /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 49674aa01..ce582cf85 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 330e1f17c..5ca523a1d 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 dfc56e68f..9291bf747 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, }, ]