Gunakan Cursor CLI dalam skrip dan workflow otomasi untuk tugas analisis kode, pembuatan, dan refactoring.

Cara kerjanya

Gunakan print mode (-p, --print) untuk scripting dan automasi non-interaktif.

Modifikasi file dalam skrip

Gabungkan --print dengan --force untuk memodifikasi file dalam skrip:
# Aktifkan modifikasi file dalam print mode
cursor-agent -p --force "Refactor this code to use modern ES6+ syntax"

# Tanpa --force, perubahan hanya diusulkan, tidak diterapkan
cursor-agent -p "Add JSDoc comments to this file"  # Tidak akan memodifikasi file

# Batch processing dengan perubahan file yang sebenarnya
find src/ -name "*.js" | while read file; do
  cursor-agent -p --force "Add comprehensive JSDoc comments to $file"
done
Flag --force memungkinkan agent melakukan perubahan file secara langsung tanpa konfirmasi

Setup

Lihat Installation dan Authentication untuk detail setup lengkap.
# Install Cursor CLI
curl https://cursor.com/install -fsS | bash

# Set API key untuk skrip  
export CURSOR_API_KEY=your_api_key_here
cursor-agent -p "Analyze this code"

Contoh skrip

Gunakan format output yang berbeda untuk kebutuhan skrip yang berbeda. Lihat Format output untuk detailnya.

Mencari di codebase

Gunakan --output-format text untuk respons yang mudah dibaca:
#!/bin/bash
# Pertanyaan codebase sederhana

cursor-agent -p --output-format text "Apa yang dilakukan codebase ini?"

Code review otomatis

Gunakan --output-format json untuk analisis terstruktur:
#!/bin/bash
# simple-code-review.sh - Skrip code review dasar

echo "Memulai code review..."

# Review perubahan terbaru
cursor-agent -p --force --output-format text \
  "Review perubahan kode terbaru dan berikan feedback tentang:
  - Kualitas kode dan keterbacaan  
  - Bug atau masalah potensial
  - Pertimbangan keamanan
  - Kepatuhan terhadap best practices

  Berikan saran spesifik untuk perbaikan dan tulis ke review.txt"

if [ $? -eq 0 ]; then
  echo "✅ Code review berhasil diselesaikan"
else
  echo "❌ Code review gagal"
  exit 1
fi

Pelacakan progress real-time

Gunakan --output-format stream-json untuk pelacakan progress real-time:
#!/bin/bash
# stream-progress.sh - Lacak progress secara real-time

echo "🚀 Memulai stream processing..."

# Lacak progress secara real-time
accumulated_text=""
tool_count=0
start_time=$(date +%s)

cursor-agent -p --force --output-format stream-json \
  "Analisis struktur proyek ini dan buat laporan ringkasan di analysis.txt" | \
  while IFS= read -r line; do
    
    type=$(echo "$line" | jq -r '.type // empty')
    subtype=$(echo "$line" | jq -r '.subtype // empty')
    
    case "$type" in
      "system")
        if [ "$subtype" = "init" ]; then
          model=$(echo "$line" | jq -r '.model // "unknown"')
          echo "🤖 Menggunakan model: $model"
        fi
        ;;
        
      "assistant")
        # Akumulasi streaming text deltas
        content=$(echo "$line" | jq -r '.message.content[0].text // empty')
        accumulated_text="$accumulated_text$content"
        
        # Tampilkan progress langsung
        printf "\r📝 Menghasilkan: %d karakter" ${#accumulated_text}
        ;;
        
      "tool_call")
        if [ "$subtype" = "started" ]; then
          tool_count=$((tool_count + 1))
          
          # Ekstrak informasi tool
          if echo "$line" | jq -e '.tool_call.writeToolCall' > /dev/null 2>&1; then
            path=$(echo "$line" | jq -r '.tool_call.writeToolCall.args.path // "unknown"')
            echo -e "\n🔧 Tool #$tool_count: Membuat $path"
          elif echo "$line" | jq -e '.tool_call.readToolCall' > /dev/null 2>&1; then
            path=$(echo "$line" | jq -r '.tool_call.readToolCall.args.path // "unknown"')
            echo -e "\n📖 Tool #$tool_count: Membaca $path"
          fi
          
        elif [ "$subtype" = "completed" ]; then
          # Ekstrak dan tampilkan hasil tool
          if echo "$line" | jq -e '.tool_call.writeToolCall.result.success' > /dev/null 2>&1; then
            lines=$(echo "$line" | jq -r '.tool_call.writeToolCall.result.success.linesCreated // 0')
            size=$(echo "$line" | jq -r '.tool_call.writeToolCall.result.success.fileSize // 0')
            echo "   ✅ Berhasil membuat $lines baris ($size bytes)"
          elif echo "$line" | jq -e '.tool_call.readToolCall.result.success' > /dev/null 2>&1; then
            lines=$(echo "$line" | jq -r '.tool_call.readToolCall.result.success.totalLines // 0')
            echo "   ✅ Berhasil membaca $lines baris"
          fi
        fi
        ;;
        
      "result")
        duration=$(echo "$line" | jq -r '.duration_ms // 0')
        end_time=$(date +%s)
        total_time=$((end_time - start_time))
        
        echo -e "\n\n🎯 Selesai dalam ${duration}ms (${total_time}s total)"
        echo "📊 Statistik akhir: $tool_count tool, ${#accumulated_text} karakter dihasilkan"
        ;;
    esac
  done