Skip to content

Commit

Permalink
Restructured OS specific /etc/hosts related files (kyma-project#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor authored May 21, 2019
1 parent d042ac9 commit 07d1041
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 84 deletions.
8 changes: 8 additions & 0 deletions pkg/kyma/cmd/provision/minikube/cfg_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build !windows
// +build !darwin

package minikube

const hostsFile = "/etc/hosts"

const defaultVMDriver = vmDriverNone
7 changes: 7 additions & 0 deletions pkg/kyma/cmd/provision/minikube/cfg_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build windows

package minikube

const hostsFile = "C:\\Windows\\system32\\drivers\\etc\\hosts"

const defaultVMDriver = vmDriverVirtualBox
77 changes: 0 additions & 77 deletions pkg/kyma/cmd/provision/minikube/common.go

This file was deleted.

75 changes: 72 additions & 3 deletions pkg/kyma/cmd/provision/minikube/hosts_others.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 0 additions & 4 deletions pkg/kyma/cmd/provision/minikube/hosts_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand Down

0 comments on commit 07d1041

Please sign in to comment.