Skip to content
This repository has been archived by the owner on Feb 27, 2018. It is now read-only.

Commit

Permalink
Merge pull request #356 from ahmetalpbalkan/shellinit-fix
Browse files Browse the repository at this point in the history
Fix shellinit output for cmd.exe/powershell
  • Loading branch information
tianon committed Mar 26, 2015
2 parents 7a15ad8 + b215553 commit 63bf6b5
Showing 1 changed file with 59 additions and 17 deletions.
76 changes: 59 additions & 17 deletions cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,17 @@ func cmdUp() error {
// These errors are not fatal
fmt.Fprintf(os.Stderr, "Warning: error copying certificates: %s\n", err)
}
switch runtime.GOOS {
case "windows":
fmt.Printf("Docker client does not run on Windows for now. Please use\n")
fmt.Printf(" \"%s\" ssh\n", os.Args[0])
fmt.Printf("to SSH into the VM instead.\n")
default:
if socket == "" {
fmt.Fprintf(os.Stderr, "Auto detection of the VM's Docker socket failed.\n")
fmt.Fprintf(os.Stderr, "Please run `boot2docker -v up` to diagnose.\n")

if socket == "" {
fmt.Fprintf(os.Stderr, "Auto detection of the VM's Docker socket failed.\n")
fmt.Fprintf(os.Stderr, "Please run `boot2docker -v up` to diagnose.\n")
} else {
// Check if $DOCKER_* ENV vars are properly configured.
if !checkEnvironment(socket, certPath) {
fmt.Printf("\nTo connect the Docker client to the Docker daemon, please set:\n")
printExport(socket, certPath)
} else {
// Check if $DOCKER_* ENV vars are properly configured.
if !checkEnvironment(socket, certPath) {
fmt.Printf("\nTo connect the Docker client to the Docker daemon, please set:\n")
printExport(socket, certPath)
} else {
fmt.Printf("Your environment variables are already set correctly.\n")
}
fmt.Printf("Your environment variables are already set correctly.\n")
}
}
fmt.Printf("\n")
Expand Down Expand Up @@ -189,6 +183,7 @@ func cmdShellInit() error {

func checkEnvironment(socket, certPath string) bool {
for name, value := range exports(socket, certPath) {
value = strings.Trim(value, `"'`) // remove surrounding quotes
if os.Getenv(name) != value {
return false
}
Expand All @@ -198,7 +193,20 @@ func checkEnvironment(socket, certPath string) bool {
}

func printExport(socket, certPath string) {
for name, value := range exports(socket, certPath) {
values := exports(socket, certPath)
if runtime.GOOS == "windows" && !isUnixShellOnWindows() {
printExportWindows(values)
} else {
printExportUnix(values)
}
}

func isUnixShellOnWindows() bool {
return os.Getenv("TERM") != ""
}

func printExportUnix(values map[string]string) {
for name, value := range values {
switch filepath.Base(os.Getenv("SHELL")) {
case "fish":
if value == "" {
Expand All @@ -216,12 +224,46 @@ func printExport(socket, certPath string) {
}
}

func printExportWindows(values map[string]string) {
// Print cmd.exe instructions to stderr
fmt.Fprintln(os.Stderr, "If you are running inside Windows Command Prompt (cmd.exe), copy and paste the")
fmt.Fprintln(os.Stderr, "following commands to your terminal to set the environment variables:")
for name, value := range values {
if value == "" {
fmt.Fprintf(os.Stderr, " set %s=\n", name)
} else {
fmt.Fprintf(os.Stderr, " set %s=%s\n", name, value)
}
}
fmt.Fprintln(os.Stderr, "")
// Print powershell instructions to stderr
fmt.Fprintln(os.Stderr, "If you are running inside PowerShell, copy or paste the following commands")
fmt.Fprintln(os.Stderr, `to your shell or run "boot2docker shellinit | Invoke-Expression" to set the`)
fmt.Fprintln(os.Stderr, "environment variables:")

// Print powershell exports to stdout
for name, value := range values {
if value == "" {
fmt.Printf(" Remove-Item Env:\\%s\n", name)
} else {
fmt.Printf(" $Env:%s = \"%s\"\n", name, value)
}
}
}

func exports(socket, certPath string) map[string]string {
out := make(map[string]string)

out["DOCKER_HOST"] = socket
out["DOCKER_CERT_PATH"] = certPath

if runtime.GOOS == "windows" && isUnixShellOnWindows() {
// Surround Windows-style paths with single quotes in exported path otherwise
// bash swallows the backslashes in export statements like:
// export DOCKER_CERT_PATH=C:\Users\ahmet\.boot2docker\certs\boot2docker-vm
out["DOCKER_CERT_PATH"] = fmt.Sprintf("'%s'", out["DOCKER_CERT_PATH"])
}

if certPath == "" {
out["DOCKER_TLS_VERIFY"] = ""
} else {
Expand Down

0 comments on commit 63bf6b5

Please sign in to comment.