Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add manage domains script #113

wants to merge 4 commits into from

Conversation

PeterDaveHello
Copy link
Owner

@PeterDaveHello PeterDaveHello commented Jan 4, 2025

Summary by CodeRabbit

  • New Features

    • Added a shell script for managing domains
    • Implemented functionality to add and move domains between active and inactive lists
    • Created automated testing for domain management script
  • Documentation

    • Updated README with detailed instructions for domain management script
    • Added usage guidelines for managing domains
  • Tests

    • Introduced comprehensive test script to validate domain management functionalities

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 4, 2025
Copy link

coderabbitai bot commented Jan 4, 2025

Warning

Rate limit exceeded

@PeterDaveHello has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 28 minutes and 2 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c9907b0 and 0f85d3f.

📒 Files selected for processing (2)
  • manage_domains.sh (1 hunks)
  • tests/manage_domains_test.sh (1 hunks)
📝 Walkthrough

Walkthrough

The pull request introduces a new shell script manage_domains.sh for managing domain lists, along with accompanying documentation and tests. The script provides functionality to add new domains to an active list and move domains between active and inactive lists while maintaining sorting and preserving comments. A corresponding test script manage_domains_test.sh has been created to validate the script's core functionalities, ensuring proper domain management and list maintenance.

Changes

File Change Summary
README.md Added new "Managing Domains" section with usage instructions for manage_domains.sh
manage_domains.sh New script with add_domain() and move_domain() functions for domain list management
tests/manage_domains_test.sh New test script to validate domain management script functionalities

Sequence Diagram

sequenceDiagram
    participant User
    participant Script as manage_domains.sh
    participant ListFile as list
    participant InactiveFile as inactive

    alt Adding Domain
        User->>Script: add newdomain.com
        Script->>ListFile: Check if domain exists
        alt Domain not in list
            Script->>ListFile: Add domain
            Script->>ListFile: Sort entries
        else Domain already exists
            Script-->>User: Show existing domain message
        end
    end

    alt Moving Domain
        User->>Script: move newdomain.com
        Script->>ListFile: Check if domain exists
        alt Domain in list
            Script->>ListFile: Remove domain
            Script->>InactiveFile: Add domain
            Script->>InactiveFile: Sort entries
        else Domain not found
            Script-->>User: Show not found message
        end
    end
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@PeterDaveHello PeterDaveHello requested a review from Copilot January 4, 2025 12:13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 3 changed files in this pull request and generated no comments.

Files not reviewed (2)
  • manage_domains.sh: Language not supported
  • tests/manage_domains_test.sh: Language not supported
@dosubot dosubot bot added documentation Improvements or additions to documentation enhancement New feature or request labels Jan 4, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (3)
manage_domains.sh (1)

29-36: Enhance command-line argument validation.

The current argument validation could be more robust by validating the command format and providing more helpful error messages.

+# Validate command format
+valid_commands="add move"
+
 if [ "$#" -ne 2 ]; then
-    echo "Usage: $0 {add|move} domain"
+    echo "Error: Incorrect number of arguments"
+    echo "Usage: $(basename "$0") {add|move} domain"
+    echo "Commands:"
+    echo "  add   Add a new domain to the active list"
+    echo "  move  Move a domain to the inactive list"
     exit 1
 fi

 command=$1
 domain=$2
+
+if [[ ! " $valid_commands " =~ " $command " ]]; then
+    echo "Error: Invalid command '$command'"
+    echo "Valid commands are: $valid_commands"
+    exit 1
+fi
tests/manage_domains_test.sh (1)

38-40: Implement safer file restoration.

The current file restoration process could be improved to prevent potential data loss.

 # Restore original files
-mv "$test_list_file" "$list_file"
-mv "$test_inactive_file" "$inactive_file"
+if ! cmp -s "$test_list_file" "$list_file"; then
+    cp "$test_list_file" "$list_file" || {
+        echo "Error: Failed to restore $list_file"
+        exit 1
+    }
+fi
+if ! cmp -s "$test_inactive_file" "$inactive_file"; then
+    cp "$test_inactive_file" "$inactive_file" || {
+        echo "Error: Failed to restore $inactive_file"
+        exit 1
+    }
+fi
README.md (1)

35-51: Enhance documentation with additional details.

The documentation could be improved by:

  1. Adding examples of successful/error outputs
  2. Documenting the test script
  3. Explaining the comment preservation behavior
 ## 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.
+A shell script `manage_domains.sh` is provided to help manage the domains in the `list` and `inactive` files. The script:
+- Validates domain format
+- Maintains file sorting
+- Preserves comment headers
+- Provides clear success/error messages

 ### Usage

 To add a new domain to the list file:
 ```sh
 ./manage_domains.sh add <domain>
+# Success: Domain example.com added to list.
+# Error: Invalid domain format: invalid..domain

To move an inactive domain to the inactive list:

./manage_domains.sh move <domain>
+# Success: Domain example.com moved to inactive.
+# Error: Domain example.com does not exist in list.

+### Testing
+
+A test script manage_domains_test.sh is provided to verify the functionality:
+sh +./tests/manage_domains_test.sh +


</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between fe20514c404b6ce118c2e41a9057d47c8f028cf0 and c9907b0d37be419d1f50e4f0f85458693ee973c2.

</details>

<details>
<summary>📒 Files selected for processing (3)</summary>

* `README.md` (1 hunks)
* `manage_domains.sh` (1 hunks)
* `tests/manage_domains_test.sh` (1 hunks)

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

manage_domains.sh Outdated Show resolved Hide resolved
manage_domains.sh Outdated Show resolved Hide resolved
Comment on lines +17 to +27
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
}
Copy link

@coderabbitai coderabbitai bot Jan 4, 2025

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:

  1. Non-atomic file operations could lead to data loss
  2. sed operations could fail silently
  3. Same hardcoded line number issue as add_domain
 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"
+        # Create temporary files
+        tmp_list=$(mktemp)
+        tmp_inactive=$(mktemp)
+        
+        # Atomic operations using temporary files
+        grep -v "^$domain$" "$list_file" > "$tmp_list"
+        (echo "$domain"; cat "$inactive_file") | grep -v '^#' | sort > "$tmp_inactive"
+        
+        # Preserve comments
+        (grep '^#' "$inactive_file"; cat "$tmp_inactive") > "$inactive_file"
+        mv "$tmp_list" "$list_file"
+        
+        # Cleanup
+        rm -f "$tmp_list" "$tmp_inactive"
         echo "Domain $domain moved to $inactive_file."
     else
         echo "Domain $domain does not exist in $list_file."
     fi
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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
}
move_domain() {
local domain=$1
if grep -q "^$domain$" "$list_file"; then
# Create temporary files
tmp_list=$(mktemp)
tmp_inactive=$(mktemp)
# Atomic operations using temporary files
grep -v "^$domain$" "$list_file" > "$tmp_list"
(echo "$domain"; cat "$inactive_file") | grep -v '^#' | sort > "$tmp_inactive"
# Preserve comments
(grep '^#' "$inactive_file"; cat "$tmp_inactive") > "$inactive_file"
mv "$tmp_list" "$list_file"
# Cleanup
rm -f "$tmp_list" "$tmp_inactive"
echo "Domain $domain moved to $inactive_file."
else
echo "Domain $domain does not exist in $list_file."
fi
}

Copy link
Owner Author

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?

Copy link

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.

Copy link
Owner Author

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?

Copy link

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:

  1. Remove the domain using sed -i in place.
  2. Append the domain to the inactive file.
  3. Sort the domain lines without worrying about partially preserving the file contents in a separate temp file.

For instance:

move_domain() {
    local domain=$1
    if grep -q "^$domain$" "$list_file"; then
        # Remove the domain in place
        sed -i "/^$domain$/d" "$list_file"
        
        # Append to inactive file, then re-sort domain lines
        echo "$domain" >> "$inactive_file"
        (grep '^#' "$inactive_file"; grep -v '^#' "$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
}

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.

tests/manage_domains_test.sh Outdated Show resolved Hide resolved
Comment on lines +12 to +36
# 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
Copy link

Choose a reason for hiding this comment

The 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:

  1. Adding error case testing
  2. Tracking test status for summary
  3. Verifying file contents more comprehensively
+# 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" ]

Committable suggestion skipped: line range outside the PR's diff.

PeterDaveHello and others added 3 commits January 4, 2025 20:21
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant