1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 15:04:42 +00:00

chore: auto format code

This commit is contained in:
Tw93
2026-01-28 11:46:59 +00:00
parent 64f79a59d8
commit c12f76c9f3
2 changed files with 76 additions and 76 deletions

View File

@@ -13,7 +13,7 @@ source "$LIB_DIR/core/base.sh"
source "$LIB_DIR/core/log.sh" source "$LIB_DIR/core/log.sh"
show_help() { show_help() {
cat <<EOF cat << EOF
Usage: mo log [OPTIONS] Usage: mo log [OPTIONS]
View and analyze Mole operation logs. View and analyze Mole operation logs.
@@ -72,7 +72,7 @@ search_log() {
echo "────────────────────────────────────────────────────────────────" echo "────────────────────────────────────────────────────────────────"
local results local results
results=$(grep -iF -- "$term" "$OPERATIONS_LOG_FILE" 2>/dev/null || true) results=$(grep -iF -- "$term" "$OPERATIONS_LOG_FILE" 2> /dev/null || true)
if [[ -z "$results" ]]; then if [[ -z "$results" ]]; then
echo -e "${YELLOW}No matches found.${NC}" echo -e "${YELLOW}No matches found.${NC}"
@@ -91,24 +91,24 @@ show_stats() {
echo "────────────────────────────────────────────────────────────────" echo "────────────────────────────────────────────────────────────────"
local total_lines local total_lines
total_lines=$(grep -c '^\[' "$OPERATIONS_LOG_FILE" 2>/dev/null || echo 0) total_lines=$(grep -c '^\[' "$OPERATIONS_LOG_FILE" 2> /dev/null || echo 0)
echo -e "${GREEN}Total operations:${NC} $total_lines" echo -e "${GREEN}Total operations:${NC} $total_lines"
echo "" echo ""
echo -e "${GREEN}By command:${NC}" echo -e "${GREEN}By command:${NC}"
grep -o '\[clean\]\|\[uninstall\]\|\[optimize\]\|\[purge\]' "$OPERATIONS_LOG_FILE" 2>/dev/null | grep -o '\[clean\]\|\[uninstall\]\|\[optimize\]\|\[purge\]' "$OPERATIONS_LOG_FILE" 2> /dev/null |
sort | uniq -c | sort -rn | sed 's/\[//g; s/\]//g' | sort | uniq -c | sort -rn | sed 's/\[//g; s/\]//g' |
awk '{printf " %-15s %s\n", $2":", $1}' || echo " No command data" awk '{printf " %-15s %s\n", $2":", $1}' || echo " No command data"
echo "" echo ""
echo -e "${GREEN}By action:${NC}" echo -e "${GREEN}By action:${NC}"
grep -o 'REMOVED\|SKIPPED\|FAILED\|REBUILT' "$OPERATIONS_LOG_FILE" 2>/dev/null | grep -o 'REMOVED\|SKIPPED\|FAILED\|REBUILT' "$OPERATIONS_LOG_FILE" 2> /dev/null |
sort | uniq -c | sort -rn | sort | uniq -c | sort -rn |
awk '{printf " %-15s %s\n", $2":", $1}' || echo " No action data" awk '{printf " %-15s %s\n", $2":", $1}' || echo " No action data"
echo "" echo ""
echo -e "${GREEN}Recent sessions:${NC}" echo -e "${GREEN}Recent sessions:${NC}"
grep 'session started' "$OPERATIONS_LOG_FILE" 2>/dev/null | tail -n 5 || echo " No session data" grep 'session started' "$OPERATIONS_LOG_FILE" 2> /dev/null | tail -n 5 || echo " No session data"
} }
show_today() { show_today() {
@@ -124,7 +124,7 @@ show_today() {
echo "────────────────────────────────────────────────────────────────" echo "────────────────────────────────────────────────────────────────"
local results local results
results=$(grep "^\[$today" "$OPERATIONS_LOG_FILE" 2>/dev/null || true) results=$(grep "^\[$today" "$OPERATIONS_LOG_FILE" 2> /dev/null || true)
if [[ -z "$results" ]]; then if [[ -z "$results" ]]; then
echo -e "${YELLOW}No operations today.${NC}" echo -e "${YELLOW}No operations today.${NC}"
@@ -152,7 +152,7 @@ filter_by_command() {
echo "────────────────────────────────────────────────────────────────" echo "────────────────────────────────────────────────────────────────"
local results local results
results=$(grep -F -- "[$cmd]" "$OPERATIONS_LOG_FILE" 2>/dev/null || true) results=$(grep -F -- "[$cmd]" "$OPERATIONS_LOG_FILE" 2> /dev/null || true)
if [[ -z "$results" ]]; then if [[ -z "$results" ]]; then
echo -e "${YELLOW}No operations found for ${cmd}.${NC}" echo -e "${YELLOW}No operations found for ${cmd}.${NC}"
@@ -175,46 +175,46 @@ main() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
--tail) --tail)
shift shift
show_tail "${1:-50}" show_tail "${1:-50}"
exit 0 exit 0
;; ;;
--search) --search)
shift shift
if [[ -z "${1:-}" ]]; then if [[ -z "${1:-}" ]]; then
echo -e "${RED}Error: --search requires an argument${NC}" echo -e "${RED}Error: --search requires an argument${NC}"
exit 1
fi
search_log "$1"
exit 0
;;
--stats)
show_stats
exit 0
;;
--today)
show_today
exit 0
;;
--command)
shift
if [[ -z "${1:-}" ]]; then
echo -e "${RED}Error: --command requires an argument${NC}"
exit 1
fi
filter_by_command "$1"
exit 0
;;
--help | -h)
show_help
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Use 'mo log --help' for usage information."
exit 1 exit 1
fi ;;
search_log "$1"
exit 0
;;
--stats)
show_stats
exit 0
;;
--today)
show_today
exit 0
;;
--command)
shift
if [[ -z "${1:-}" ]]; then
echo -e "${RED}Error: --command requires an argument${NC}"
exit 1
fi
filter_by_command "$1"
exit 0
;;
--help | -h)
show_help
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Use 'mo log --help' for usage information."
exit 1
;;
esac esac
# shellcheck disable=SC2317 # shellcheck disable=SC2317
shift shift

View File

@@ -48,7 +48,7 @@ rotate_log_once() {
local size local size
size=$(get_file_size "$LOG_FILE") size=$(get_file_size "$LOG_FILE")
if [[ "$size" -gt "$max_size" ]]; then if [[ "$size" -gt "$max_size" ]]; then
mv "$LOG_FILE" "${LOG_FILE}.old" 2>/dev/null || true mv "$LOG_FILE" "${LOG_FILE}.old" 2> /dev/null || true
ensure_user_file "$LOG_FILE" ensure_user_file "$LOG_FILE"
fi fi
fi fi
@@ -60,7 +60,7 @@ rotate_log_once() {
local size local size
size=$(get_file_size "$OPERATIONS_LOG_FILE") size=$(get_file_size "$OPERATIONS_LOG_FILE")
if [[ "$size" -gt "$oplog_max_size" ]]; then if [[ "$size" -gt "$oplog_max_size" ]]; then
mv "$OPERATIONS_LOG_FILE" "${OPERATIONS_LOG_FILE}.old" 2>/dev/null || true mv "$OPERATIONS_LOG_FILE" "${OPERATIONS_LOG_FILE}.old" 2> /dev/null || true
ensure_user_file "$OPERATIONS_LOG_FILE" ensure_user_file "$OPERATIONS_LOG_FILE"
fi fi
fi fi
@@ -81,9 +81,9 @@ log_info() {
echo -e "${BLUE}$1${NC}" echo -e "${BLUE}$1${NC}"
local timestamp local timestamp
timestamp=$(get_timestamp) timestamp=$(get_timestamp)
echo "[$timestamp] INFO: $1" >>"$LOG_FILE" 2>/dev/null || true echo "[$timestamp] INFO: $1" >> "$LOG_FILE" 2> /dev/null || true
if [[ "${MO_DEBUG:-}" == "1" ]]; then if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] INFO: $1" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "[$timestamp] INFO: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -92,9 +92,9 @@ log_success() {
echo -e " ${GREEN}${ICON_SUCCESS}${NC} $1" echo -e " ${GREEN}${ICON_SUCCESS}${NC} $1"
local timestamp local timestamp
timestamp=$(get_timestamp) timestamp=$(get_timestamp)
echo "[$timestamp] SUCCESS: $1" >>"$LOG_FILE" 2>/dev/null || true echo "[$timestamp] SUCCESS: $1" >> "$LOG_FILE" 2> /dev/null || true
if [[ "${MO_DEBUG:-}" == "1" ]]; then if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] SUCCESS: $1" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "[$timestamp] SUCCESS: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -103,9 +103,9 @@ log_warning() {
echo -e "${YELLOW}$1${NC}" echo -e "${YELLOW}$1${NC}"
local timestamp local timestamp
timestamp=$(get_timestamp) timestamp=$(get_timestamp)
echo "[$timestamp] WARNING: $1" >>"$LOG_FILE" 2>/dev/null || true echo "[$timestamp] WARNING: $1" >> "$LOG_FILE" 2> /dev/null || true
if [[ "${MO_DEBUG:-}" == "1" ]]; then if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] WARNING: $1" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "[$timestamp] WARNING: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -114,9 +114,9 @@ log_error() {
echo -e "${YELLOW}${ICON_ERROR}${NC} $1" >&2 echo -e "${YELLOW}${ICON_ERROR}${NC} $1" >&2
local timestamp local timestamp
timestamp=$(get_timestamp) timestamp=$(get_timestamp)
echo "[$timestamp] ERROR: $1" >>"$LOG_FILE" 2>/dev/null || true echo "[$timestamp] ERROR: $1" >> "$LOG_FILE" 2> /dev/null || true
if [[ "${MO_DEBUG:-}" == "1" ]]; then if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo "[$timestamp] ERROR: $1" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "[$timestamp] ERROR: $1" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -126,7 +126,7 @@ debug_log() {
echo -e "${GRAY}[DEBUG]${NC} $*" >&2 echo -e "${GRAY}[DEBUG]${NC} $*" >&2
local timestamp local timestamp
timestamp=$(get_timestamp) timestamp=$(get_timestamp)
echo "[$timestamp] DEBUG: $*" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "[$timestamp] DEBUG: $*" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -163,7 +163,7 @@ log_operation() {
local log_line="[$timestamp] [$command] $action $path" local log_line="[$timestamp] [$command] $action $path"
[[ -n "$detail" ]] && log_line+=" ($detail)" [[ -n "$detail" ]] && log_line+=" ($detail)"
echo "$log_line" >>"$OPERATIONS_LOG_FILE" 2>/dev/null || true echo "$log_line" >> "$OPERATIONS_LOG_FILE" 2> /dev/null || true
} }
# Log session start marker # Log session start marker
@@ -178,7 +178,7 @@ log_operation_session_start() {
{ {
echo "" echo ""
echo "# ========== $command session started at $timestamp ==========" echo "# ========== $command session started at $timestamp =========="
} >>"$OPERATIONS_LOG_FILE" 2>/dev/null || true } >> "$OPERATIONS_LOG_FILE" 2> /dev/null || true
} }
# shellcheck disable=SC2329 # shellcheck disable=SC2329
@@ -193,14 +193,14 @@ log_operation_session_end() {
local size_human="" local size_human=""
if [[ "$size" =~ ^[0-9]+$ ]] && [[ "$size" -gt 0 ]]; then if [[ "$size" =~ ^[0-9]+$ ]] && [[ "$size" -gt 0 ]]; then
size_human=$(bytes_to_human "$((size * 1024))" 2>/dev/null || echo "${size}KB") size_human=$(bytes_to_human "$((size * 1024))" 2> /dev/null || echo "${size}KB")
else else
size_human="0B" size_human="0B"
fi fi
{ {
echo "# ========== $command session ended at $timestamp, $items items, $size_human ==========" echo "# ========== $command session ended at $timestamp, $items items, $size_human =========="
} >>"$OPERATIONS_LOG_FILE" 2>/dev/null || true } >> "$OPERATIONS_LOG_FILE" 2> /dev/null || true
} }
# Enhanced debug logging for operations # Enhanced debug logging for operations
@@ -218,7 +218,7 @@ debug_operation_start() {
echo "" echo ""
echo "=== $operation_name ===" echo "=== $operation_name ==="
[[ -n "$operation_desc" ]] && echo "Description: $operation_desc" [[ -n "$operation_desc" ]] && echo "Description: $operation_desc"
} >>"$DEBUG_LOG_FILE" 2>/dev/null || true } >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -232,7 +232,7 @@ debug_operation_detail() {
echo -e "${GRAY}[DEBUG] $detail_type: $detail_value${NC}" >&2 echo -e "${GRAY}[DEBUG] $detail_type: $detail_value${NC}" >&2
# Also log to file # Also log to file
echo "$detail_type: $detail_value" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "$detail_type: $detail_value" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -252,7 +252,7 @@ debug_file_action() {
echo -e "${GRAY}[DEBUG] $action: $msg${NC}" >&2 echo -e "${GRAY}[DEBUG] $action: $msg${NC}" >&2
# Also log to file # Also log to file
echo "$action: $msg" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "$action: $msg" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -264,16 +264,16 @@ debug_risk_level() {
if [[ "${MO_DEBUG:-}" == "1" ]]; then if [[ "${MO_DEBUG:-}" == "1" ]]; then
local color="$GRAY" local color="$GRAY"
case "$risk_level" in case "$risk_level" in
LOW) color="$GREEN" ;; LOW) color="$GREEN" ;;
MEDIUM) color="$YELLOW" ;; MEDIUM) color="$YELLOW" ;;
HIGH) color="$RED" ;; HIGH) color="$RED" ;;
esac esac
# Output to stderr with color # Output to stderr with color
echo -e "${GRAY}[DEBUG] Risk Level: ${color}${risk_level}${GRAY}, $reason${NC}" >&2 echo -e "${GRAY}[DEBUG] Risk Level: ${color}${risk_level}${GRAY}, $reason${NC}" >&2
# Also log to file # Also log to file
echo "Risk Level: $risk_level, $reason" >>"$DEBUG_LOG_FILE" 2>/dev/null || true echo "Risk Level: $risk_level, $reason" >> "$DEBUG_LOG_FILE" 2> /dev/null || true
fi fi
} }
@@ -285,7 +285,7 @@ log_system_info() {
# Reset debug log file for this new session # Reset debug log file for this new session
ensure_user_file "$DEBUG_LOG_FILE" ensure_user_file "$DEBUG_LOG_FILE"
if ! : >"$DEBUG_LOG_FILE" 2>/dev/null; then if ! : > "$DEBUG_LOG_FILE" 2> /dev/null; then
echo -e "${YELLOW}${ICON_WARNING}${NC} Debug log not writable: $DEBUG_LOG_FILE" >&2 echo -e "${YELLOW}${ICON_WARNING}${NC} Debug log not writable: $DEBUG_LOG_FILE" >&2
fi fi
@@ -298,19 +298,19 @@ log_system_info() {
echo "Hostname: $(hostname)" echo "Hostname: $(hostname)"
echo "Architecture: $(uname -m)" echo "Architecture: $(uname -m)"
echo "Kernel: $(uname -r)" echo "Kernel: $(uname -r)"
if command -v sw_vers >/dev/null; then if command -v sw_vers > /dev/null; then
echo "macOS: $(sw_vers -productVersion), $(sw_vers -buildVersion)" echo "macOS: $(sw_vers -productVersion), $(sw_vers -buildVersion)"
fi fi
echo "Shell: ${SHELL:-unknown}, ${TERM:-unknown}" echo "Shell: ${SHELL:-unknown}, ${TERM:-unknown}"
# Check sudo status non-interactively # Check sudo status non-interactively
if sudo -n true 2>/dev/null; then if sudo -n true 2> /dev/null; then
echo "Sudo Access: Active" echo "Sudo Access: Active"
else else
echo "Sudo Access: Required" echo "Sudo Access: Required"
fi fi
echo "----------------------------------------------------------------------" echo "----------------------------------------------------------------------"
} >>"$DEBUG_LOG_FILE" 2>/dev/null || true } >> "$DEBUG_LOG_FILE" 2> /dev/null || true
# Notification to stderr # Notification to stderr
echo -e "${GRAY}[DEBUG] Debug logging enabled. Session log: $DEBUG_LOG_FILE${NC}" >&2 echo -e "${GRAY}[DEBUG] Debug logging enabled. Session log: $DEBUG_LOG_FILE${NC}" >&2
@@ -322,7 +322,7 @@ log_system_info() {
# Run command silently (ignore errors) # Run command silently (ignore errors)
run_silent() { run_silent() {
"$@" >/dev/null 2>&1 || true "$@" > /dev/null 2>&1 || true
} }
# Run command with error logging # Run command with error logging
@@ -330,12 +330,12 @@ run_logged() {
local cmd="$1" local cmd="$1"
# Log to main file, and also to debug file if enabled # Log to main file, and also to debug file if enabled
if [[ "${MO_DEBUG:-}" == "1" ]]; then if [[ "${MO_DEBUG:-}" == "1" ]]; then
if ! "$@" 2>&1 | tee -a "$LOG_FILE" | tee -a "$DEBUG_LOG_FILE" >/dev/null; then if ! "$@" 2>&1 | tee -a "$LOG_FILE" | tee -a "$DEBUG_LOG_FILE" > /dev/null; then
log_warning "Command failed: $cmd" log_warning "Command failed: $cmd"
return 1 return 1
fi fi
else else
if ! "$@" 2>&1 | tee -a "$LOG_FILE" >/dev/null; then if ! "$@" 2>&1 | tee -a "$LOG_FILE" > /dev/null; then
log_warning "Command failed: $cmd" log_warning "Command failed: $cmd"
return 1 return 1
fi fi