Extract shell detection to its own package

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2016-01-04 15:39:38 +01:00
parent 6511c9a45a
commit 5ff7ab91fb
7 changed files with 19 additions and 13 deletions

View File

@ -21,7 +21,6 @@ import (
) )
var ( var (
ErrUnknownShell = errors.New("Error: Unknown shell")
ErrNoMachineSpecified = errors.New("Error: Expected to get one or more machine names as arguments") ErrNoMachineSpecified = errors.New("Error: Expected to get one or more machine names as arguments")
ErrExpectedOneMachine = errors.New("Error: Expected one machine name as an argument") ErrExpectedOneMachine = errors.New("Error: Expected one machine name as an argument")
ErrTooManyArguments = errors.New("Error: Too many arguments given") ErrTooManyArguments = errors.New("Error: Too many arguments given")

View File

@ -9,6 +9,7 @@ import (
"text/template" "text/template"
"github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/commands/shell"
"github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/check" "github.com/docker/machine/libmachine/check"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
@ -200,7 +201,7 @@ func getShell(userShell string) (string, error) {
if userShell != "" { if userShell != "" {
return userShell, nil return userShell, nil
} }
return detectShell() return shell.Detect()
} }
func findNoProxyFromEnv() (string, string) { func findNoProxyFromEnv() (string, string) {

View File

@ -1,14 +1,20 @@
// +build !windows // +build !windows
package commands package shell
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
) )
func detectShell() (string, error) { var (
ErrUnknownShell = errors.New("Error: Unknown shell")
)
// Detect detects user's current shell.
func Detect() (string, error) {
shell := os.Getenv("SHELL") shell := os.Getenv("SHELL")
if shell == "" { if shell == "" {

View File

@ -1,4 +1,4 @@
package commands package shell
import ( import (
"os" "os"
@ -11,7 +11,7 @@ func TestDetectBash(t *testing.T) {
originalShell := os.Getenv("SHELL") originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "/bin/bash") os.Setenv("SHELL", "/bin/bash")
defer os.Setenv("SHELL", originalShell) defer os.Setenv("SHELL", originalShell)
shell, err := detectShell() shell, err := Detect()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "bash", shell) assert.Equal(t, "bash", shell)
} }
@ -23,7 +23,7 @@ func TestDetectFish(t *testing.T) {
originalFishdir := os.Getenv("__fish_bin_dir") originalFishdir := os.Getenv("__fish_bin_dir")
os.Setenv("__fish_bin_dir", "/usr/local/Cellar/fish/2.2.0/bin") os.Setenv("__fish_bin_dir", "/usr/local/Cellar/fish/2.2.0/bin")
defer os.Setenv("__fish_bin_dir", originalFishdir) defer os.Setenv("__fish_bin_dir", originalFishdir)
shell, err := detectShell() shell, err := Detect()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "fish", shell) assert.Equal(t, "fish", shell)
} }

View File

@ -1,6 +1,6 @@
// +build !windows // +build !windows
package commands package shell
import ( import (
"fmt" "fmt"
@ -14,7 +14,7 @@ func TestUnknowShell(t *testing.T) {
originalShell := os.Getenv("SHELL") originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "") os.Setenv("SHELL", "")
defer os.Setenv("SHELL", originalShell) defer os.Setenv("SHELL", originalShell)
shell, err := detectShell() shell, err := Detect()
fmt.Println(shell) fmt.Println(shell)
assert.Equal(t, err, ErrUnknownShell) assert.Equal(t, err, ErrUnknownShell)
assert.Equal(t, "", shell) assert.Equal(t, "", shell)

View File

@ -1,4 +1,4 @@
package commands package shell
import ( import (
"fmt" "fmt"
@ -50,7 +50,7 @@ func startedBy() (exefile string, err error) {
return name, nil return name, nil
} }
func detectShell() (string, error) { func Detect() (string, error) {
shell := os.Getenv("SHELL") shell := os.Getenv("SHELL")
if shell == "" { if shell == "" {

View File

@ -1,4 +1,4 @@
package commands package shell
import ( import (
"os" "os"
@ -11,7 +11,7 @@ func TestDetect(t *testing.T) {
originalShell := os.Getenv("SHELL") originalShell := os.Getenv("SHELL")
os.Setenv("SHELL", "") os.Setenv("SHELL", "")
defer os.Setenv("SHELL", originalShell) defer os.Setenv("SHELL", originalShell)
shell, err := detectShell() shell, err := Detect()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "cmd", shell) assert.Equal(t, "cmd", shell)
} }