From 2d1e6d01631b219f7a35623dd26610b8155e5197 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 26 Oct 2015 09:55:41 +0100 Subject: [PATCH] Simpler code for env usage hints. We can just output the original os.Args in the eval call. Signed-off-by: David Gageot --- commands/env.go | 26 ++++++++------------------ commands/env_test.go | 44 ++++++++++++++++++-------------------------- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/commands/env.go b/commands/env.go index 51d01220d6..2bdc30f32e 100644 --- a/commands/env.go +++ b/commands/env.go @@ -34,10 +34,6 @@ type ShellConfig struct { NoProxyValue string } -type CmdEnvFlags interface { - Bool(name string) bool -} - func cmdEnv(c *cli.Context) { // Ensure that log messages always go to stderr when this command is // being run (it is intended to be run in a subshell) @@ -65,7 +61,7 @@ func cmdEnv(c *cli.Context) { t := template.New("envConfig") - usageHint := generateUsageHint(c.App.Name, c.Args().First(), userShell, c) + usageHint := generateUsageHint(userShell, os.Args) shellCfg := &ShellConfig{ DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), h.Name), @@ -170,28 +166,22 @@ func cmdEnv(c *cli.Context) { } } -func generateUsageHint(appName, machineName, userShell string, flags CmdEnvFlags) string { - args := machineName - if flags.Bool("swarm") { - args = "--swarm " + args - } - if flags.Bool("no-proxy") { - args = "--no-proxy " + args - } - +func generateUsageHint(userShell string, args []string) string { cmd := "" comment := "#" + commandLine := strings.Join(args, " ") + switch userShell { case "fish": - cmd = fmt.Sprintf("eval (%s env --shell=fish %s)", appName, args) + cmd = fmt.Sprintf("eval (%s)", commandLine) case "powershell": - cmd = fmt.Sprintf("%s env --shell=powershell %s | Invoke-Expression", appName, args) + cmd = fmt.Sprintf("%s | Invoke-Expression", commandLine) case "cmd": - cmd = fmt.Sprintf("\tFOR /f \"tokens=*\" %%i IN ('%s env --shell=cmd %s') DO %%i", appName, args) + cmd = fmt.Sprintf("\tFOR /f \"tokens=*\" %%i IN ('%s') DO %%i", commandLine) comment = "REM" default: - cmd = fmt.Sprintf("eval \"$(%s env %s)\"", appName, args) + cmd = fmt.Sprintf("eval \"$(%s)\"", commandLine) } return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd) diff --git a/commands/env_test.go b/commands/env_test.go index 10f4bf356c..c18a1f07d2 100644 --- a/commands/env_test.go +++ b/commands/env_test.go @@ -8,43 +8,35 @@ import ( "github.com/stretchr/testify/assert" ) -type MockCmdEnvFlags struct { - flags string -} - -func (f *MockCmdEnvFlags) Bool(name string) bool { - return strings.Contains(f.flags, name) -} - func TestHints(t *testing.T) { var tests = []struct { userShell string - flags *MockCmdEnvFlags + commandLine string expectedHints string }{ - {"", &MockCmdEnvFlags{}, "# Run this command to configure your shell: \n# eval \"$(machine env default)\"\n"}, - {"", &MockCmdEnvFlags{"--no-proxy"}, "# Run this command to configure your shell: \n# eval \"$(machine env --no-proxy default)\"\n"}, - {"", &MockCmdEnvFlags{"--swarm"}, "# Run this command to configure your shell: \n# eval \"$(machine env --swarm default)\"\n"}, - {"", &MockCmdEnvFlags{"--no-proxy --swarm"}, "# Run this command to configure your shell: \n# eval \"$(machine env --no-proxy --swarm default)\"\n"}, + {"", "machine env default", "# Run this command to configure your shell: \n# eval \"$(machine env default)\"\n"}, + {"", "machine env --no-proxy default", "# Run this command to configure your shell: \n# eval \"$(machine env --no-proxy default)\"\n"}, + {"", "machine env --swarm default", "# Run this command to configure your shell: \n# eval \"$(machine env --swarm default)\"\n"}, + {"", "machine env --no-proxy --swarm default", "# Run this command to configure your shell: \n# eval \"$(machine env --no-proxy --swarm default)\"\n"}, - {"fish", &MockCmdEnvFlags{}, "# Run this command to configure your shell: \n# eval (machine env --shell=fish default)\n"}, - {"fish", &MockCmdEnvFlags{"--no-proxy"}, "# Run this command to configure your shell: \n# eval (machine env --shell=fish --no-proxy default)\n"}, - {"fish", &MockCmdEnvFlags{"--swarm"}, "# Run this command to configure your shell: \n# eval (machine env --shell=fish --swarm default)\n"}, - {"fish", &MockCmdEnvFlags{"--no-proxy --swarm"}, "# Run this command to configure your shell: \n# eval (machine env --shell=fish --no-proxy --swarm default)\n"}, + {"fish", "./machine env --shell=fish default", "# Run this command to configure your shell: \n# eval (./machine env --shell=fish default)\n"}, + {"fish", "./machine env --shell=fish --no-proxy default", "# Run this command to configure your shell: \n# eval (./machine env --shell=fish --no-proxy default)\n"}, + {"fish", "./machine env --shell=fish --swarm default", "# Run this command to configure your shell: \n# eval (./machine env --shell=fish --swarm default)\n"}, + {"fish", "./machine env --shell=fish --no-proxy --swarm default", "# Run this command to configure your shell: \n# eval (./machine env --shell=fish --no-proxy --swarm default)\n"}, - {"powershell", &MockCmdEnvFlags{}, "# Run this command to configure your shell: \n# machine env --shell=powershell default | Invoke-Expression\n"}, - {"powershell", &MockCmdEnvFlags{"--no-proxy"}, "# Run this command to configure your shell: \n# machine env --shell=powershell --no-proxy default | Invoke-Expression\n"}, - {"powershell", &MockCmdEnvFlags{"--swarm"}, "# Run this command to configure your shell: \n# machine env --shell=powershell --swarm default | Invoke-Expression\n"}, - {"powershell", &MockCmdEnvFlags{"--no-proxy --swarm"}, "# Run this command to configure your shell: \n# machine env --shell=powershell --no-proxy --swarm default | Invoke-Expression\n"}, + {"powershell", "./machine env --shell=powershell default", "# Run this command to configure your shell: \n# ./machine env --shell=powershell default | Invoke-Expression\n"}, + {"powershell", "./machine env --shell=powershell --no-proxy default", "# Run this command to configure your shell: \n# ./machine env --shell=powershell --no-proxy default | Invoke-Expression\n"}, + {"powershell", "./machine env --shell=powershell --swarm default", "# Run this command to configure your shell: \n# ./machine env --shell=powershell --swarm default | Invoke-Expression\n"}, + {"powershell", "./machine env --shell=powershell --no-proxy --swarm default", "# Run this command to configure your shell: \n# ./machine env --shell=powershell --no-proxy --swarm default | Invoke-Expression\n"}, - {"cmd", &MockCmdEnvFlags{}, "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('machine env --shell=cmd default') DO %i\n"}, - {"cmd", &MockCmdEnvFlags{"--no-proxy"}, "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('machine env --shell=cmd --no-proxy default') DO %i\n"}, - {"cmd", &MockCmdEnvFlags{"--swarm"}, "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('machine env --shell=cmd --swarm default') DO %i\n"}, - {"cmd", &MockCmdEnvFlags{"--no-proxy --swarm"}, "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('machine env --shell=cmd --no-proxy --swarm default') DO %i\n"}, + {"cmd", "./machine env --shell=cmd default", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd default') DO %i\n"}, + {"cmd", "./machine env --shell=cmd --no-proxy default", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd --no-proxy default') DO %i\n"}, + {"cmd", "./machine env --shell=cmd --swarm default", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd --swarm default') DO %i\n"}, + {"cmd", "./machine env --shell=cmd --no-proxy --swarm default", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd --no-proxy --swarm default') DO %i\n"}, } for _, test := range tests { - hints := generateUsageHint("machine", "default", test.userShell, test.flags) + hints := generateUsageHint(test.userShell, strings.Split(test.commandLine, " ")) assert.Equal(t, test.expectedHints, hints) }