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

Merge branch 'dev'

This commit is contained in:
Tw93
2026-01-12 17:50:07 +08:00
13 changed files with 353 additions and 134 deletions

View File

@@ -47,21 +47,119 @@ start_purge() {
printf '\033[2J\033[H'
fi
printf '\n'
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
# Initialize stats file in user cache directory
local stats_dir="${XDG_CACHE_HOME:-$HOME/.cache}/mole"
ensure_user_dir "$stats_dir"
ensure_user_file "$stats_dir/purge_stats"
ensure_user_file "$stats_dir/purge_count"
ensure_user_file "$stats_dir/purge_scanning"
echo "0" > "$stats_dir/purge_stats"
echo "0" > "$stats_dir/purge_count"
echo "" > "$stats_dir/purge_scanning"
}
# Perform the purge
perform_purge() {
local stats_dir="${XDG_CACHE_HOME:-$HOME/.cache}/mole"
local monitor_pid=""
# Cleanup function
cleanup_monitor() {
# Remove scanning file to stop monitor
rm -f "$stats_dir/purge_scanning" 2> /dev/null || true
if [[ -n "$monitor_pid" ]]; then
kill "$monitor_pid" 2> /dev/null || true
wait "$monitor_pid" 2> /dev/null || true
fi
if [[ -t 1 ]]; then
printf '\r\033[K\n\033[K\033[A'
fi
}
# Set up trap for cleanup
trap cleanup_monitor INT TERM
# Show scanning with spinner on same line as title
if [[ -t 1 ]]; then
# Print title first
printf '%s' "${PURPLE_BOLD}Purge Project Artifacts${NC} "
# Start background monitor with ASCII spinner
(
local spinner_chars="|/-\\"
local spinner_idx=0
local last_path=""
# Set up trap to exit cleanly
trap 'exit 0' INT TERM
# Function to truncate path in the middle
truncate_path() {
local path="$1"
local max_len=80
if [[ ${#path} -le $max_len ]]; then
echo "$path"
return
fi
# Calculate how much to show on each side
local side_len=$(( (max_len - 3) / 2 ))
local start="${path:0:$side_len}"
local end="${path: -$side_len}"
echo "${start}...${end}"
}
while [[ -f "$stats_dir/purge_scanning" ]]; do
local current_path=$(cat "$stats_dir/purge_scanning" 2> /dev/null || echo "")
local display_path=""
if [[ -n "$current_path" ]]; then
display_path="${current_path/#$HOME/~}"
display_path=$(truncate_path "$display_path")
last_path="$display_path"
elif [[ -n "$last_path" ]]; then
display_path="$last_path"
fi
# Get current spinner character
local spin_char="${spinner_chars:$spinner_idx:1}"
spinner_idx=$(( (spinner_idx + 1) % ${#spinner_chars} ))
# Show title on first line, spinner and scanning info on second line
if [[ -n "$display_path" ]]; then
printf '\r%s\n%s %sScanning %s\033[K\033[A' \
"${PURPLE_BOLD}Purge Project Artifacts${NC}" \
"${BLUE}${spin_char}${NC}" \
"${GRAY}" "$display_path"
else
printf '\r%s\n%s %sScanning...\033[K\033[A' \
"${PURPLE_BOLD}Purge Project Artifacts${NC}" \
"${BLUE}${spin_char}${NC}" \
"${GRAY}"
fi
sleep 0.05
done
exit 0
) &
monitor_pid=$!
else
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
fi
clean_project_artifacts
local exit_code=$?
# Clean up
trap - INT TERM
cleanup_monitor
if [[ -t 1 ]]; then
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
fi
# Exit codes:
# 0 = success, show summary
@@ -79,15 +177,11 @@ perform_purge() {
local total_size_cleaned=0
local total_items_cleaned=0
# Read stats from user cache directory
local stats_dir="${XDG_CACHE_HOME:-$HOME/.cache}/mole"
if [[ -f "$stats_dir/purge_stats" ]]; then
total_size_cleaned=$(cat "$stats_dir/purge_stats" 2> /dev/null || echo "0")
rm -f "$stats_dir/purge_stats"
fi
# Read count
if [[ -f "$stats_dir/purge_count" ]]; then
total_items_cleaned=$(cat "$stats_dir/purge_count" 2> /dev/null || echo "0")
rm -f "$stats_dir/purge_count"

View File

@@ -13,8 +13,13 @@ LIB_DIR="$(cd "$SCRIPT_DIR/../lib" && pwd)"
# shellcheck source=../lib/core/common.sh
source "$LIB_DIR/core/common.sh"
readonly PAM_SUDO_FILE="${MOLE_PAM_SUDO_FILE:-/etc/pam.d/sudo}"
readonly PAM_SUDO_LOCAL_FILE="${MOLE_PAM_SUDO_LOCAL_FILE:-/etc/pam.d/sudo_local}"
# Set up global cleanup trap
trap cleanup_temp_files EXIT INT TERM
PAM_SUDO_FILE="${MOLE_PAM_SUDO_FILE:-/etc/pam.d/sudo}"
PAM_SUDO_LOCAL_FILE="${MOLE_PAM_SUDO_LOCAL_FILE:-$(dirname "$PAM_SUDO_FILE")/sudo_local}"
readonly PAM_SUDO_FILE
readonly PAM_SUDO_LOCAL_FILE
readonly PAM_TID_LINE="auth sufficient pam_tid.so"
# Check if Touch ID is already configured
@@ -66,9 +71,8 @@ show_status() {
# Enable Touch ID for sudo
enable_touchid() {
# Cleanup trap
# Cleanup trap handled by global EXIT trap
local temp_file=""
trap '[[ -n "${temp_file:-}" ]] && rm -f "${temp_file:-}"' EXIT
# First check if system supports Touch ID
if ! supports_touchid; then
@@ -88,7 +92,7 @@ enable_touchid() {
# It is in sudo_local, but let's check if it's ALSO in sudo (incomplete migration)
if grep -q "pam_tid.so" "$PAM_SUDO_FILE"; then
# Clean up legacy config
temp_file=$(mktemp)
temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
if sudo mv "$temp_file" "$PAM_SUDO_FILE" 2> /dev/null; then
echo -e "${GREEN}${ICON_SUCCESS} Cleanup legacy configuration${NC}"
@@ -117,7 +121,7 @@ enable_touchid() {
else
# Append if not present
if ! grep -q "pam_tid.so" "$PAM_SUDO_LOCAL_FILE"; then
temp_file=$(mktemp)
temp_file=$(create_temp_file)
cp "$PAM_SUDO_LOCAL_FILE" "$temp_file"
echo "$PAM_TID_LINE" >> "$temp_file"
sudo mv "$temp_file" "$PAM_SUDO_LOCAL_FILE"
@@ -132,7 +136,7 @@ enable_touchid() {
if $write_success; then
# If we migrated from legacy, clean it up now
if $is_legacy_configured; then
temp_file=$(mktemp)
temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
sudo mv "$temp_file" "$PAM_SUDO_FILE"
log_success "Touch ID migrated to sudo_local"
@@ -163,7 +167,7 @@ enable_touchid() {
fi
# Create temp file
temp_file=$(mktemp)
temp_file=$(create_temp_file)
# Insert pam_tid.so after the first comment block
awk '
@@ -194,9 +198,8 @@ enable_touchid() {
# Disable Touch ID for sudo
disable_touchid() {
# Cleanup trap
# Cleanup trap handled by global EXIT trap
local temp_file=""
trap '[[ -n "${temp_file:-}" ]] && rm -f "${temp_file:-}"' EXIT
if ! is_touchid_configured; then
echo -e "${YELLOW}Touch ID is not currently enabled${NC}"
@@ -206,13 +209,13 @@ disable_touchid() {
# Check sudo_local first
if [[ -f "$PAM_SUDO_LOCAL_FILE" ]] && grep -q "pam_tid.so" "$PAM_SUDO_LOCAL_FILE"; then
# Remove from sudo_local
temp_file=$(mktemp)
temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_LOCAL_FILE" > "$temp_file"
if sudo mv "$temp_file" "$PAM_SUDO_LOCAL_FILE" 2> /dev/null; then
# Since we modified sudo_local, we should also check if it's in sudo file (legacy cleanup)
if grep -q "pam_tid.so" "$PAM_SUDO_FILE"; then
temp_file=$(mktemp)
temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
sudo mv "$temp_file" "$PAM_SUDO_FILE"
fi
@@ -236,7 +239,7 @@ disable_touchid() {
fi
# Remove pam_tid.so line
temp_file=$(mktemp)
temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
if sudo mv "$temp_file" "$PAM_SUDO_FILE" 2> /dev/null; then