Advertisement
snick512

CLI Bludit

Jun 8th, 2025
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.99 KB | Source Code | 0 0
  1. #!/bin/bash
  2. # Create .page_config
  3. # API_URL="https://example.com/api/pages"
  4. # TOKEN=""
  5. # AUTH=""
  6. #
  7. set -euo pipefail
  8.  
  9. # Load secure configuration
  10. CONFIG_FILE=".page_config"
  11. if [[ ! -f "$CONFIG_FILE" ]]; then
  12.     echo "❌ Missing .page_config file. Please create it with API_URL, TOKEN, and AUTH."
  13.     exit 1
  14. fi
  15. source "$CONFIG_FILE"
  16.  
  17. # --- Parse command-line arguments ---
  18. TITLE=""
  19. TAGS=""
  20. while [[ "$#" -gt 0 ]]; do
  21.     case "$1" in
  22.         --title)
  23.             TITLE="$2"
  24.             shift 2
  25.             ;;
  26.         --tags)
  27.             TAGS="$2"
  28.             shift 2
  29.             ;;
  30.         *)
  31.             echo "❌ Unknown option: $1"
  32.             echo "Usage: $0 --title \"My Title\" --tags \"tag1,tag2\""
  33.             exit 1
  34.             ;;
  35.     esac
  36. done
  37.  
  38. # --- Validate input ---
  39. if [[ -z "$TITLE" || -z "$TAGS" ]]; then
  40.     echo "❌ Error: Both --title and --tags are required."
  41.     exit 1
  42. fi
  43.  
  44. # --- Sanitize title into slug ---
  45. SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9 ]//g' | tr ' ' '-' | sed 's/-\+/-/g' | sed 's/^-//;s/-$//')
  46.  
  47. # --- Secure temp file for markdown input ---
  48. TMP_FILE=$(mktemp "/tmp/markdown_XXXXXX.md")
  49. trap 'shred -u "$TMP_FILE" 2>/dev/null' EXIT
  50.  
  51. echo "# Replace with your content" > "$TMP_FILE"
  52. ${EDITOR:-nano} "$TMP_FILE"
  53.  
  54. # --- Read Markdown content ---
  55. CONTENT=$(<"$TMP_FILE")
  56.  
  57. # --- Secure JSON construction (no temp payload file) ---
  58. JSON=$(jq -n \
  59.     --arg token "$TOKEN" \
  60.     --arg auth "$AUTH" \
  61.     --arg title "$TITLE" \
  62.     --arg content "$CONTENT" \
  63.     --arg slug "$SLUG" \
  64.     --arg tags "$TAGS" \
  65.     '{
  66.      token: $token,
  67.      authentication: $auth,
  68.      title: $title,
  69.      content: $content,
  70.      slug: $slug,
  71.      tags: $tags
  72.    }')
  73.  
  74. # --- Send POST request ---
  75. echo "🚀 Posting securely to $API_URL ..."
  76. RESPONSE=$(curl -s -X POST "$API_URL" \
  77.     -H "Content-Type: application/json" \
  78.     -d "$JSON")
  79.  
  80. # --- Show response ---
  81. echo "📬 Response:"
  82. echo "$RESPONSE"
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement