From 5ff7ab91fbbfc87b974f418d938079cd121d6b7b Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 4 Jan 2016 15:39:38 +0100 Subject: [PATCH] Extract shell detection to its own package Signed-off-by: David Gageot --- commands/commands.go | 1 - commands/env.go | 3 ++- commands/{detectshell.go => shell/shell.go} | 10 ++++++++-- commands/{detectshell_test.go => shell/shell_test.go} | 6 +++--- .../shell_unix_test.go} | 4 ++-- .../{detectshell_windows.go => shell/shell_windows.go} | 4 ++-- .../shell_windows_test.go} | 4 ++-- 7 files changed, 19 insertions(+), 13 deletions(-) rename commands/{detectshell.go => shell/shell.go} (70%) rename commands/{detectshell_test.go => shell/shell_test.go} (89%) rename commands/{detectshell_unix_test.go => shell/shell_unix_test.go} (87%) rename commands/{detectshell_windows.go => shell/shell_windows.go} (96%) rename commands/{detectshell_windows_test.go => shell/shell_windows_test.go} (89%) diff --git a/commands/commands.go b/commands/commands.go index e35d164ce9..b979546058 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -21,7 +21,6 @@ import ( ) var ( - ErrUnknownShell = errors.New("Error: Unknown shell") 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") ErrTooManyArguments = errors.New("Error: Too many arguments given") diff --git a/commands/env.go b/commands/env.go index e4fda1fe20..ddbb2379a5 100644 --- a/commands/env.go +++ b/commands/env.go @@ -9,6 +9,7 @@ import ( "text/template" "github.com/docker/machine/commands/mcndirs" + "github.com/docker/machine/commands/shell" "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/check" "github.com/docker/machine/libmachine/log" @@ -200,7 +201,7 @@ func getShell(userShell string) (string, error) { if userShell != "" { return userShell, nil } - return detectShell() + return shell.Detect() } func findNoProxyFromEnv() (string, string) { diff --git a/commands/detectshell.go b/commands/shell/shell.go similarity index 70% rename from commands/detectshell.go rename to commands/shell/shell.go index 12be8d4190..dd1579b5ef 100644 --- a/commands/detectshell.go +++ b/commands/shell/shell.go @@ -1,14 +1,20 @@ // +build !windows -package commands +package shell import ( + "errors" "fmt" "os" "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") if shell == "" { diff --git a/commands/detectshell_test.go b/commands/shell/shell_test.go similarity index 89% rename from commands/detectshell_test.go rename to commands/shell/shell_test.go index 8c44b1caef..51a33ee434 100644 --- a/commands/detectshell_test.go +++ b/commands/shell/shell_test.go @@ -1,4 +1,4 @@ -package commands +package shell import ( "os" @@ -11,7 +11,7 @@ func TestDetectBash(t *testing.T) { originalShell := os.Getenv("SHELL") os.Setenv("SHELL", "/bin/bash") defer os.Setenv("SHELL", originalShell) - shell, err := detectShell() + shell, err := Detect() assert.Nil(t, err) assert.Equal(t, "bash", shell) } @@ -23,7 +23,7 @@ func TestDetectFish(t *testing.T) { 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() + shell, err := Detect() assert.Nil(t, err) assert.Equal(t, "fish", shell) } diff --git a/commands/detectshell_unix_test.go b/commands/shell/shell_unix_test.go similarity index 87% rename from commands/detectshell_unix_test.go rename to commands/shell/shell_unix_test.go index ed495c8e74..472fe1d8cf 100644 --- a/commands/detectshell_unix_test.go +++ b/commands/shell/shell_unix_test.go @@ -1,6 +1,6 @@ // +build !windows -package commands +package shell import ( "fmt" @@ -14,7 +14,7 @@ func TestUnknowShell(t *testing.T) { originalShell := os.Getenv("SHELL") os.Setenv("SHELL", "") defer os.Setenv("SHELL", originalShell) - shell, err := detectShell() + shell, err := Detect() fmt.Println(shell) assert.Equal(t, err, ErrUnknownShell) assert.Equal(t, "", shell) diff --git a/commands/detectshell_windows.go b/commands/shell/shell_windows.go similarity index 96% rename from commands/detectshell_windows.go rename to commands/shell/shell_windows.go index faf7c90a45..e46c5681d9 100644 --- a/commands/detectshell_windows.go +++ b/commands/shell/shell_windows.go @@ -1,4 +1,4 @@ -package commands +package shell import ( "fmt" @@ -50,7 +50,7 @@ func startedBy() (exefile string, err error) { return name, nil } -func detectShell() (string, error) { +func Detect() (string, error) { shell := os.Getenv("SHELL") if shell == "" { diff --git a/commands/detectshell_windows_test.go b/commands/shell/shell_windows_test.go similarity index 89% rename from commands/detectshell_windows_test.go rename to commands/shell/shell_windows_test.go index a65d95c74f..7d3b76f395 100644 --- a/commands/detectshell_windows_test.go +++ b/commands/shell/shell_windows_test.go @@ -1,4 +1,4 @@ -package commands +package shell import ( "os" @@ -11,7 +11,7 @@ func TestDetect(t *testing.T) { originalShell := os.Getenv("SHELL") os.Setenv("SHELL", "") defer os.Setenv("SHELL", originalShell) - shell, err := detectShell() + shell, err := Detect() assert.Nil(t, err) assert.Equal(t, "cmd", shell) }