Merge pull request #2610 from jeanlaurent/detect-shell

Enhance shell detection in `env` command
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-18 12:19:46 +01:00
commit fbf97b7c73
3 changed files with 59 additions and 7 deletions

View File

@ -248,18 +248,20 @@ 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
}
return shell, nil
if os.Getenv("__fish_bin_dir") != "" {
return "fish", 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"
@ -547,3 +549,34 @@ func TestShellCfgUnset(t *testing.T) {
os.Setenv(test.noProxyVar, "")
}
}
func TestDetectBash(t *testing.T) {
originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "/bin/bash")
defer os.Setenv("SHELL", originalShell)
shell, err := detectShell()
assert.Nil(t, err)
assert.Equal(t, "bash", shell)
}
func TestDetectFish(t *testing.T) {
originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "/bin/bash")
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", 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)
}