diff --git a/README.md b/README.md index 92baaef..1cb56a9 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,21 @@ Your assistance in maintaining and improving these lists is greatly appreciated. ## License This project is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License(CC-BY-SA-4.0)](https://creativecommons.org/licenses/by-sa/4.0/). + +## Managing Domains + +A shell script `manage_domains.sh` is provided to help manage the domains in the `list` and `inactive` files. The script ensures that the files remain sorted and comments are not affected. + +### Usage + +To add a new domain to the list file: + +```sh +./manage_domains.sh add +``` + +To move an inactive domain to the inactive list: + +```sh +./manage_domains.sh move +``` diff --git a/manage_domains.sh b/manage_domains.sh new file mode 100755 index 0000000..f35c3e9 --- /dev/null +++ b/manage_domains.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +export LC_ALL=C + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +if [ -z "$LIST_FILE" ]; then + LIST_FILE="${SCRIPT_DIR}/list" +fi +if [ -z "$INACTIVE_FILE" ]; then + INACTIVE_FILE="${SCRIPT_DIR}/inactive" +fi + +# Validate files exist +for file in "$LIST_FILE" "$INACTIVE_FILE"; do + if [[ ! -f "$file" ]]; then + echo "Error: Required file $file not found" + exit 1 + fi +done + +add_domain() { + local domain=$1 + # Validate domain format + if ! echo "$domain" | grep -qP '^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$'; then + echo "Error: Invalid domain format: $domain" + exit 1 + fi + if ! grep -q "^$domain$" "$LIST_FILE"; then + echo "$domain" >> "$LIST_FILE" + # Preserve comments at top of file + (grep '^#' "$LIST_FILE"; grep -v '^#' "$LIST_FILE" | sort) > "$LIST_FILE.tmp" && mv "$LIST_FILE.tmp" "$LIST_FILE" + echo "Domain $domain added to $LIST_FILE." + else + echo "Domain $domain already exists in $LIST_FILE." + fi +} + +move_domain() { + local domain=$1 + if grep -q "^$domain$" "$LIST_FILE"; then + sed -i "/^$domain$/d" "$LIST_FILE" + echo "$domain" >> "$INACTIVE_FILE" + (head -n 15 "$INACTIVE_FILE" && tail -n +16 "$INACTIVE_FILE" | sort) > "$INACTIVE_FILE.tmp" && mv "$INACTIVE_FILE.tmp" "$INACTIVE_FILE" + echo "Domain $domain moved to $INACTIVE_FILE." + else + echo "Domain $domain does not exist in $LIST_FILE." + fi +} + +if [ "$#" -ne 2 ]; then + echo "Usage: $0 {add|move} domain" + exit 1 +fi + +command=$1 +domain=$2 + +case $command in + add) + add_domain "$domain" + ;; + move) + move_domain "$domain" + ;; + *) + echo "Invalid command. Use 'add' to add a domain or 'move' to move a domain." + exit 1 + ;; +esac diff --git a/tests/manage_domains_test.sh b/tests/manage_domains_test.sh new file mode 100755 index 0000000..3be6a4a --- /dev/null +++ b/tests/manage_domains_test.sh @@ -0,0 +1,108 @@ +#!/bin/bash +set -euo pipefail + +failures=0 +export LC_ALL=C +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +TEST_DIR=$(mktemp -d) +echo -e "${YELLOW}Creating temporary test directory: $TEST_DIR${NC}" +trap 'rm -rf "$TEST_DIR"' EXIT + +echo -e "${YELLOW}Copying test files to temporary directory${NC}" +list_file="${SCRIPT_DIR}/../list" +inactive_file="${SCRIPT_DIR}/../inactive" + +test_list_file="${TEST_DIR}/test_list" +test_inactive_file="${TEST_DIR}/test_inactive" + +cp "$list_file" "$test_list_file" +cp "$inactive_file" "$test_inactive_file" + +export LIST_FILE="$test_list_file" +export INACTIVE_FILE="$test_inactive_file" + +# Test helper function +run_test() { + local name="$1" + local code="$2" + if [ "$code" -eq 0 ]; then + echo -e "${GREEN}Test $name: PASSED${NC}" + else + echo -e "${RED}Test $name: FAILED${NC}" + failures=$((failures+1)) + fi +} + +# Testing: Add domain +echo +echo -e "${YELLOW}Testing: Add domain${NC}" +"${SCRIPT_DIR}/../manage_domains.sh" add "newdomain.com" +run_test "add domain" "$(grep -q "^newdomain.com$" "$test_list_file"; echo $?)" +echo + +# Testing: Add invalid domain +echo -e "${YELLOW}Testing: Add invalid domain${NC}" +if "${SCRIPT_DIR}/../manage_domains.sh" add "invalid..domain" 2>/dev/null; then + run_test "reject invalid domain" 1 +else + run_test "reject invalid domain" 0 +fi +echo + +# Testing: Move domain to inactive +echo -e "${YELLOW}Testing: Move domain to inactive${NC}" +"${SCRIPT_DIR}/../manage_domains.sh" move "newdomain.com" +run_test "move domain" "$(grep -q "^newdomain.com$" "$test_inactive_file" && ! grep -q "^newdomain.com$" "$test_list_file"; echo $?)" +echo + +# Testing: Move non-existent domain +echo -e "${YELLOW}Testing: Move non-existent domain${NC}" +if "${SCRIPT_DIR}/../manage_domains.sh" move "nonexistentdomain.com" 2>/dev/null; then + run_test "graceful handling of non-existent domain" "$( ! grep -q "^nonexistentdomain.com$" "$test_inactive_file"; echo $?)" +else + run_test "graceful handling of non-existent domain" 1 +fi +echo + +# Testing: File sorting and comment preservation +echo -e "${YELLOW}Testing: File sorting and comment preservation${NC}" +"${SCRIPT_DIR}/../manage_domains.sh" add "anotherdomain.com" +"${SCRIPT_DIR}/../manage_domains.sh" move "anotherdomain.com" +run_test "file sorting and comments" "$(diff -u <(grep -v -E '^(#|$)' "$test_list_file" | sort) <(grep -v -E '^(#|$)' "$test_list_file") && diff -u <(grep -v -E '^(#|$)' "$test_inactive_file" | sort) <(grep -v -E '^(#|$)' "$test_inactive_file"); echo $?)" +echo + +# Testing: Comment preservation +echo -e "${YELLOW}Testing: Comment preservation${NC}" +orig_comments=$(grep '^#' "$list_file") +new_comments=$(grep '^#' "$test_list_file") +if [ "$orig_comments" = "$new_comments" ]; then + echo -e "${GREEN}Test comment preservation (active): PASSED${NC}" +else + echo -e "${RED}Test comment preservation (active): FAILED${NC}" + failures=$((failures+1)) +fi + +orig_comments_inactive=$(grep '^#' "$inactive_file") +new_comments_inactive=$(grep '^#' "$test_inactive_file") +if [ "$orig_comments_inactive" = "$new_comments_inactive" ]; then + echo -e "${GREEN}Test comment preservation (inactive): PASSED${NC}" +else + echo -e "${RED}Test comment preservation (inactive): FAILED${NC}" + failures=$((failures+1)) +fi + +echo +echo -e "${YELLOW}Test summary:${NC}" +if [ "$failures" -eq 0 ]; then + echo -e "${GREEN}All tests passed${NC}" +else + echo -e "${RED}$failures tests failed${NC}" +fi + +exit "$failures"