Skip to content

Commit 2e39a4a

Browse files
Add manage domains script
1 parent fe20514 commit 2e39a4a

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ Your assistance in maintaining and improving these lists is greatly appreciated.
3131
## License
3232

3333
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/).
34+
35+
## Managing Domains
36+
37+
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.
38+
39+
### Usage
40+
41+
To add a new domain to the list file:
42+
43+
```sh
44+
./manage_domains.sh add <domain>
45+
```
46+
47+
To move an inactive domain to the inactive list:
48+
49+
```sh
50+
./manage_domains.sh move <domain>
51+
```

manage_domains.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
export LC_ALL=C
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
7+
if [ -z "$LIST_FILE" ]; then
8+
LIST_FILE="${SCRIPT_DIR}/list"
9+
fi
10+
if [ -z "$INACTIVE_FILE" ]; then
11+
INACTIVE_FILE="${SCRIPT_DIR}/inactive"
12+
fi
13+
14+
# Validate files exist
15+
for file in "$LIST_FILE" "$INACTIVE_FILE"; do
16+
if [[ ! -f "$file" ]]; then
17+
echo "Error: Required file $file not found"
18+
exit 1
19+
fi
20+
done
21+
22+
add_domain() {
23+
local domain=$1
24+
# Validate domain format
25+
if ! echo "$domain" | grep -qP '^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$'; then
26+
echo "Error: Invalid domain format: $domain"
27+
exit 1
28+
fi
29+
if ! grep -q "^$domain$" "$LIST_FILE"; then
30+
echo "$domain" >> "$LIST_FILE"
31+
# Preserve comments at top of file
32+
(grep '^#' "$LIST_FILE"; grep -v '^#' "$LIST_FILE" | sort) > "$LIST_FILE.tmp" && mv "$LIST_FILE.tmp" "$LIST_FILE"
33+
echo "Domain $domain added to $LIST_FILE."
34+
else
35+
echo "Domain $domain already exists in $LIST_FILE."
36+
fi
37+
}
38+
39+
move_domain() {
40+
local domain=$1
41+
if grep -q "^$domain$" "$LIST_FILE"; then
42+
sed -i "/^$domain$/d" "$LIST_FILE"
43+
echo "$domain" >> "$INACTIVE_FILE"
44+
(head -n 15 "$INACTIVE_FILE" && tail -n +16 "$INACTIVE_FILE" | sort) > "$INACTIVE_FILE.tmp" && mv "$INACTIVE_FILE.tmp" "$INACTIVE_FILE"
45+
echo "Domain $domain moved to $INACTIVE_FILE."
46+
else
47+
echo "Domain $domain does not exist in $LIST_FILE."
48+
fi
49+
}
50+
51+
if [ "$#" -ne 2 ]; then
52+
echo "Usage: $0 {add|move} domain"
53+
exit 1
54+
fi
55+
56+
command=$1
57+
domain=$2
58+
59+
case $command in
60+
add)
61+
add_domain "$domain"
62+
;;
63+
move)
64+
move_domain "$domain"
65+
;;
66+
*)
67+
echo "Invalid command. Use 'add' to add a domain or 'move' to move a domain."
68+
exit 1
69+
;;
70+
esac

tests/manage_domains_test.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
failures=0
5+
export LC_ALL=C
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
YELLOW='\033[1;33m'
11+
NC='\033[0m'
12+
13+
TEST_DIR=$(mktemp -d)
14+
echo -e "${YELLOW}Creating temporary test directory: $TEST_DIR${NC}"
15+
trap 'rm -rf "$TEST_DIR"' EXIT
16+
17+
echo -e "${YELLOW}Copying test files to temporary directory${NC}"
18+
list_file="${SCRIPT_DIR}/../list"
19+
inactive_file="${SCRIPT_DIR}/../inactive"
20+
21+
test_list_file="${TEST_DIR}/test_list"
22+
test_inactive_file="${TEST_DIR}/test_inactive"
23+
24+
cp "$list_file" "$test_list_file"
25+
cp "$inactive_file" "$test_inactive_file"
26+
27+
export LIST_FILE="$test_list_file"
28+
export INACTIVE_FILE="$test_inactive_file"
29+
30+
# Test helper function
31+
run_test() {
32+
local name="$1"
33+
local code="$2"
34+
if [ "$code" -eq 0 ]; then
35+
echo -e "${GREEN}Test $name: PASSED${NC}"
36+
else
37+
echo -e "${RED}Test $name: FAILED${NC}"
38+
failures=$((failures+1))
39+
fi
40+
}
41+
42+
# Testing: Add domain
43+
echo
44+
echo -e "${YELLOW}Testing: Add domain${NC}"
45+
"${SCRIPT_DIR}/../manage_domains.sh" add "newdomain.com"
46+
run_test "add domain" "$(grep -q "^newdomain.com$" "$test_list_file"; echo $?)"
47+
echo
48+
49+
# Testing: Add invalid domain
50+
echo -e "${YELLOW}Testing: Add invalid domain${NC}"
51+
if "${SCRIPT_DIR}/../manage_domains.sh" add "invalid..domain" 2>/dev/null; then
52+
run_test "reject invalid domain" 1
53+
else
54+
run_test "reject invalid domain" 0
55+
fi
56+
echo
57+
58+
# Testing: Move domain to inactive
59+
echo -e "${YELLOW}Testing: Move domain to inactive${NC}"
60+
"${SCRIPT_DIR}/../manage_domains.sh" move "newdomain.com"
61+
run_test "move domain" "$(grep -q "^newdomain.com$" "$test_inactive_file" && ! grep -q "^newdomain.com$" "$test_list_file"; echo $?)"
62+
echo
63+
64+
# Testing: Move non-existent domain
65+
echo -e "${YELLOW}Testing: Move non-existent domain${NC}"
66+
if "${SCRIPT_DIR}/../manage_domains.sh" move "nonexistentdomain.com" 2>/dev/null; then
67+
run_test "graceful handling of non-existent domain" "$( ! grep -q "^nonexistentdomain.com$" "$test_inactive_file"; echo $?)"
68+
else
69+
run_test "graceful handling of non-existent domain" 1
70+
fi
71+
echo
72+
73+
# Testing: File sorting and comment preservation
74+
echo -e "${YELLOW}Testing: File sorting and comment preservation${NC}"
75+
"${SCRIPT_DIR}/../manage_domains.sh" add "anotherdomain.com"
76+
"${SCRIPT_DIR}/../manage_domains.sh" move "anotherdomain.com"
77+
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 $?)"
78+
echo
79+
80+
# Testing: Comment preservation
81+
echo -e "${YELLOW}Testing: Comment preservation${NC}"
82+
orig_comments=$(grep '^#' "$list_file")
83+
new_comments=$(grep '^#' "$test_list_file")
84+
if [ "$orig_comments" = "$new_comments" ]; then
85+
echo -e "${GREEN}Test comment preservation (active): PASSED${NC}"
86+
else
87+
echo -e "${RED}Test comment preservation (active): FAILED${NC}"
88+
failures=$((failures+1))
89+
fi
90+
91+
orig_comments_inactive=$(grep '^#' "$inactive_file")
92+
new_comments_inactive=$(grep '^#' "$test_inactive_file")
93+
if [ "$orig_comments_inactive" = "$new_comments_inactive" ]; then
94+
echo -e "${GREEN}Test comment preservation (inactive): PASSED${NC}"
95+
else
96+
echo -e "${RED}Test comment preservation (inactive): FAILED${NC}"
97+
failures=$((failures+1))
98+
fi
99+
100+
echo
101+
echo -e "${YELLOW}Test summary:${NC}"
102+
if [ "$failures" -eq 0 ]; then
103+
echo -e "${GREEN}All tests passed${NC}"
104+
else
105+
echo -e "${RED}$failures tests failed${NC}"
106+
fi
107+
108+
exit "$failures"

0 commit comments

Comments
 (0)