mirror of https://github.com/docker/docs.git
Simpler code for env usage hints.
We can just output the original os.Args in the eval call. Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
parent
f155bc9df9
commit
2d1e6d0163
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue