diff --git a/commands/env.go b/commands/env.go index 72eeab55dd..59bbad5b1c 100644 --- a/commands/env.go +++ b/commands/env.go @@ -59,7 +59,7 @@ func cmdEnv(c *cli.Context) { t := template.New("envConfig") - usageHint := generateUsageHint(c.App.Name, c.Args().First(), userShell) + usageHint := generateUsageHint(c.App.Name, c.Args().First(), userShell, c.Bool("no-proxy"), c.Bool("swarm")) shellCfg := &ShellConfig{ DockerCertPath: authOptions.CertDir, @@ -164,29 +164,25 @@ func cmdEnv(c *cli.Context) { } } -func generateUsageHint(appName, machineName, userShell string) string { +func generateUsageHint(appName, machineName, userShell string, noProxy bool, swarm bool) string { + flags := "" + if noProxy { + flags += "--no-proxy " + } + if swarm { + flags += "--swarm " + } + cmd := "" switch userShell { case "fish": - if machineName != "" { - cmd = fmt.Sprintf("eval (%s env %s)", appName, machineName) - } else { - cmd = fmt.Sprintf("eval (%s env)", appName) - } + cmd = fmt.Sprintf("eval (%s env --shell=fish %s%s)", appName, flags, machineName) case "powershell": - if machineName != "" { - cmd = fmt.Sprintf("%s env --shell=powershell %s | Invoke-Expression", appName, machineName) - } else { - cmd = fmt.Sprintf("%s env --shell=powershell | Invoke-Expression", appName) - } + cmd = fmt.Sprintf("%s env --shell=powershell %s%s | Invoke-Expression", appName, flags, machineName) case "cmd": cmd = "copy and paste the above values into your command prompt" default: - if machineName != "" { - cmd = fmt.Sprintf("eval \"$(%s env %s)\"", appName, machineName) - } else { - cmd = fmt.Sprintf("eval \"$(%s env)\"", appName) - } + cmd = fmt.Sprintf("eval \"$(%s env %s%s)\"", appName, flags, machineName) } return fmt.Sprintf("# Run this command to configure your shell: \n# %s\n", cmd) diff --git a/commands/env_test.go b/commands/env_test.go new file mode 100644 index 0000000000..957b0eb6ac --- /dev/null +++ b/commands/env_test.go @@ -0,0 +1,42 @@ +package commands + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHints(t *testing.T) { + var tests = []struct { + userShell string + noProxy bool + swarm bool + hints string + }{ + {"", false, false, "# Run this command to configure your shell: \n# eval \"$(machine env default)\"\n"}, + {"", true, false, "# Run this command to configure your shell: \n# eval \"$(machine env --no-proxy default)\"\n"}, + {"", false, true, "# Run this command to configure your shell: \n# eval \"$(machine env --swarm default)\"\n"}, + {"", true, true, "# Run this command to configure your shell: \n# eval \"$(machine env --no-proxy --swarm default)\"\n"}, + + {"fish", false, false, "# Run this command to configure your shell: \n# eval (machine env --shell=fish default)\n"}, + {"fish", true, false, "# Run this command to configure your shell: \n# eval (machine env --shell=fish --no-proxy default)\n"}, + {"fish", false, true, "# Run this command to configure your shell: \n# eval (machine env --shell=fish --swarm default)\n"}, + {"fish", true, true, "# Run this command to configure your shell: \n# eval (machine env --shell=fish --no-proxy --swarm default)\n"}, + + {"powershell", false, false, "# Run this command to configure your shell: \n# machine env --shell=powershell default | Invoke-Expression\n"}, + {"powershell", true, false, "# Run this command to configure your shell: \n# machine env --shell=powershell --no-proxy default | Invoke-Expression\n"}, + {"powershell", false, true, "# Run this command to configure your shell: \n# machine env --shell=powershell --swarm default | Invoke-Expression\n"}, + {"powershell", true, true, "# Run this command to configure your shell: \n# machine env --shell=powershell --no-proxy --swarm default | Invoke-Expression\n"}, + + {"cmd", false, false, "# Run this command to configure your shell: \n# copy and paste the above values into your command prompt\n"}, + {"cmd", true, false, "# Run this command to configure your shell: \n# copy and paste the above values into your command prompt\n"}, + {"cmd", false, true, "# Run this command to configure your shell: \n# copy and paste the above values into your command prompt\n"}, + {"cmd", true, true, "# Run this command to configure your shell: \n# copy and paste the above values into your command prompt\n"}, + } + + for _, expected := range tests { + hints := generateUsageHint("machine", "default", expected.userShell, expected.noProxy, expected.swarm) + + assert.Equal(t, expected.hints, hints) + } +}