Default to cmd shell on windows

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-17 16:25:50 +01:00
parent c07067c7e4
commit c279d0aba2
3 changed files with 44 additions and 15 deletions

View File

@ -248,16 +248,14 @@ func (g *EnvUsageHintGenerator) GenerateUsageHint(userShell string, args []strin
}
func detectShell() (string, error) {
// attempt to get the SHELL env var
shell := filepath.Base(os.Getenv("SHELL"))
shell := os.Getenv("SHELL")
log.Debugf("shell: %s", shell)
if shell == "" {
// check for windows env and not bash (i.e. msysgit, etc)
if runtime.GOOS == "windows" {
log.Info("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
fmt.Printf("You can further specify your shell with either 'cmd' or 'powershell' with the --shell flag.\n\n")
return "cmd", nil // this could be either powershell or cmd, defaulting to cmd
}
fmt.Printf("The default lines below are for a sh/bash shell, you can specify the shell you're using, with the --shell flag.\n\n")
return "", ErrUnknownShell
}
@ -265,5 +263,5 @@ func detectShell() (string, error) {
return "fish", nil
}
return shell, nil
return filepath.Base(shell), nil
}

View File

@ -6,6 +6,8 @@ import (
"strings"
"testing"
"fmt"
"github.com/docker/machine/commands/commandstest"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/drivers/fakedriver"
@ -549,20 +551,32 @@ func TestShellCfgUnset(t *testing.T) {
}
func TestDetectBash(t *testing.T) {
original_shell := os.Getenv("SHELL")
originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "/bin/bash")
defer os.Setenv("SHELL", original_shell)
shell, _ := detectShell()
defer os.Setenv("SHELL", originalShell)
shell, err := detectShell()
assert.Nil(t, err)
assert.Equal(t, "bash", shell)
}
func TestDetectFish(t *testing.T) {
original_shell := os.Getenv("SHELL")
originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "/bin/bash")
defer os.Setenv("SHELL", original_shell)
original_fishdir := os.Getenv("__fish_bin_dir")
defer os.Setenv("SHELL", originalShell)
originalFishdir := os.Getenv("__fish_bin_dir")
os.Setenv("__fish_bin_dir", "/usr/local/Cellar/fish/2.2.0/bin")
defer os.Setenv("__fish_bin_dir", original_fishdir)
shell, _ := detectShell()
defer os.Setenv("__fish_bin_dir", originalFishdir)
shell, err := detectShell()
assert.Nil(t, err)
assert.Equal(t, "fish", shell)
}
func TestUnknowShell(t *testing.T) {
originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "")
defer os.Setenv("SHELL", originalShell)
shell, err := detectShell()
fmt.Println(shell)
assert.Equal(t, err, ErrUnknownShell)
assert.Equal(t, "", shell)
}

View File

@ -0,0 +1,17 @@
package commands
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDetect(t *testing.T) {
originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "")
defer os.Setenv("SHELL", originalShell)
shell, err := detectShell()
assert.Nil(t, err)
assert.Equal(t, "cmd", shell)
}