From aad227bc134f3fd2ac0588589ec38411c2553bbb Mon Sep 17 00:00:00 2001 From: Keval Date: Wed, 29 Jan 2025 16:23:21 +0530 Subject: [PATCH] :sparkles:Feat: Add custom curl plugin with various aliases and functions Signed-off-by: Keval --- plugins/curl/README.md | 90 +++++++++++++++++++++++++++++++++++ plugins/curl/curl.plugin.zsh | 91 ++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 plugins/curl/README.md create mode 100644 plugins/curl/curl.plugin.zsh diff --git a/plugins/curl/README.md b/plugins/curl/README.md new file mode 100644 index 000000000..3e314aabb --- /dev/null +++ b/plugins/curl/README.md @@ -0,0 +1,90 @@ +# Oh My Zsh Curl Plugin + +This plugin extends the functionality of `curl` with useful aliases and functions, making HTTP requests easier from the terminal. + +## Installation + +1. Add the plugin to your `.zshrc`: + ```bash + plugins=(... curl) + ``` + +3. Reload your shell configuration: + ```bash + source ~/.zshrc + ``` + +--- + +## Aliases + +| Alias | Command | Description | +|--------------- |--------------------------------|-------------| +| `curlget` | `curl -L` | Perform a GET request (follows redirects). | +| `curlhead` | `curl -I` | Fetch only HTTP headers. | +| `curlpost` | `curl -X POST` | Send a POST request. | +| `curlput` | `curl -X PUT` | Send a PUT request. | +| `curldelete` | `curl -X DELETE` | Send a DELETE request. | +| `curljson` | `curl -H 'Content-Type: application/json'` | Use curl with a JSON content type. | +| `curldownload` | `curl -LO` | Download a file (keeps original filename). | +| `curlverbose` | `curl -v` | Show detailed request/response info. | +| `curlsilent` | `curl -s` | Silent mode (no progress or messages). | +| `curlheadonly` | `curl -I` | Fetch only response headers. | +| `curlbasic` | `curl -u` | Perform basic authentication. | +| `curlrange` | `curl -r` | Request a specific range of bytes. | + +--- + +## Functions + +### `curl_url ` +Fetches the content of the specified URL. +```bash +curl_url https://example.com +``` + +### `curl_test ` +Performs a HEAD request to check server response. +```bash +curl_test https://example.com +``` + +### `curl_download ` +Downloads content from the given URL and saves it as the specified file. +```bash +curl_download https://example.com/file.zip myfile.zip +``` + +### `curl_to_file ` +Fetches response and outputs it to a file. +```bash +curl_to_file https://example.com api-response.json +``` + +### `curl_json_pretty ` +Fetches and pretty-prints JSON response (requires `jq`). +```bash +curl_json_pretty https://api.example.com/data +``` + +### `curl_with_headers
` +Sends a request with custom headers. +```bash +curl_with_headers https://example.com "Authorization: Bearer TOKEN" +``` + +### `curl_basic_auth ` +Sends a request with basic authentication. +```bash +curl_basic_auth https://example.com admin secretpassword +``` + +### `curl_follow_redirect ` +Follows HTTP redirects. +```bash +curl_follow_redirect https://short.url/link +``` + +--- + +Enjoy enhanced `curl` productivity with this Oh My Zsh plugin! \ No newline at end of file diff --git a/plugins/curl/curl.plugin.zsh b/plugins/curl/curl.plugin.zsh new file mode 100644 index 000000000..f13e083a6 --- /dev/null +++ b/plugins/curl/curl.plugin.zsh @@ -0,0 +1,91 @@ +# curl.plugin.zsh + +# Function to quickly curl a URL +curl_url() { + if [[ -z "$1" ]]; then + echo "Usage: curl_url " + return 1 + fi + curl -L "$1" +} + +# Function for testing curl response +curl_test() { + if [[ -z "$1" ]]; then + echo "Usage: curl_test " + return 1 + fi + curl -I "$1" +} + +# Function to download a file using curl +curl_download() { + if [[ -z "$1" || -z "$2" ]]; then + echo "Usage: curl_download " + return 1 + fi + curl -o "$2" "$1" +} + +# Aliases for common curl operations +alias curlget='curl -L' # Short alias for performing curl GET request +alias curlhead='curl -I' # Short alias for getting the HTTP header info (HEAD request) +alias curlpost='curl -X POST' # Alias for sending a POST request +alias curlput='curl -X PUT' # Alias for sending a PUT request +alias curldelete='curl -X DELETE' # Alias for sending a DELETE request +alias curljson="curl -H 'Content-Type: application/json'" # curl with default JSON header +alias curldownload='curl -LO' # Alias for downloading files with `-O` (remote file name) + +# Additional Aliases for convenience +alias curlverbose='curl -v' # Alias for verbose output, shows request/response +alias curlsilent='curl -s' # Silent curl, hides progress bar and output +alias curlheadonly='curl -I' # Shortcut for HTTP headers only +alias curlbasic='curl -u' # Basic auth for curl +alias curlrange='curl -r' # For range requests (e.g., resume downloads) + +# Custom functions +# Fetch response and output to a file +curl_to_file() { + if [[ -z "$1" || -z "$2" ]]; then + echo "Usage: curl_to_file " + return 1 + fi + curl -o "$2" "$1" + echo "File saved as $2" +} + +# Function to fetch and display JSON content prettily +curl_json_pretty() { + if [[ -z "$1" ]]; then + echo "Usage: curl_json_pretty " + return 1 + fi + curl -s "$1" | jq . # jq for pretty JSON output +} + +# Send curl request with custom headers +curl_with_headers() { + if [[ -z "$1" || -z "$2" ]]; then + echo "Usage: curl_with_headers
" + return 1 + fi + curl -H "$2" "$1" +} + +# Function to perform simple curl with authentication +curl_basic_auth() { + if [[ -z "$1" || -z "$2" || -z "$3" ]]; then + echo "Usage: curl_basic_auth " + return 1 + fi + curl -u "$2:$3" "$1" +} + +# Function for a safe URL redirect to follow (useful for API endpoints that send a 301/302 redirect) +curl_follow_redirect() { + if [[ -z "$1" ]]; then + echo "Usage: curl_follow_redirect " + return 1 + fi + curl -L "$1" # Follow redirects with -L flag +}