generated from nhcarrigan/template
feat: rework scripts to be platform/user agnostic
This commit is contained in:
@@ -1,88 +1,136 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Color definitions
|
||||
RESET='\033[0m'
|
||||
BOLD='\033[1m'
|
||||
RED='\033[91m'
|
||||
GREEN='\033[92m'
|
||||
YELLOW='\033[93m'
|
||||
BLUE='\033[94m'
|
||||
MAGENTA='\033[95m'
|
||||
CYAN='\033[96m'
|
||||
|
||||
# Directory containing YAML files
|
||||
MATCH_DIR="match"
|
||||
|
||||
# Check if directory exists
|
||||
if [ ! -d "$MATCH_DIR" ]; then
|
||||
echo "Error: Directory '$MATCH_DIR' not found!"
|
||||
echo -e "${RED}${BOLD}Error:${RESET} ${RED}Directory '$MATCH_DIR' not found!${RESET}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Temporary file to store all triggers
|
||||
ALL_TRIGGERS=$(mktemp)
|
||||
ALL_REPLACES=$(mktemp)
|
||||
# Temporary files to store triggers and replaces with metadata
|
||||
# Format: trigger|replace|file|trigger_line|replace_line
|
||||
ALL_TRIGGERS_DATA=$(mktemp)
|
||||
ALL_REPLACES_DATA=$(mktemp)
|
||||
|
||||
# Find all YAML files and extract triggers
|
||||
echo "Scanning YAML files in $MATCH_DIR directory..."
|
||||
# Find all YAML files and extract triggers with their replace values and line numbers
|
||||
echo -e "${CYAN}${BOLD}Scanning YAML files in $MATCH_DIR directory...${RESET}"
|
||||
for file in "$MATCH_DIR"/*.y*ml; do
|
||||
if [ -f "$file" ]; then
|
||||
echo "Processing file: $(basename "$file")"
|
||||
# Extract triggers using grep and sed
|
||||
# This looks for lines with "trigger:" and captures the value
|
||||
grep -E '^\s*-\s*trigger:' "$file" | sed 's/.*trigger:\s*"\([^"]*\)".*/\1/' >> "$ALL_TRIGGERS"
|
||||
# Also extract replace values if they exist
|
||||
grep -E '^\s*replace:' "$file" | sed 's/.*replace:\s*"\([^"]*\)".*/\1/' >> "$ALL_REPLACES"
|
||||
echo -e "${BLUE}Processing file:${RESET} ${BOLD}$(basename "$file")${RESET}"
|
||||
# Process file line by line to track trigger-replace pairs
|
||||
trigger_line=""
|
||||
trigger_value=""
|
||||
replace_value=""
|
||||
replace_line=""
|
||||
line_num=0
|
||||
|
||||
while IFS= read -r line; do
|
||||
line_num=$((line_num + 1))
|
||||
|
||||
# Check if this is a trigger line
|
||||
if echo "$line" | grep -qE '^\s*-\s*trigger:'; then
|
||||
trigger_line=$line_num
|
||||
trigger_value=$(echo "$line" | sed 's/.*trigger:\s*"\([^"]*\)".*/\1/')
|
||||
replace_value="" # Reset replace value for new trigger
|
||||
replace_line="" # Reset replace line
|
||||
# Check if this is a replace line (should be right after trigger)
|
||||
elif echo "$line" | grep -qE '^\s*replace:'; then
|
||||
replace_value=$(echo "$line" | sed 's/.*replace:\s*"\([^"]*\)".*/\1/')
|
||||
replace_line=$line_num
|
||||
# Save trigger data: trigger|replace|file|trigger_line|replace_line
|
||||
if [ -n "$trigger_value" ]; then
|
||||
echo "$trigger_value|$replace_value|$(basename "$file")|$trigger_line|$replace_line" >> "$ALL_TRIGGERS_DATA"
|
||||
fi
|
||||
fi
|
||||
done < "$file"
|
||||
else
|
||||
echo "Warning: No YAML files found in $MATCH_DIR directory!"
|
||||
echo -e "${YELLOW}${BOLD}Warning:${RESET} ${YELLOW}No YAML files found in $MATCH_DIR directory!${RESET}"
|
||||
continue
|
||||
fi
|
||||
done
|
||||
|
||||
# Also create a reverse mapping for replace values: replace|trigger|file|trigger_line|replace_line
|
||||
awk -F'|' '{print $2"|"$1"|"$3"|"$4"|"$5}' "$ALL_TRIGGERS_DATA" > "$ALL_REPLACES_DATA"
|
||||
|
||||
# Check if we found any triggers
|
||||
if [ ! -s "$ALL_TRIGGERS" ]; then
|
||||
echo "No triggers found in YAML files. Check file format or directory."
|
||||
rm "$ALL_TRIGGERS"
|
||||
if [ ! -s "$ALL_TRIGGERS_DATA" ]; then
|
||||
echo -e "${RED}${BOLD}Error:${RESET} ${RED}No triggers found in YAML files. Check file format or directory.${RESET}"
|
||||
rm "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Extract just triggers to find duplicates
|
||||
ALL_TRIGGERS=$(mktemp)
|
||||
cut -d'|' -f1 "$ALL_TRIGGERS_DATA" > "$ALL_TRIGGERS"
|
||||
|
||||
# Sort triggers and find duplicates
|
||||
echo -e "\nChecking for duplicate triggers..."
|
||||
echo -e "\n${CYAN}${BOLD}Checking for duplicate triggers...${RESET}"
|
||||
DUPLICATE_TRIGGERS=$(sort "$ALL_TRIGGERS" | uniq -d)
|
||||
|
||||
# Display results
|
||||
if [ -z "$DUPLICATE_TRIGGERS" ]; then
|
||||
echo "Success: All triggers are unique!"
|
||||
echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All triggers are unique!${RESET}"
|
||||
TOTAL=$(wc -l < "$ALL_TRIGGERS")
|
||||
echo "Total number of triggers found: $TOTAL"
|
||||
echo -e "${CYAN}Total number of triggers found:${RESET} ${BOLD}$TOTAL${RESET}"
|
||||
else
|
||||
echo "Error: Duplicate triggers found:"
|
||||
echo "$DUPLICATE_TRIGGERS"
|
||||
echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Duplicate triggers found:${RESET}"
|
||||
echo -e "${YELLOW}$DUPLICATE_TRIGGERS${RESET}"
|
||||
|
||||
# Optional: Show which files contain each duplicate
|
||||
echo -e "\nDuplicates found in these files:"
|
||||
# Show detailed information for each duplicate
|
||||
echo -e "\n${CYAN}${BOLD}Detailed information for duplicates:${RESET}"
|
||||
for dupe in $DUPLICATE_TRIGGERS; do
|
||||
echo "Trigger \"$dupe\" found in:"
|
||||
grep -l "trigger: \"$dupe\"" "$MATCH_DIR"/*.y*ml
|
||||
echo -e "\n${MAGENTA}${BOLD}Trigger${RESET} ${BOLD}\"$dupe\"${RESET} ${MAGENTA}found:${RESET}"
|
||||
grep "^$dupe|" "$ALL_TRIGGERS_DATA" | while IFS='|' read -r trigger replace file trigger_line replace_line; do
|
||||
echo -e " ${YELLOW}→${RESET} Line ${MAGENTA}${BOLD}$trigger_line${RESET} in ${BLUE}$file${RESET}: ${CYAN}replace=${RESET}\"${GREEN}$replace${RESET}\""
|
||||
done
|
||||
done
|
||||
rm "$ALL_TRIGGERS" "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Extract just replace values to find duplicates
|
||||
ALL_REPLACES=$(mktemp)
|
||||
cut -d'|' -f1 "$ALL_REPLACES_DATA" > "$ALL_REPLACES"
|
||||
|
||||
# Sort replace values and find duplicates
|
||||
echo -e "\nChecking for duplicate replace values..."
|
||||
echo -e "\n${CYAN}${BOLD}Checking for duplicate replace values...${RESET}"
|
||||
DUPLICATE_REPLACES=$(sort "$ALL_REPLACES" | uniq -d)
|
||||
|
||||
# Display results
|
||||
if [ -z "$DUPLICATE_REPLACES" ]; then
|
||||
echo "Success: All replace values are unique!"
|
||||
echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All replace values are unique!${RESET}"
|
||||
TOTAL_REPLACES=$(wc -l < "$ALL_REPLACES")
|
||||
echo "Total number of replace values found: $TOTAL_REPLACES"
|
||||
echo -e "${CYAN}Total number of replace values found:${RESET} ${BOLD}$TOTAL_REPLACES${RESET}"
|
||||
else
|
||||
echo "Error: Duplicate replace values found:"
|
||||
echo "$DUPLICATE_REPLACES"
|
||||
echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Duplicate replace values found:${RESET}"
|
||||
echo -e "${YELLOW}$DUPLICATE_REPLACES${RESET}"
|
||||
|
||||
# Optional: Show which files contain each duplicate replace value
|
||||
echo -e "\nDuplicates found in these files:"
|
||||
# Show detailed information for each duplicate
|
||||
echo -e "\n${CYAN}${BOLD}Detailed information for duplicates:${RESET}"
|
||||
for dupe in $DUPLICATE_REPLACES; do
|
||||
echo "Replace value \"$dupe\" found in:"
|
||||
grep -l "replace: \"$dupe\"" "$MATCH_DIR"/*.y*ml
|
||||
echo -e "\n${MAGENTA}${BOLD}Replace value${RESET} ${BOLD}\"$dupe\"${RESET} ${MAGENTA}found:${RESET}"
|
||||
grep "^$dupe|" "$ALL_REPLACES_DATA" | while IFS='|' read -r replace trigger file trigger_line replace_line; do
|
||||
echo -e " ${YELLOW}→${RESET} Line ${MAGENTA}${BOLD}$replace_line${RESET} in ${BLUE}$file${RESET}: ${CYAN}trigger=${RESET}\"${GREEN}$trigger${RESET}\""
|
||||
done
|
||||
done
|
||||
rm "$ALL_TRIGGERS" "$ALL_REPLACES" "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
rm "$ALL_TRIGGERS"
|
||||
rm "$ALL_REPLACES"
|
||||
rm "$ALL_TRIGGERS" "$ALL_REPLACES" "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
|
||||
|
||||
### Now we test that all gifs.yml replace values match "c.tenor.com".
|
||||
|
||||
@@ -90,31 +138,31 @@ rm "$ALL_REPLACES"
|
||||
REPLACE_VALUES=$(mktemp)
|
||||
|
||||
# Read gifs.yml file and extract replace values
|
||||
echo "Scanning gifs.yml for replace values..."
|
||||
echo -e "${CYAN}${BOLD}Scanning gifs.yml for replace values...${RESET}"
|
||||
if [ -f "$MATCH_DIR/gifs.yml" ]; then
|
||||
# We match lines like: ` replace: "https://c.tenor.com/some-value/tenor.gif"`
|
||||
grep -E '^\s*replace:' "$MATCH_DIR/gifs.yml" | sed 's/.*replace:\s*"\([^"]*\)".*/\1/' >> "$REPLACE_VALUES"
|
||||
else
|
||||
echo "Error: gifs.yml file not found in $MATCH_DIR directory!"
|
||||
echo -e "${RED}${BOLD}Error:${RESET} ${RED}gifs.yml file not found in $MATCH_DIR directory!${RESET}"
|
||||
rm "$REPLACE_VALUES"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if we found any replace values
|
||||
if [ ! -s "$REPLACE_VALUES" ]; then
|
||||
echo "No replace values found in gifs.yml. Check file format."
|
||||
echo -e "${RED}${BOLD}Error:${RESET} ${RED}No replace values found in gifs.yml. Check file format.${RESET}"
|
||||
rm "$REPLACE_VALUES"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if all replace values start with "https://c.tenor.com/"
|
||||
echo -e "\nChecking replace values in gifs.yml..."
|
||||
echo -e "\n${CYAN}${BOLD}Checking replace values in gifs.yml...${RESET}"
|
||||
INVALID_REPLACE=$(grep -v '^https://c\.tenor\.com/' "$REPLACE_VALUES")
|
||||
if [ -z "$INVALID_REPLACE" ]; then
|
||||
echo "Success: All replace values in gifs.yml are valid!"
|
||||
echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All replace values in gifs.yml are valid!${RESET}"
|
||||
else
|
||||
echo "Error: Invalid replace values found in gifs.yml:"
|
||||
echo "$INVALID_REPLACE"
|
||||
echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Invalid replace values found in gifs.yml:${RESET}"
|
||||
echo -e "${YELLOW}$INVALID_REPLACE${RESET}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user