name: monitor_logs
id: org.themobileprof.monitor_logs
version: 1.0.0
description: Tail and monitor log files with filtering options
tags: [devops-ops, composite, logs, monitoring]
provides:
  - logs_monitored
requires:
  - check_file_exists
size_kb: 2

flows:
  main:
    start: monitor
    steps:
      monitor:
        type: action
        message: "Monitoring log file: {{.log_file}}"
        command: |
          LOG_FILE="{{.log_file}}"
          FILTER="{{.filter}}"
          LINES="{{.lines}}"
          FOLLOW="{{.follow}}"
          
          if [ -z "$LOG_FILE" ]; then
            echo "❌ Must specify log_file path"
            echo ""
            echo "💡 Examples:"
            echo "  • log_file=/var/log/syslog"
            echo "  • log_file=/var/log/nginx/access.log"
            exit 1
          fi
          
          # Expand tilde
          if [[ "$LOG_FILE" == ~* ]]; then
            LOG_FILE="${LOG_FILE/#\~/$HOME}"
          fi
          
          if [ ! -f "$LOG_FILE" ]; then
            echo "❌ Log file not found: $LOG_FILE"
            exit 1
          fi
          
          # Default to last 50 lines
          if [ -z "$LINES" ]; then
            LINES=50
          fi
          
          echo "📋 Log Monitor"
          echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
          echo "File:   $LOG_FILE"
          echo "Lines:  $LINES"
          
          if [ -n "$FILTER" ]; then
            echo "Filter: $FILTER"
          fi
          
          if [ "$FOLLOW" = "true" ]; then
            echo "Mode:   Following (live updates)"
          fi
          
          echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
          echo ""
          
          # Monitor logs
          if [ "$FOLLOW" = "true" ]; then
            if [ -n "$FILTER" ]; then
              tail -n "$LINES" -f "$LOG_FILE" | grep --line-buffered -i "$FILTER"
            else
              tail -n "$LINES" -f "$LOG_FILE"
            fi
          else
            if [ -n "$FILTER" ]; then
              tail -n "$LINES" "$LOG_FILE" | grep -i "$FILTER"
            else
              tail -n "$LINES" "$LOG_FILE"
            fi
          fi
        next: end
      
      end:
        type: terminal
        message: "Log monitoring complete"
