mirror of https://github.com/docker/docs.git
Merge pull request #882 from tombee/env-usage-hint
Added env usage hint for bash and fish shells
This commit is contained in:
commit
1b81ddeb90
30
commands.go
30
commands.go
|
@ -727,16 +727,38 @@ func cmdEnv(c *cli.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
usageHint := generateUsageHint(c.Args().First(), userShell)
|
||||
|
||||
switch userShell {
|
||||
case "fish":
|
||||
fmt.Printf("set -x DOCKER_TLS_VERIFY 1;\nset -x DOCKER_CERT_PATH %q;\nset -x DOCKER_HOST %s;\n",
|
||||
cfg.machineDir, dockerHost)
|
||||
fmt.Printf("set -x DOCKER_TLS_VERIFY 1;\nset -x DOCKER_CERT_PATH %q;\nset -x DOCKER_HOST %s;\n\n%s\n",
|
||||
cfg.machineDir, dockerHost, usageHint)
|
||||
default:
|
||||
fmt.Printf("export DOCKER_TLS_VERIFY=1\nexport DOCKER_CERT_PATH=%q\nexport DOCKER_HOST=%s\n",
|
||||
cfg.machineDir, dockerHost)
|
||||
fmt.Printf("export DOCKER_TLS_VERIFY=1\nexport DOCKER_CERT_PATH=%q\nexport DOCKER_HOST=%s\n\n%s\n",
|
||||
cfg.machineDir, dockerHost, usageHint)
|
||||
}
|
||||
}
|
||||
|
||||
func generateUsageHint(machineName string, userShell string) string {
|
||||
cmd := ""
|
||||
switch userShell {
|
||||
case "fish":
|
||||
if machineName != "" {
|
||||
cmd = fmt.Sprintf("eval (docker-machine env %s)", machineName)
|
||||
} else {
|
||||
cmd = "eval (docker-machine env)"
|
||||
}
|
||||
default:
|
||||
if machineName != "" {
|
||||
cmd = fmt.Sprintf("eval $(docker-machine env %s)", machineName)
|
||||
} else {
|
||||
cmd = "eval $(docker-machine env)"
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("# Run this command to configure your shell: %s\n", cmd)
|
||||
}
|
||||
|
||||
func cmdSsh(c *cli.Context) {
|
||||
var (
|
||||
err error
|
||||
|
|
|
@ -475,6 +475,9 @@ func TestCmdEnvBash(t *testing.T) {
|
|||
// parse the output into a map of envvar:value for easier testing below
|
||||
envvars := make(map[string]string)
|
||||
for _, e := range strings.Split(strings.TrimSpace(out), "\n") {
|
||||
if !strings.HasPrefix(e, "export ") {
|
||||
continue
|
||||
}
|
||||
kv := strings.SplitN(e, "=", 2)
|
||||
key, value := kv[0], kv[1]
|
||||
envvars[strings.Replace(key, "export ", "", 1)] = value
|
||||
|
@ -570,6 +573,9 @@ func TestCmdEnvFish(t *testing.T) {
|
|||
// parse the output into a map of envvar:value for easier testing below
|
||||
envvars := make(map[string]string)
|
||||
for _, e := range strings.Split(strings.TrimSuffix(out, ";\n"), ";\n") {
|
||||
if !strings.HasPrefix(e, "set -x ") {
|
||||
continue
|
||||
}
|
||||
kv := strings.SplitN(strings.Replace(e, "set -x ", "", 1), " ", 2)
|
||||
key, value := kv[0], kv[1]
|
||||
envvars[key] = value
|
||||
|
|
Loading…
Reference in New Issue