refactor: modularize kugetsu shell script
Split the monolithic kugetsu script into modular components: Modules created: - kugetsu-config.sh - Config/env loading and global variables - kugetsu-index.sh - Index.json read/write via JSON - kugetsu-worktree.sh - Git worktree operations - kugetsu-log.sh - Structured logging and notifications - kugetsu-session.sh - Session create/fork/destroy logic - kugetsu-queue-daemon.sh - Queue daemon subprocess Main script (kugetsu) is now a thin dispatcher that sources all modules. Acceptance Criteria: - All existing commands work exactly as before - Main script sources modules - Each module is independently testable Fixes #116
This commit is contained in:
91
skills/kugetsu/scripts/kugetsu-log.sh
Executable file
91
skills/kugetsu/scripts/kugetsu-log.sh
Executable file
@@ -0,0 +1,91 @@
|
||||
#!/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
|
||||
}
|
||||
Reference in New Issue
Block a user