-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move shell package from libmachine to crc
This is the only location where this package is used.
- Loading branch information
1 parent
1630ee2
commit b5773ad
Showing
8 changed files
with
177 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build !windows | ||
|
||
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/fish") | ||
|
||
shell, err := Detect() | ||
|
||
assert.Equal(t, "fish", shell) | ||
assert.NoError(t, err) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,90 @@ | ||
package shell | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
var ( | ||
supportedShell = []string{"cmd", "powershell"} | ||
) | ||
|
||
// re-implementation of private function in https://github.com/golang/go/blob/master/src/syscall/syscall_windows.go | ||
func getProcessEntry(pid int) (pe *syscall.ProcessEntry32, err error) { | ||
snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
_ = 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 | ||
} | ||
} | ||
} | ||
|
||
// getNameAndItsPpid returns the exe file name its parent process id. | ||
func getNameAndItsPpid(pid int) (exefile string, parentid int, err error) { | ||
pe, err := getProcessEntry(pid) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
|
||
name := syscall.UTF16ToString(pe.ExeFile[:]) | ||
return name, int(pe.ParentProcessID), nil | ||
} | ||
|
||
func Detect() (string, error) { | ||
shell := os.Getenv("SHELL") | ||
|
||
if shell == "" { | ||
shell, shellppid, err := getNameAndItsPpid(os.Getppid()) | ||
if err != nil { | ||
return "cmd", err // defaulting to cmd | ||
} | ||
switch { | ||
case strings.Contains(strings.ToLower(shell), "powershell"): | ||
return "powershell", nil | ||
case strings.Contains(strings.ToLower(shell), "cmd"): | ||
return "cmd", nil | ||
default: | ||
shell, _, err := getNameAndItsPpid(shellppid) | ||
if err != nil { | ||
return "cmd", err // defaulting to cmd | ||
} | ||
switch { | ||
case strings.Contains(strings.ToLower(shell), "powershell"): | ||
return "powershell", nil | ||
case strings.Contains(strings.ToLower(shell), "cmd"): | ||
return "cmd", nil | ||
default: | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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, "powershell", shell) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGetNameAndItsPpidOfCurrent(t *testing.T) { | ||
shell, shellppid, err := getNameAndItsPpid(os.Getpid()) | ||
|
||
assert.Equal(t, "shell.test.exe", shell) | ||
assert.Equal(t, os.Getppid(), shellppid) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGetNameAndItsPpidOfParent(t *testing.T) { | ||
shell, _, err := getNameAndItsPpid(os.Getppid()) | ||
|
||
assert.Equal(t, "go.exe", shell) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGetNameAndItsPpidOfGrandParent(t *testing.T) { | ||
_, shellppid, _ := getNameAndItsPpid(os.Getppid()) | ||
shell, _, err := getNameAndItsPpid(shellppid) | ||
|
||
assert.Equal(t, "powershell.exe", shell) | ||
assert.NoError(t, err) | ||
} |
88 changes: 0 additions & 88 deletions
88
vendor/github.com/code-ready/machine/libmachine/shell/shell_windows.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters