From 34404091496ec50a86eccc43dc511b5e319fc3c5 Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Mon, 13 Sep 2021 09:04:05 +0530 Subject: [PATCH] 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 ``` --- pkg/hosts/hosts.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/hosts/hosts.go b/pkg/hosts/hosts.go index c0f2cd8e..16a2b823 100644 --- a/pkg/hosts/hosts.go +++ b/pkg/hosts/hosts.go @@ -3,6 +3,7 @@ package hosts import ( "fmt" "regexp" + "runtime" "sort" "strings" @@ -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() }