Skip to content

Commit 678151b

Browse files
Add manage domains script
1 parent fe20514 commit 678151b

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
TEST_DIR=$(mktemp -d)
9+
trap 'rm -rf "$TEST_DIR"' EXIT
10+
11+
list_file="${SCRIPT_DIR}/../list"
12+
inactive_file="${SCRIPT_DIR}/../inactive"
13+
14+
test_list_file="${TEST_DIR}/test_list"
15+
test_inactive_file="${TEST_DIR}/test_inactive"
16+
17+
cp "$list_file" "$test_list_file"
18+
cp "$inactive_file" "$test_inactive_file"
19+
20+
export LIST_FILE="$test_list_file"
21+
export INACTIVE_FILE="$test_inactive_file"
22+
23+
RED='\033[0;31m'
24+
GREEN='\033[0;32m'
25+
YELLOW='\033[1;33m'
26+
NC='\033[0m'
27+
28+
echo -e "${YELLOW}Testing: Add domain${NC}"
29+
"${SCRIPT_DIR}/../manage_domains.sh" add "newdomain.com"
30+
if grep -q "^newdomain.com$" "$test_list_file"; then
31+
echo -e "${GREEN}Test add domain: PASSED${NC}"
32+
else
33+
echo -e "${RED}Test add domain: FAILED${NC}"
34+
failures=$((failures+1))
35+
fi
36+
37+
echo
38+
echo -e "${YELLOW}Testing: Move domain to inactive${NC}"
39+
"${SCRIPT_DIR}/../manage_domains.sh" move "newdomain.com"
40+
if grep -q "^newdomain.com$" "$test_inactive_file" && ! grep -q "^newdomain.com$" "$test_list_file"; then
41+
echo -e "${GREEN}Test move domain: PASSED${NC}"
42+
else
43+
echo -e "${RED}Test move domain: FAILED${NC}"
44+
failures=$((failures+1))
45+
fi
46+
47+
echo
48+
echo -e "${YELLOW}Testing: File sorting and comment preservation${NC}"
49+
"${SCRIPT_DIR}/../manage_domains.sh" add "anotherdomain.com"
50+
"${SCRIPT_DIR}/../manage_domains.sh" move "anotherdomain.com"
51+
if diff -u <(grep -v -E '^(#|$)' "$test_list_file" | sort) <(grep -v -E '^(#|$)' "$test_list_file") && \
52+
diff -u <(grep -v -E '^(#|$)' "$test_inactive_file" | sort) <(grep -v -E '^(#|$)' "$test_inactive_file"); then
53+
echo -e "${GREEN}Test file sorting and comments: PASSED${NC}"
54+
else
55+
echo -e "${RED}Test file sorting and comments: FAILED${NC}"
56+
failures=$((failures+1))
57+
fi
58+
59+
echo
60+
echo -e "${YELLOW}Test summary:${NC}"
61+
if [ "$failures" -eq 0 ]; then
62+
echo -e "${GREEN}All tests passed${NC}"
63+
else
64+
echo -e "${RED}$failures tests failed${NC}"
65+
fi
66+
67+
exit "$failures"

0 commit comments

Comments
 (0)