Make the add-to-hosts-file regex stricter to avoid incorrect matches #31
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.
What does this change?
There's a grep call in add-to-hosts-file which aims to prevent adding duplicate entries. Unfortunately the regex is naive so will match a domain even if it doesn't exist and is only a substring of an existing domain. For example, if we've already added
api.media.local.dev-gutools.co.uk, then the command will not allow us to addmedia.local.dev-gutools.co.ukas that domain appears to already exist.Improve the match with grep's extended regex to match the entire line, from IP address, delimiting whitespace to domain name and end-of-line marker. Now dev-nginx should correctly deduplicate entries.
How to test
Run
add-to-hosts-file api.media.local.dev-gutools.co.ukthenadd-to-hosts-file media.local.dev-gutools.co.uk- you should not get a message that the entry already exists. (remember to clean up your /etc/hosts file afterwards!)