FIX #2746 Improve shell detection in bugsnag reports
Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
"github.com/bugsnag/bugsnag-go"
|
||||
"github.com/docker/machine/libmachine/log"
|
||||
"github.com/docker/machine/libmachine/shell"
|
||||
"github.com/docker/machine/version"
|
||||
)
|
||||
|
||||
@@ -125,12 +126,8 @@ func addFile(path string, metaData *bugsnag.MetaData) {
|
||||
}
|
||||
|
||||
func detectRunningShell(metaData *bugsnag.MetaData) {
|
||||
shell := os.Getenv("SHELL")
|
||||
if shell != "" {
|
||||
metaData.Add("device", "shell", shell)
|
||||
}
|
||||
shell = os.Getenv("__fish_bin_dir")
|
||||
if shell != "" {
|
||||
shell, err := shell.Detect()
|
||||
if err == nil {
|
||||
metaData.Add("device", "shell", shell)
|
||||
}
|
||||
}
|
||||
|
||||
30
libmachine/shell/shell.go
Normal file
30
libmachine/shell/shell.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// +build !windows
|
||||
|
||||
package shell
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnknownShell = errors.New("Error: Unknown shell")
|
||||
)
|
||||
|
||||
// Detect detects user's current shell.
|
||||
func Detect() (string, error) {
|
||||
shell := os.Getenv("SHELL")
|
||||
|
||||
if shell == "" {
|
||||
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
|
||||
}
|
||||
|
||||
if os.Getenv("__fish_bin_dir") != "" {
|
||||
return "fish", nil
|
||||
}
|
||||
|
||||
return filepath.Base(shell), nil
|
||||
}
|
||||
31
libmachine/shell/shell_test.go
Normal file
31
libmachine/shell/shell_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDetectBash(t *testing.T) {
|
||||
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
|
||||
os.Setenv("SHELL", "/bin/bash")
|
||||
|
||||
shell, err := Detect()
|
||||
|
||||
assert.Equal(t, "bash", shell)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDetectFish(t *testing.T) {
|
||||
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
|
||||
os.Setenv("SHELL", "/bin/bash")
|
||||
|
||||
defer func(fishDir string) { os.Setenv("__fish_bin_dir", fishDir) }(os.Getenv("__fish_bin_dir"))
|
||||
os.Setenv("__fish_bin_dir", "/usr/local/Cellar/fish/2.2.0/bin")
|
||||
|
||||
shell, err := Detect()
|
||||
|
||||
assert.Equal(t, "fish", shell)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
20
libmachine/shell/shell_unix_test.go
Normal file
20
libmachine/shell/shell_unix_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// +build !windows
|
||||
|
||||
package shell
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUnknownShell(t *testing.T) {
|
||||
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
|
||||
os.Setenv("SHELL", "")
|
||||
|
||||
shell, err := Detect()
|
||||
|
||||
assert.Equal(t, err, ErrUnknownShell)
|
||||
assert.Empty(t, shell)
|
||||
}
|
||||
76
libmachine/shell/shell_windows.go
Normal file
76
libmachine/shell/shell_windows.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// re-implementation of private function in https://github.com/golang/go/blob/master/src/syscall/syscall_windows.go#L945
|
||||
func getProcessEntry(pid int) (pe *syscall.ProcessEntry32, err error) {
|
||||
snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer syscall.CloseHandle(syscall.Handle(snapshot))
|
||||
|
||||
var processEntry syscall.ProcessEntry32
|
||||
processEntry.Size = uint32(unsafe.Sizeof(processEntry))
|
||||
err = syscall.Process32First(snapshot, &processEntry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for {
|
||||
if processEntry.ProcessID == uint32(pid) {
|
||||
pe = &processEntry
|
||||
return
|
||||
}
|
||||
|
||||
err = syscall.Process32Next(snapshot, &processEntry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// startedBy returns the exe file name of the parent process.
|
||||
func startedBy() (exefile string, err error) {
|
||||
ppid := os.Getppid()
|
||||
|
||||
pe, err := getProcessEntry(ppid)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
name := syscall.UTF16ToString(pe.ExeFile[:])
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func Detect() (string, error) {
|
||||
shell := os.Getenv("SHELL")
|
||||
|
||||
if shell == "" {
|
||||
shell, err := startedBy()
|
||||
if err != nil {
|
||||
return "cmd", err // defaulting to cmd
|
||||
}
|
||||
if strings.Contains(strings.ToLower(shell), "powershell") {
|
||||
return "powershell", nil
|
||||
} else if strings.Contains(strings.ToLower(shell), "cmd") {
|
||||
return "cmd", nil
|
||||
} else {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if os.Getenv("__fish_bin_dir") != "" {
|
||||
return "fish", nil
|
||||
}
|
||||
|
||||
return filepath.Base(shell), nil
|
||||
}
|
||||
25
libmachine/shell/shell_windows_test.go
Normal file
25
libmachine/shell/shell_windows_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDetect(t *testing.T) {
|
||||
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
|
||||
os.Setenv("SHELL", "")
|
||||
|
||||
shell, err := Detect()
|
||||
|
||||
assert.Equal(t, "cmd", shell)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestStartedBy(t *testing.T) {
|
||||
shell, err := startedBy()
|
||||
|
||||
assert.Equal(t, "go.exe", shell)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user