From 07d1041389d1cb0b8a9e69cda40e3afa762bbc72 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 21 May 2019 10:48:47 +0200 Subject: [PATCH] Restructured OS specific /etc/hosts related files (#86) --- .../{hosts_darwin.go => cfg_darwin.go} | 0 pkg/kyma/cmd/provision/minikube/cfg_others.go | 8 ++ .../cmd/provision/minikube/cfg_windows.go | 7 ++ pkg/kyma/cmd/provision/minikube/common.go | 77 ------------------- .../cmd/provision/minikube/hosts_others.go | 75 +++++++++++++++++- .../cmd/provision/minikube/hosts_windows.go | 4 - 6 files changed, 87 insertions(+), 84 deletions(-) rename pkg/kyma/cmd/provision/minikube/{hosts_darwin.go => cfg_darwin.go} (100%) create mode 100644 pkg/kyma/cmd/provision/minikube/cfg_others.go create mode 100644 pkg/kyma/cmd/provision/minikube/cfg_windows.go delete mode 100644 pkg/kyma/cmd/provision/minikube/common.go diff --git a/pkg/kyma/cmd/provision/minikube/hosts_darwin.go b/pkg/kyma/cmd/provision/minikube/cfg_darwin.go similarity index 100% rename from pkg/kyma/cmd/provision/minikube/hosts_darwin.go rename to pkg/kyma/cmd/provision/minikube/cfg_darwin.go diff --git a/pkg/kyma/cmd/provision/minikube/cfg_others.go b/pkg/kyma/cmd/provision/minikube/cfg_others.go new file mode 100644 index 000000000..a112556a1 --- /dev/null +++ b/pkg/kyma/cmd/provision/minikube/cfg_others.go @@ -0,0 +1,8 @@ +// +build !windows +// +build !darwin + +package minikube + +const hostsFile = "/etc/hosts" + +const defaultVMDriver = vmDriverNone diff --git a/pkg/kyma/cmd/provision/minikube/cfg_windows.go b/pkg/kyma/cmd/provision/minikube/cfg_windows.go new file mode 100644 index 000000000..677a80417 --- /dev/null +++ b/pkg/kyma/cmd/provision/minikube/cfg_windows.go @@ -0,0 +1,7 @@ +// +build windows + +package minikube + +const hostsFile = "C:\\Windows\\system32\\drivers\\etc\\hosts" + +const defaultVMDriver = vmDriverVirtualBox diff --git a/pkg/kyma/cmd/provision/minikube/common.go b/pkg/kyma/cmd/provision/minikube/common.go deleted file mode 100644 index 39ad2afa1..000000000 --- a/pkg/kyma/cmd/provision/minikube/common.go +++ /dev/null @@ -1,77 +0,0 @@ -// +build !windows - -package minikube - -import ( - "bytes" - "fmt" - "os" - "os/exec" - - "github.com/kyma-project/cli/internal" - "github.com/kyma-project/cli/internal/step" -) - -func isWithSudo() bool { - return os.Getenv("SUDO_UID") != "" -} - -func promptUser() bool { - for { - fmt.Print("Type [y/n]: ") - var res string - if _, err := fmt.Scanf("%s", &res); err != nil { - return false - } - switch res { - case "yes", "y": - return true - case "no", "n": - return false - default: - continue - } - } -} - -func addDevDomainsToEtcHostsOSSpecific(o *MinikubeOptions, s step.Step, hostAlias string) error { - notifyUserFunc := func(err error) { - if err != nil { - s.LogInfof("Error: %s", err.Error()) - } - s.LogInfof("Execute the following command manually to add domain entries: sudo sed -i '' \"/"+o.Domain+"/d\" "+hostsFile+" && echo '%s' | sudo tee -a /etc/hosts\r\n", hostAlias) - } - - s.LogInfo("Adding domain mappings to your 'hosts' file") - if isWithSudo() { - s.LogInfo("You're running CLI with sudo. CLI has to add Kyma domain entries to your 'hosts'. Type 'y' to allow this action") - if !promptUser() { - notifyUserFunc(nil) - return nil - } - } - _, err := internal.RunCmd("sudo", []string{ - "sed", "-i", - "''", - fmt.Sprintf("/%s/d", o.Domain), - hostsFile}) - if err != nil { - notifyUserFunc(err) - return nil - } - - cmd := exec.Command("sudo", "tee", "-a", hostsFile) - buf := &bytes.Buffer{} - _, err = fmt.Fprint(buf, hostAlias) - if err != nil { - notifyUserFunc(err) - return nil - } - cmd.Stdin = buf - err = cmd.Run() - if err != nil { - notifyUserFunc(err) - return nil - } - return nil -} diff --git a/pkg/kyma/cmd/provision/minikube/hosts_others.go b/pkg/kyma/cmd/provision/minikube/hosts_others.go index a112556a1..39ad2afa1 100644 --- a/pkg/kyma/cmd/provision/minikube/hosts_others.go +++ b/pkg/kyma/cmd/provision/minikube/hosts_others.go @@ -1,8 +1,77 @@ // +build !windows -// +build !darwin package minikube -const hostsFile = "/etc/hosts" +import ( + "bytes" + "fmt" + "os" + "os/exec" -const defaultVMDriver = vmDriverNone + "github.com/kyma-project/cli/internal" + "github.com/kyma-project/cli/internal/step" +) + +func isWithSudo() bool { + return os.Getenv("SUDO_UID") != "" +} + +func promptUser() bool { + for { + fmt.Print("Type [y/n]: ") + var res string + if _, err := fmt.Scanf("%s", &res); err != nil { + return false + } + switch res { + case "yes", "y": + return true + case "no", "n": + return false + default: + continue + } + } +} + +func addDevDomainsToEtcHostsOSSpecific(o *MinikubeOptions, s step.Step, hostAlias string) error { + notifyUserFunc := func(err error) { + if err != nil { + s.LogInfof("Error: %s", err.Error()) + } + s.LogInfof("Execute the following command manually to add domain entries: sudo sed -i '' \"/"+o.Domain+"/d\" "+hostsFile+" && echo '%s' | sudo tee -a /etc/hosts\r\n", hostAlias) + } + + s.LogInfo("Adding domain mappings to your 'hosts' file") + if isWithSudo() { + s.LogInfo("You're running CLI with sudo. CLI has to add Kyma domain entries to your 'hosts'. Type 'y' to allow this action") + if !promptUser() { + notifyUserFunc(nil) + return nil + } + } + _, err := internal.RunCmd("sudo", []string{ + "sed", "-i", + "''", + fmt.Sprintf("/%s/d", o.Domain), + hostsFile}) + if err != nil { + notifyUserFunc(err) + return nil + } + + cmd := exec.Command("sudo", "tee", "-a", hostsFile) + buf := &bytes.Buffer{} + _, err = fmt.Fprint(buf, hostAlias) + if err != nil { + notifyUserFunc(err) + return nil + } + cmd.Stdin = buf + err = cmd.Run() + if err != nil { + notifyUserFunc(err) + return nil + } + return nil +} diff --git a/pkg/kyma/cmd/provision/minikube/hosts_windows.go b/pkg/kyma/cmd/provision/minikube/hosts_windows.go index 0f24bc901..85940ab6c 100644 --- a/pkg/kyma/cmd/provision/minikube/hosts_windows.go +++ b/pkg/kyma/cmd/provision/minikube/hosts_windows.go @@ -8,10 +8,6 @@ import ( "github.com/kyma-project/cli/internal/step" ) -const hostsFile = "C:\\Windows\\system32\\drivers\\etc\\hosts" - -const defaultVMDriver = vmDriverVirtualBox - func addDevDomainsToEtcHostsOSSpecific(o *MinikubeOptions, s step.Step, hostAlias string) error { s.LogErrorf("Please add these lines to your " + hostsFile + " file:")