Skip to content

Commit 3440409

Browse files
committed
Use clean to cleanup the host file for windows
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 ```
1 parent 4eb2d25 commit 3440409

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pkg/hosts/hosts.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hosts
33
import (
44
"fmt"
55
"regexp"
6+
"runtime"
67
"sort"
78
"strings"
89

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

0 commit comments

Comments
 (0)