mirror of
https://github.com/goodhosts/cli.git
synced 2026-04-27 07:06:00 +03:00
[GH-ISSUE #25] False assumption: IPs appear in a single position #14
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @tomjn on GitHub (Jan 13, 2021).
Original GitHub issue: https://github.com/goodhosts/cli/issues/25
https://github.com/goodhosts/hostsfile/blob/master/hosts.go#L103
The code that adds a host performs a check to see if a line with the IP is already present, and if so, it inspects the hosts on that line and only adds the host if it is not listed.
However, this assumption is not true.
For example, consider this hosts file:
Goodhosts will pick up the first line, see that
example.testis not present, and add the host, leading to duplication.Instead, this needs to be a loop, and
position := h.getIpPosition(ip)needs to bepositions := h.getIpPositions(ip).Likewise, if an existing entry is present, this code does not remove it, which can lead to hostnames being defined multiple times but with different IPs
@tomjn commented on GitHub (Jan 13, 2021):
Likewise
getHostPositiongetHostPositionandgetHostnamePositionneed plural versions@luthermonson commented on GitHub (Jan 14, 2021):
good find!
@tomjn commented on GitHub (Jan 15, 2021):
I notice that in places in the code it uses this and then loops until no instances are found, and in other places it might not do this.
Instead, if it fetched all positions, sorted them from smallest to largest, then worked backwards from the end of the file to the start, we can achieve the same result using these functions. I did start on adding the functions which seemed easy enough, but didn't look into using them, golang isn't super familiar to me. Something like this is what I was thinking: