From 51983757c65ce5850621290897b896d16ca0e7dc Mon Sep 17 00:00:00 2001 From: Hanashiko Date: Wed, 7 May 2025 14:59:01 +0300 Subject: [PATCH 1/2] feat(easy-curl): created aliases for curl commands --- plugins/easy-curl/README.md | 140 +++++++++++++++++++ plugins/easy-curl/easy-curl.plugin.zsh | 177 +++++++++++++++++++++++++ 2 files changed, 317 insertions(+) create mode 100644 plugins/easy-curl/README.md create mode 100644 plugins/easy-curl/easy-curl.plugin.zsh diff --git a/plugins/easy-curl/README.md b/plugins/easy-curl/README.md new file mode 100644 index 000000000..a918c8e6c --- /dev/null +++ b/plugins/easy-curl/README.md @@ -0,0 +1,140 @@ +# easy-curl plugin + +This plugin provides a comprehensive set of aliases and functions to simplify working with `curl` command-line tool, making API testing and HTTP requests more convenient. + +## Installation + +To use it, add `easy-curl` to the plugins array in your zshrc file: + +```zsh +plugins=(... easy-curl) +``` + +## Requirements + +- **curl**: The plugin is built around the curl utility +- **jq**: Recommended for JSON formatting functionality (some functions will fall back to plain output if jq is not installed) +- **xmllint**: Optional for XML formatting +- **js-beautify**: Optional for HTML formatting + +## Command Reference + +### Basic Aliases + +| Command | Description | Example Usage | +|---------|-------------|---------------| +| `cl` | curl with redirect following | `cl https://example.com` | +| `clh` | curl headers only with redirect following | `clh https://example.com` | +| `ch` | curl headers only | `ch https://example.com` | +| `cj` | curl with JSON content-type header | `cj https://api.example.com` | +| `cjson` | curl with JSON content-type and accept headers | `cjson https://api.example.com` | +| `cv` | curl with verbose output | `cv https://example.com` | +| `cs` | curl in silent mode | `cs https://example.com` | +| `csv` | curl in silent mode with verbose output | `csv https://example.com` | +| `ca` | curl with custom User-Agent | `ca "Mozilla/5.0" https://example.com` | +| `ct` | curl showing request time | `ct https://example.com` | + +### HTTP Method Aliases + +| Command | Description | Example Usage | +|---------|-------------|---------------| +| `cget` | curl with GET method | `cget https://api.example.com/users` | +| `cpost` | curl with POST method | `cpost https://api.example.com/users` | +| `cput` | curl with PUT method | `cput https://api.example.com/users/1` | +| `cpatch` | curl with PATCH method | `cpatch https://api.example.com/users/1` | +| `cdel` | curl with DELETE method | `cdel https://api.example.com/users/1` | +| `chead` | curl with HEAD method | `chead https://example.com` | +| `coptions` | curl with OPTIONS method | `coptions https://api.example.com` | + +### Output Formatting + +| Command | Description | Example Usage | +|---------|-------------|---------------| +| `cjp` | curl with JSON pretty-printing via jq | `cjp https://api.example.com/users` | +| `cjps` | curl in silent mode with JSON pretty-printing | `cjps https://api.example.com/users` | + +### File Operations + +| Command | Description | Example Usage | +|---------|-------------|---------------| +| `cof` | curl and save to specified file | `cof output.txt https://example.com` | +| `cOrf` | curl and save with original filename | `cOrf https://example.com/file.zip` | + +### Advanced Functions + +| Function | Description | Example Usage | +|----------|-------------|---------------| +| `curlpost` | Send JSON data via POST | `curlpost https://api.example.com/users '{"name":"John"}'` | +| `curlauth` | Make request with basic authentication | `curlauth https://api.example.com/users username password` | +| `curltoken` | Make request with bearer token | `curltoken https://api.example.com/users "your-token"` | +| `curlinfo` | Display comprehensive timing and header information | `curlinfo https://example.com` | +| `curlcompare` | Compare response times of multiple URLs | `curlcompare https://site1.com https://site2.com` | +| `curlmonitor` | Monitor website availability periodically | `curlmonitor https://example.com 30` | +| `curlallmethods` | Test all HTTP methods on a URL | `curlallmethods https://api.example.com/endpoint` | +| `curlformat` | Auto-detect content type and format accordingly | `curlformat https://api.example.com/users` | + +## Function Details + +### curlpost +Makes a POST request with JSON data and formats the response using jq. + +```zsh +curlpost https://api.example.com/users '{"name":"John","email":"john@example.com"}' +``` + +### curlauth +Tests an API endpoint with basic authentication. + +```zsh +curlauth https://api.example.com/protected username password +``` + +### curltoken +Makes a request with a bearer token for authorization. + +```zsh +curltoken https://api.example.com/me "eyJhbGciOiJIUzI1NiIsI..." +``` + +### curlinfo +Displays detailed timing metrics and response headers for a URL. + +```zsh +curlinfo https://example.com +``` + +This will show: +- DNS lookup time +- Connection time +- TLS setup time +- Total time +- Response size +- All response headers + +### curlcompare +Compares response times of multiple URLs to benchmark performance. + +```zsh +curlcompare https://site1.com https://site2.com https://site3.com +``` + +### curlmonitor +Continuously monitors a website's availability at specified intervals (default: 60 seconds). + +```zsh +curlmonitor https://example.com 30 # Check every 30 seconds +``` + +### curlallmethods +Tests all standard HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH) on a URL. + +```zsh +curlallmethods https://api.example.com/test +``` + +### curlformat +Auto-detects the content type of the response and formats it accordingly (JSON, XML, HTML). + +```zsh +curlformat https://api.example.com/users +``` diff --git a/plugins/easy-curl/easy-curl.plugin.zsh b/plugins/easy-curl/easy-curl.plugin.zsh new file mode 100644 index 000000000..ab2ddc6c3 --- /dev/null +++ b/plugins/easy-curl/easy-curl.plugin.zsh @@ -0,0 +1,177 @@ +#!/usr/bin/env zsh + +# easy-curl - an Oh My Zsh plugin to simplify working with curl + +# Basic curl aliases +alias cl='curl -L' # follow redirects +alias clh='curl -LI' # headers only + follow redirects +alias ch='curl -I' # headers only +alias cj='curl -H "Content-Type: application/json"' # JSON header +alias cjson='curl -H "Content-Type: application/json" -H "Accept: application/json"' +alias cv='curl -vv' # verbose output +alias cs='curl -s' # silent mode +alias csv='curl -s -v' # silent mode + verbose output +alias ca='curl -A' # specify User-Agent +alias ct='curl -w "%{time_total}\n"' # show request time + +# HTTP methods +alias cget='curl -X GET' +alias cpost='curl -X POST' +alias cput='curl -X PUT' +alias cpatch='curl -X PATCH' +alias cdel='curl -X DELETE' +alias chead='curl -X HEAD' +alias coptions='curl -X OPTIONS' + +# JSON response formatting +alias cjp='curl | jq .' # curl with JSON formatting using jq +alias cjps='curl -s | jq .' # silent mode with jq + +# Saving to file +alias cof='curl -o' # save to specified file +alias cOrf='curl -O -J' # save with original filename + +# curl functions + +# Function to send JSON data via POST +curlpost() { + if [[ $# -lt 2 ]]; then + echo "Usage: curlpost " + return 1 + fi + + curl -s -X POST \ + -H "Content-Type: application/json" \ + -d "$2" \ + "$1" | jq 2>/dev/null || cat +} + +# Check API with basic authentication +curlauth() { + if [[ $# -lt 3 ]]; then + echo "Usage: curlauth " + return 1 + fi + + curl -s -u "$2:$3" "$1" | jq 2>/dev/null || cat +} + +# Function for testing APIs with bearer token +curltoken() { + if [[ $# -lt 2 ]]; then + echo "Usage: curltoken " + return 1 + fi + + curl -s -H "Authorization: Bearer $2" "$1" | jq 2>/dev/null || cat +} + +# Function for quick request with headers and timing information +curlinfo() { + if [[ $# -lt 1 ]]; then + echo "Usage: curlinfo " + return 1 + fi + + echo "šŸ•’ Timing information for $1:" + curl -w "\n\nStatus: %{http_code}\nDNS lookup: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS setup: %{time_appconnect}s\nPreTransfer: %{time_pretransfer}s\nStartTransfer: %{time_starttransfer}s\nTotal: %{time_total}s\nSize: %{size_download} bytes\n" -o /dev/null -s "$1" + + echo "\n🌐 Headers information:" + curl -s -I "$1" +} + +# Function to compare response time of different URLs +curlcompare() { + if [[ $# -lt 2 ]]; then + echo "Usage: curlcompare [URL3...]" + return 1 + fi + + for url in "$@"; do + echo "Testing $url" + time curl -s -o /dev/null "$url" + echo "----------------------------------------" + done +} + +# Function to monitor website availability +curlmonitor() { + if [[ $# -lt 1 ]]; then + echo "Usage: curlmonitor [interval in seconds (default: 60)]" + return 1 + fi + + local url="$1" + local interval=${2:-60} + + echo "Monitoring $url every $interval seconds. Press Ctrl+C to stop." + + while true; do + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url") + local response_time=$(curl -s -o /dev/null -w "%{time_total}" "$url") + + if [[ "$status" == "200" ]]; then + echo "$timestamp - āœ… Status: $http_status - Response: ${response_time}s" + else + echo "$timestamp - āŒ Status: $http_status - Response: ${response_time}s" + fi + + sleep $interval + done +} + +# Function to test all HTTP methods on a URL +curlallmethods() { + if [[ $# -lt 1 ]]; then + echo "Usage: curlallmethods " + return 1 + fi + + local url="$1" + local methods=("GET" "POST" "PUT" "DELETE" "HEAD" "OPTIONS" "PATCH") + + for method in "${methods[@]}"; do + echo "Testing $method on $url" + curl -s -X $method -w "\nStatus: %{http_code}\nTime: %{time_total}s\n" -o /dev/null "$url" + echo "----------------------------------------" + done +} + +# Auto-detect content type for formatted output +curlformat() { + if [[ $# -lt 1 ]]; then + echo "Usage: curlformat " + return 1 + fi + + local url="$1" + local content_type=$(curl -s -I "$url" | grep -i "content-type" | head -n 1) + + if [[ "$content_type" =~ "json" ]]; then + echo "Detected JSON content, formatting with jq" + curl -s "$url" | jq 2>/dev/null || cat + elif [[ "$content_type" =~ "xml" ]]; then + echo "Detected XML content, formatting with xmllint" + curl -s "$url" | xmllint --format - 2>/dev/null || cat + elif [[ "$content_type" =~ "html" ]]; then + echo "Detected HTML content, formatting with html-beautify" + if command -v html-beautify &> /dev/null; then + curl -s "$url" | html-beautify 2>/dev/null || cat + else + curl -s "$url" | cat + echo "\nInstall js-beautify for better HTML formatting" + fi + else + curl -s "$url" + fi +} + +# Check if required dependencies are installed +if ! command -v jq &> /dev/null; then + echo "āš ļø jq is not installed. Some functions may not work correctly." + echo "Install jq for a better JSON experience." +fi + +# Auto-completion for plugin functions +compdef _urls curlpost curlauth curltoken curlinfo curlcompare curlmonitor curlallmethods curlformat From 2aed98cf448c3a9aa3c21885a28987295b97ee6c Mon Sep 17 00:00:00 2001 From: Hanashiko Date: Wed, 7 May 2025 20:33:24 +0300 Subject: [PATCH 2/2] fix(curl): rename easy-curl plugin into curl plugin --- plugins/{easy-curl => curl}/README.md | 6 +++--- .../easy-curl.plugin.zsh => curl/curl.plugin.zsh} | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) rename plugins/{easy-curl => curl}/README.md (97%) rename plugins/{easy-curl/easy-curl.plugin.zsh => curl/curl.plugin.zsh} (98%) diff --git a/plugins/easy-curl/README.md b/plugins/curl/README.md similarity index 97% rename from plugins/easy-curl/README.md rename to plugins/curl/README.md index a918c8e6c..a70f81edc 100644 --- a/plugins/easy-curl/README.md +++ b/plugins/curl/README.md @@ -1,13 +1,13 @@ -# easy-curl plugin +# curl plugin This plugin provides a comprehensive set of aliases and functions to simplify working with `curl` command-line tool, making API testing and HTTP requests more convenient. ## Installation -To use it, add `easy-curl` to the plugins array in your zshrc file: +To use it, add `curl` to the plugins array in your zshrc file: ```zsh -plugins=(... easy-curl) +plugins=(... curl) ``` ## Requirements diff --git a/plugins/easy-curl/easy-curl.plugin.zsh b/plugins/curl/curl.plugin.zsh similarity index 98% rename from plugins/easy-curl/easy-curl.plugin.zsh rename to plugins/curl/curl.plugin.zsh index ab2ddc6c3..49c52f44e 100644 --- a/plugins/easy-curl/easy-curl.plugin.zsh +++ b/plugins/curl/curl.plugin.zsh @@ -1,6 +1,6 @@ #!/usr/bin/env zsh -# easy-curl - an Oh My Zsh plugin to simplify working with curl +# curl - an Oh My Zsh plugin to simplify working with curl # Basic curl aliases alias cl='curl -L' # follow redirects