Update loginshell to version with Windows support (#184)

This commit is contained in:
Max Risuhin 2019-01-29 16:06:58 +02:00 committed by Liam Galvin
parent 8cdbe2517d
commit 23cf1e8b7d
3 changed files with 33 additions and 8 deletions

2
Gopkg.lock generated
View File

@ -137,7 +137,7 @@
branch = "master" branch = "master"
name = "github.com/riywo/loginshell" name = "github.com/riywo/loginshell"
packages = ["."] packages = ["."]
revision = "5da3acff02554cee5e8ccb4651cc5a4072f41790" revision = "c2f4167b23039c6289b5a488080176426ec5dad9"
[[projects]] [[projects]]
name = "github.com/stretchr/testify" name = "github.com/stretchr/testify"

View File

@ -17,15 +17,29 @@ func Shell() (string, error) {
return LinuxShell() return LinuxShell()
case "darwin": case "darwin":
return DarwinShell() return DarwinShell()
case "windows":
return WindowsShell()
} }
return "", errors.New("Undefined GOOS: " + runtime.GOOS) return "", errors.New("Undefined GOOS: " + runtime.GOOS)
} }
func WindowsShell() (string, error) {
consoleApp := os.Getenv("COMSPEC")
if consoleApp == "" {
consoleApp = "cmd.exe"
}
return consoleApp, nil
}
func LinuxShell() (string, error) { func LinuxShell() (string, error) {
user, err := user.Current() user, err := user.Current()
if err != nil { return "", err } if err != nil {
return "", err
}
out, err := exec.Command("getent", "passwd", user.Uid).Output() out, err := exec.Command("getent", "passwd", user.Uid).Output()
if err != nil { return "", err } if err != nil {
return "", err
}
ent := strings.Split(strings.TrimSuffix(string(out), "\n"), ":") ent := strings.Split(strings.TrimSuffix(string(out), "\n"), ":")
return ent[6], nil return ent[6], nil
@ -34,11 +48,15 @@ func LinuxShell() (string, error) {
func DarwinShell() (string, error) { func DarwinShell() (string, error) {
dir := "Local/Default/Users/" + os.Getenv("USER") dir := "Local/Default/Users/" + os.Getenv("USER")
out, err := exec.Command("dscl", "localhost", "-read", dir, "UserShell").Output() out, err := exec.Command("dscl", "localhost", "-read", dir, "UserShell").Output()
if err != nil { return "", err } if err != nil {
return "", err
}
re := regexp.MustCompile("UserShell: (/[^ ]+)\n") re := regexp.MustCompile("UserShell: (/[^ ]+)\n")
matched := re.FindStringSubmatch(string(out)) matched := re.FindStringSubmatch(string(out))
shell := matched[1] shell := matched[1]
if shell == "" { return "", errors.New(fmt.Sprintf("Invalid output: %s", string(out))) } if shell == "" {
return "", errors.New(fmt.Sprintf("Invalid output: %s", string(out)))
}
return shell, nil return shell, nil
} }

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"os" "os"
"fmt" "fmt"
"runtime"
) )
func TestShell(t *testing.T) { func TestShell(t *testing.T) {
@ -12,8 +13,14 @@ func TestShell(t *testing.T) {
t.Error(err) t.Error(err)
} }
currentShell := os.Getenv("SHELL") if runtime.GOOS == "windows" {
if shell != currentShell { if shell == "" {
t.Error(fmt.Sprintf("Output: %s, Current login shell: %s", shell, currentShell)) t.Error("Output is empty!")
}
} else {
currentShell := os.Getenv("SHELL")
if shell != currentShell {
t.Error(fmt.Sprintf("Output: %s, Current login shell: %s", shell, currentShell))
}
} }
} }