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

Commit b215553

Browse files
committed
Fix shellinit output for cmd.exe/powershell
Also fixes the issue with Windows paths with backslashes in the bash `export` statements. When shellinit/up executed on windows, prints instructions on how to set environment variables correctly for Windows Command Prompt (cmd.exe) and PowerShell. (only the powershell set path statements are printed to stdout; the rest goes to stderr). Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
1 parent ad28233 commit b215553

1 file changed

Lines changed: 59 additions & 17 deletions

File tree

cmds.go

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,17 @@ func cmdUp() error {
138138
// These errors are not fatal
139139
fmt.Fprintf(os.Stderr, "Warning: error copying certificates: %s\n", err)
140140
}
141-
switch runtime.GOOS {
142-
case "windows":
143-
fmt.Printf("Docker client does not run on Windows for now. Please use\n")
144-
fmt.Printf(" \"%s\" ssh\n", os.Args[0])
145-
fmt.Printf("to SSH into the VM instead.\n")
146-
default:
147-
if socket == "" {
148-
fmt.Fprintf(os.Stderr, "Auto detection of the VM's Docker socket failed.\n")
149-
fmt.Fprintf(os.Stderr, "Please run `boot2docker -v up` to diagnose.\n")
141+
142+
if socket == "" {
143+
fmt.Fprintf(os.Stderr, "Auto detection of the VM's Docker socket failed.\n")
144+
fmt.Fprintf(os.Stderr, "Please run `boot2docker -v up` to diagnose.\n")
145+
} else {
146+
// Check if $DOCKER_* ENV vars are properly configured.
147+
if !checkEnvironment(socket, certPath) {
148+
fmt.Printf("\nTo connect the Docker client to the Docker daemon, please set:\n")
149+
printExport(socket, certPath)
150150
} else {
151-
// Check if $DOCKER_* ENV vars are properly configured.
152-
if !checkEnvironment(socket, certPath) {
153-
fmt.Printf("\nTo connect the Docker client to the Docker daemon, please set:\n")
154-
printExport(socket, certPath)
155-
} else {
156-
fmt.Printf("Your environment variables are already set correctly.\n")
157-
}
151+
fmt.Printf("Your environment variables are already set correctly.\n")
158152
}
159153
}
160154
fmt.Printf("\n")
@@ -189,6 +183,7 @@ func cmdShellInit() error {
189183

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

200195
func printExport(socket, certPath string) {
201-
for name, value := range exports(socket, certPath) {
196+
values := exports(socket, certPath)
197+
if runtime.GOOS == "windows" && !isUnixShellOnWindows() {
198+
printExportWindows(values)
199+
} else {
200+
printExportUnix(values)
201+
}
202+
}
203+
204+
func isUnixShellOnWindows() bool {
205+
return os.Getenv("TERM") != ""
206+
}
207+
208+
func printExportUnix(values map[string]string) {
209+
for name, value := range values {
202210
switch filepath.Base(os.Getenv("SHELL")) {
203211
case "fish":
204212
if value == "" {
@@ -216,12 +224,46 @@ func printExport(socket, certPath string) {
216224
}
217225
}
218226

227+
func printExportWindows(values map[string]string) {
228+
// Print cmd.exe instructions to stderr
229+
fmt.Fprintln(os.Stderr, "If you are running inside Windows Command Prompt (cmd.exe), copy and paste the")
230+
fmt.Fprintln(os.Stderr, "following commands to your terminal to set the environment variables:")
231+
for name, value := range values {
232+
if value == "" {
233+
fmt.Fprintf(os.Stderr, " set %s=\n", name)
234+
} else {
235+
fmt.Fprintf(os.Stderr, " set %s=%s\n", name, value)
236+
}
237+
}
238+
fmt.Fprintln(os.Stderr, "")
239+
// Print powershell instructions to stderr
240+
fmt.Fprintln(os.Stderr, "If you are running inside PowerShell, copy or paste the following commands")
241+
fmt.Fprintln(os.Stderr, `to your shell or run "boot2docker shellinit | Invoke-Expression" to set the`)
242+
fmt.Fprintln(os.Stderr, "environment variables:")
243+
244+
// Print powershell exports to stdout
245+
for name, value := range values {
246+
if value == "" {
247+
fmt.Printf(" Remove-Item Env:\\%s\n", name)
248+
} else {
249+
fmt.Printf(" $Env:%s = \"%s\"\n", name, value)
250+
}
251+
}
252+
}
253+
219254
func exports(socket, certPath string) map[string]string {
220255
out := make(map[string]string)
221256

222257
out["DOCKER_HOST"] = socket
223258
out["DOCKER_CERT_PATH"] = certPath
224259

260+
if runtime.GOOS == "windows" && isUnixShellOnWindows() {
261+
// Surround Windows-style paths with single quotes in exported path otherwise
262+
// bash swallows the backslashes in export statements like:
263+
// export DOCKER_CERT_PATH=C:\Users\ahmet\.boot2docker\certs\boot2docker-vm
264+
out["DOCKER_CERT_PATH"] = fmt.Sprintf("'%s'", out["DOCKER_CERT_PATH"])
265+
}
266+
225267
if certPath == "" {
226268
out["DOCKER_TLS_VERIFY"] = ""
227269
} else {

0 commit comments

Comments
 (0)