Skip to content

Commit

Permalink
Use clean to cleanup the host file for windows
Browse files Browse the repository at this point in the history
Clean() functionality in the goodhost library try to
do cleanup the hosts file which remove duplicate entry
and also make sure for windows each line have max 9 hosts per
IP address.

```
> .\crc-admin-helper.exe add 192.168.0.1 foo.apps-crc.testing b.apps-crc.testing c.apps-crc.testing d.apps-crc.testing e.apps-crc.testing f.apps-crc.testing g.apps-crc.testing h.apps-crc.testing
i.apps-crc.testing j.apps-crc.testing

> cat 'C:\Windows\System32\drivers\etc\hosts'
192.168.0.1 b.apps-crc.testing c.apps-crc.testing d.apps-crc.testing e.apps-crc.testing f.apps-crc.testing foo.apps-crc.testing g.apps-crc.testing h.apps-crc.testing i.apps-crc.testing
192.168.0.1 j.apps-crc.testing test.apps-crc.testing

```
  • Loading branch information
praveenkumar committed Oct 13, 2021
1 parent 4eb2d25 commit 3440409
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hosts
import (
"fmt"
"regexp"
"runtime"
"sort"
"strings"

Expand Down Expand Up @@ -66,6 +67,11 @@ func (h *Hosts) Add(ip string, hosts []string) error {
if err := h.File.Add(ip, hostEntries...); err != nil {
return err
}
// Only execute clean in case of windows to avoid more than
// 9 domain entry in a single line
if runtime.GOOS == "windows" {
h.File.Clean()
}
return h.File.Flush()
}

Expand Down

0 comments on commit 3440409

Please sign in to comment.