-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add manage domains script #113
Open
PeterDaveHello
wants to merge
4
commits into
master
Choose a base branch
from
add-manage-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/bin/bash | ||
|
||
# Get directory where script is located | ||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
list_file="${SCRIPT_DIR}/list" | ||
inactive_file="${SCRIPT_DIR}/inactive" | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/bash | ||
|
||
# Create temporary test directory | ||
TEST_DIR=$(mktemp -d) | ||
trap 'rm -rf "$TEST_DIR"' EXIT | ||
|
||
# Source files | ||
list_file="../list" | ||
inactive_file="../inactive" | ||
|
||
# Test files | ||
test_list_file="${TEST_DIR}/test_list" | ||
test_inactive_file="${TEST_DIR}/test_inactive" | ||
|
||
# Backup original files | ||
cp "$list_file" "$test_list_file" | ||
cp "$inactive_file" "$test_inactive_file" | ||
|
||
# Test adding a new domain | ||
./manage_domains.sh add "newdomain.com" | ||
if grep -q "^newdomain.com$" "$test_list_file"; then | ||
echo "Test add domain: PASSED" | ||
else | ||
echo "Test add domain: FAILED" | ||
fi | ||
|
||
# Test moving an inactive domain | ||
./manage_domains.sh move "newdomain.com" | ||
if grep -q "^newdomain.com$" "$test_inactive_file" && ! grep -q "^newdomain.com$" "$test_list_file"; then | ||
echo "Test move domain: PASSED" | ||
else | ||
echo "Test move domain: FAILED" | ||
fi | ||
|
||
# Test maintaining file sorting and comments | ||
./manage_domains.sh add "anotherdomain.com" | ||
./manage_domains.sh move "anotherdomain.com" | ||
if 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"); then | ||
echo "Test file sorting and comments: PASSED" | ||
else | ||
echo "Test file sorting and comments: FAILED" | ||
fi | ||
Comment on lines
+19
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance test coverage and reporting. The test suite could be improved by:
+# Test tracking
+tests_total=0
+tests_passed=0
+
+test_case() {
+ local name=$1
+ local result=$2
+ ((tests_total++))
+ if [ "$result" = 0 ]; then
+ echo "✓ $name: PASSED"
+ ((tests_passed++))
+ else
+ echo "✗ $name: FAILED"
+ fi
+}
# Test adding a new domain
+echo "Testing domain addition..."
./manage_domains.sh add "newdomain.com"
-if grep -q "^newdomain.com$" "$test_list_file"; then
- echo "Test add domain: PASSED"
-else
- echo "Test add domain: FAILED"
-fi
+test_case "Add domain" "$(grep -q '^newdomain.com$' "$test_list_file"; echo $?)"
+
+# Test adding invalid domain
+echo "Testing invalid domain addition..."
+./manage_domains.sh add "invalid..domain"
+test_case "Reject invalid domain" "$([ $? -eq 1 ]; echo $?)"
# Additional test cases...
+# Print summary
+echo
+echo "Test Summary: $tests_passed/$tests_total tests passed"
+[ "$tests_passed" -eq "$tests_total" ]
|
||
|
||
# Restore original files | ||
mv "$test_list_file" "$list_file" | ||
mv "$test_inactive_file" "$inactive_file" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve file operations safety in move_domain function.
The current implementation has several potential issues:
📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this increase the complexity too much?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PeterDaveHello, the approach suggested by the refactoring is indeed a bit more verbose, but it helps ensure safer operations (e.g., preventing data corruption if the script is interrupted), makes it clear which sections handle comments vs. domain entries, and minimizes the risk of silent failures. If you’re concerned about complexity, you might retain the current approach (keeping it simpler) unless you regularly expect concurrent file operations or potential interruptions that could corrupt the domain files. In most cases, the added safety is a worthwhile trade-off, but it does depend on how critical failure safety is for your use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we perhaps consider a simpler implementation or explore workarounds instead of pursuing a complete, complex solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PeterDaveHello, here’s a simpler approach you could try if concurrency is not a major concern:
sed -i
in place.For instance:
This approach avoids multiple temporary files while still preserving comments by separating them from the domain lines before sorting. Although not fully atomic—interrupted runs may partially update the files—it’s often simpler and sufficient unless you routinely handle concurrent updates or need strong failure-safety guarantees.