Merge pull request #2058 from dgageot/improve-cmd-exe-support

improve cmd shell support
This commit is contained in:
Nathan LeClaire 2015-10-22 11:59:55 -07:00
commit 07bcb9a7aa
2 changed files with 52 additions and 36 deletions

View File

@ -32,6 +32,10 @@ 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)
@ -59,7 +63,7 @@ func cmdEnv(c *cli.Context) {
t := template.New("envConfig")
usageHint := generateUsageHint(c.App.Name, c.Args().First(), userShell, c.Bool("no-proxy"), c.Bool("swarm"))
usageHint := generateUsageHint(c.App.Name, c.Args().First(), userShell, c)
shellCfg := &ShellConfig{
DockerCertPath: authOptions.CertDir,
@ -145,7 +149,7 @@ func cmdEnv(c *cli.Context) {
shellCfg.Suffix = "\"\n"
shellCfg.Delimiter = " = \""
case "cmd":
shellCfg.Prefix = "set "
shellCfg.Prefix = "SET "
shellCfg.Suffix = "\n"
shellCfg.Delimiter = "="
default:
@ -164,26 +168,29 @@ func cmdEnv(c *cli.Context) {
}
}
func generateUsageHint(appName, machineName, userShell string, noProxy bool, swarm bool) string {
flags := ""
if noProxy {
flags += "--no-proxy "
func generateUsageHint(appName, machineName, userShell string, flags CmdEnvFlags) string {
args := machineName
if flags.Bool("swarm") {
args = "--swarm " + args
}
if swarm {
flags += "--swarm "
if flags.Bool("no-proxy") {
args = "--no-proxy " + args
}
cmd := ""
comment := "#"
switch userShell {
case "fish":
cmd = fmt.Sprintf("eval (%s env --shell=fish %s%s)", appName, flags, machineName)
cmd = fmt.Sprintf("eval (%s env --shell=fish %s)", appName, args)
case "powershell":
cmd = fmt.Sprintf("%s env --shell=powershell %s%s | Invoke-Expression", appName, flags, machineName)
cmd = fmt.Sprintf("%s env --shell=powershell %s | Invoke-Expression", appName, args)
case "cmd":
cmd = "copy and paste the above values into your command prompt"
cmd = fmt.Sprintf("\tFOR /f \"tokens=*\" %%i IN ('%s env --shell=cmd %s') DO %%i", appName, args)
comment = "REM"
default:
cmd = fmt.Sprintf("eval \"$(%s env %s%s)\"", appName, flags, machineName)
cmd = fmt.Sprintf("eval \"$(%s env %s)\"", appName, args)
}
return fmt.Sprintf("# Run this command to configure your shell: \n# %s\n", cmd)
return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd)
}

View File

@ -3,40 +3,49 @@ package commands
import (
"testing"
"strings"
"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
noProxy bool
swarm bool
hints string
userShell string
flags *MockCmdEnvFlags
expectedHints 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"},
{"", &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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
}
for _, expected := range tests {
hints := generateUsageHint("machine", "default", expected.userShell, expected.noProxy, expected.swarm)
for _, test := range tests {
hints := generateUsageHint("machine", "default", test.userShell, test.flags)
assert.Equal(t, expected.hints, hints)
assert.Equal(t, test.expectedHints, hints)
}
}