#!/bin/bash set -euo pipefail cmd_logs() { local count="${1:-10}" if [ ! -d "$LOGS_DIR" ]; then echo "No logs found." return fi find "$LOGS_DIR" -type f -mtime +7 -delete 2>/dev/null ls -lt "$LOGS_DIR" | head -$((count + 1)) | tail -$count | while read line; do echo "$line" done echo "" echo "Recent log contents:" echo "====================" for log in $(ls -lt "$LOGS_DIR" | head -$((count + 1)) | tail -$count | awk '{print $NF}'); do if [ -f "$LOGS_DIR/$log" ]; then echo "" echo "--- $log ---" tail -20 "$LOGS_DIR/$log" | while read line; do echo " $(mask_sensitive_vars "$line")" done fi done } kugetsu_add_notification() { local notification_type="$1" local message="$2" local issue_ref="${3:-}" local timestamp=$(date -Iseconds) mkdir -p "$(dirname "$NOTIFICATIONS_FILE")" local notifications="[]" if [ -f "$NOTIFICATIONS_FILE" ]; then notifications=$(cat "$NOTIFICATIONS_FILE") fi local new_notification=$(python3 -c "import json; print(json.dumps({ 'type': '$notification_type', 'message': '$message', 'issue_ref': '$issue_ref', 'timestamp': '$timestamp', 'read': False }))") notifications=$(python3 -c "import json; n=json.loads('$notifications'); n.append(json.loads('$new_notification')); print(json.dumps(n[-50:] if len(n)>50 else n, indent=2))") echo "$notifications" > "$NOTIFICATIONS_FILE" } cmd_notify() { local action="${1:-list}" case "$action" in list) if [ ! -f "$NOTIFICATIONS_FILE" ]; then echo "No notifications." return fi local notifications=$(cat "$NOTIFICATIONS_FILE") local count=$(echo "$notifications" | python3 -c "import sys, json; n=json.load(sys.stdin); print(sum(1 for x in n if not x.get('read', False)))") if [ "$count" -eq 0 ]; then echo "No unread notifications." return fi echo "Unread notifications ($count):" echo "$notifications" | python3 -c "import sys, json; [print(f\" [{x.get('timestamp', '')}] {x.get('type', '')}: {x.get('message', '')}\") for x in json.load(sys.stdin) if not x.get('read', False)]" ;; clear) if [ -f "$NOTIFICATIONS_FILE" ]; then python3 -c "import json; print(json.dumps([x for x in json.load(open('$NOTIFICATIONS_FILE')) if x.get('read', False)], indent=2))" > "$NOTIFICATIONS_FILE" echo "Cleared unread notifications." fi ;; *) echo "Usage: kugetsu notify [list|clear]" >&2 exit 1 ;; esac }