pax_global_header00006660000000000000000000000064141337665400014523gustar00rootroot0000000000000052 comment=f37a03e6dc1defcd3981e92149ebe594f73f9105 go-shell-0.3.1/000077500000000000000000000000001413376654000132365ustar00rootroot00000000000000go-shell-0.3.1/.github/000077500000000000000000000000001413376654000145765ustar00rootroot00000000000000go-shell-0.3.1/.github/workflows/000077500000000000000000000000001413376654000166335ustar00rootroot00000000000000go-shell-0.3.1/.github/workflows/main.yml000066400000000000000000000015761413376654000203130ustar00rootroot00000000000000name: Test on: pull_request: push: branches: - master tags: - v* jobs: test: strategy: matrix: os: - macos-latest - ubuntu-latest - windows-latest runs-on: ${{ matrix.os }} steps: - name: Set up Go uses: actions/setup-go@v1 with: go-version: 1.x - name: Cache Go modules uses: actions/cache@v1 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - name: Checkout uses: actions/checkout@v2 - name: Build run: go build ./... - name: Test run: go test ./... lint: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Lint uses: golangci/golangci-lint-action@v2 with: version: v1.42.1 go-shell-0.3.1/.gitignore000066400000000000000000000000051413376654000152210ustar00rootroot00000000000000/bin go-shell-0.3.1/.golangci.yml000066400000000000000000000021261413376654000156230ustar00rootroot00000000000000linters: enable: - asciicheck - bodyclose - deadcode - depguard - dogsled - dupl - durationcheck - errcheck - errname - errorlint - exhaustive - exhaustivestruct - exportloopref - forcetypeassert - gci - gochecknoinits - gocognit - goconst - gocritic - gocyclo - godot - goerr113 - gofmt - gofumpt - goheader - goimports - gomoddirectives - gomodguard - goprintffuncname - gosec - gosimple - govet - ifshort - importas - ineffassign - makezero - misspell - nakedret - nestif - nilerr - noctx - nolintlint - prealloc - predeclared - promlinter - revive - rowserrcheck - sqlclosecheck - staticcheck - structcheck - stylecheck - tagliatelle - thelper - tparallel - typecheck - unconvert - unparam - unused - varcheck - wastedassign - whitespace - wrapcheck disable: - cyclop - forbidigo - funlen - gochecknoglobals - godox - gomnd - lll - nlreturn - paralleltest - testpackage - wsl linters-settings: goimports: local-prefixes: github.com/twpayne/go-shell go-shell-0.3.1/LICENSE000066400000000000000000000020641413376654000142450ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2018 Tom Payne Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-shell-0.3.1/README.md000066400000000000000000000005221413376654000145140ustar00rootroot00000000000000# go-shell [![Actions Status](https://github.com/twpayne/go-shell/workflows/test/badge.svg)](https://github.com/twpayne/go-shell/actions) [![PkgGoDev](https://pkg.go.dev/badge/github.com/twpayne/go-shell)](https://pkg.go.dev/github.com/twpayne/go-shell) Package `shell` returns a user's shell across multiple platforms. ## License MITgo-shell-0.3.1/cmd/000077500000000000000000000000001413376654000140015ustar00rootroot00000000000000go-shell-0.3.1/cmd/shell/000077500000000000000000000000001413376654000151105ustar00rootroot00000000000000go-shell-0.3.1/cmd/shell/main.go000066400000000000000000000003031413376654000163570ustar00rootroot00000000000000package main import ( "fmt" "os" "github.com/twpayne/go-shell" ) func main() { currentUserShell, ok := shell.CurrentUserShell() fmt.Println(currentUserShell) if !ok { os.Exit(1) } } go-shell-0.3.1/example_test.go000066400000000000000000000002531413376654000162570ustar00rootroot00000000000000package shell_test import ( "fmt" "github.com/twpayne/go-shell" ) func Example_shell_CurrentUserShell() { shell, _ := shell.CurrentUserShell() fmt.Println(shell) } go-shell-0.3.1/go.mod000066400000000000000000000000541413376654000143430ustar00rootroot00000000000000module github.com/twpayne/go-shell go 1.12 go-shell-0.3.1/shell.go000066400000000000000000000006441413376654000147000ustar00rootroot00000000000000// Package shell returns the current user's shell across multiple platforms. package shell import ( "runtime" ) // DefaultShell returns the default shell depending on runtime.GOOS. func DefaultShell() string { switch runtime.GOOS { case "darwin": return "/bin/zsh" case "openbsd": return "/bin/ksh" case "plan9": return "/bin/rc" case "windows": return "powershell.exe" default: return "/bin/sh" } } go-shell-0.3.1/shell_darwin.go000066400000000000000000000017721413376654000162470ustar00rootroot00000000000000//go:build darwin // +build darwin package shell import ( "os" "os/exec" "os/user" "regexp" ) //nolint:gochecknoglobals var dsclUserShellRegexp = regexp.MustCompile(`\AUserShell:\s+(.*?)\s*\z`) // CurrentUserShell returns the current user's shell. func CurrentUserShell() (string, bool) { // If the SHELL environment variable is set, use it. if shell, ok := os.LookupEnv("SHELL"); ok { return shell, true } // Try to get the current user. If we can't then fallback to the default // shell. u, err := user.Current() if err != nil { return DefaultShell(), false } // If getpwnam_r is available, use it. if shell, ok := cgoGetUserShell(u.Username); ok { return shell, true } // If dscl is available, use it. //nolint:gosec if output, err := exec.Command("dscl", ".", "-read", u.HomeDir, "UserShell").Output(); err == nil { if m := dsclUserShellRegexp.FindSubmatch(output); m != nil { return string(m[1]), true } } // Fallback to the default shell. return DefaultShell(), false } go-shell-0.3.1/shell_plan9.go000066400000000000000000000002601413376654000157750ustar00rootroot00000000000000//go:build plan9 // +build plan9 package shell // CurrentUserShell returns the current user's shell. func CurrentUserShell() (string, bool) { return DefaultShell(), false } go-shell-0.3.1/shell_posix.go000066400000000000000000000025661413376654000161270ustar00rootroot00000000000000//go:build !darwin && !plan9 && !windows // +build !darwin,!plan9,!windows package shell import ( "bufio" "os" "os/exec" "os/user" "strings" ) // CurrentUserShell returns the current user's shell. func CurrentUserShell() (string, bool) { // If the SHELL environment variable is set, use it. if shell, ok := os.LookupEnv("SHELL"); ok { return shell, true } // Try to get the current user. If we can't then fallback to the default // shell. u, err := user.Current() if err != nil { return DefaultShell(), false } // If getpwnam_r is available, use it. if shell, ok := cgoGetUserShell(u.Username); ok { return shell, true } // If getent is available, use it. if getent, err := exec.LookPath("getent"); err == nil { if output, err := exec.Command(getent, "passwd", u.Username).Output(); err == nil { if fields := strings.SplitN(strings.TrimSuffix(string(output), "\n"), ":", 7); len(fields) == 7 { return fields[6], true } } } // If the user has an entry in /etc/passwd, use it. if f, err := os.Open("/etc/passwd"); err == nil { defer f.Close() s := bufio.NewScanner(f) for s.Scan() { if fields := strings.SplitN(strings.TrimSuffix(s.Text(), "\n"), ":", 7); len(fields) == 7 && fields[0] == u.Username { return fields[6], true } } _ = s.Err() // Ignore errors. } // Fallback to the default shell. return DefaultShell(), false } go-shell-0.3.1/shell_test.go000066400000000000000000000003751413376654000157400ustar00rootroot00000000000000package shell_test import ( "testing" "github.com/twpayne/go-shell" ) func TestShell(t *testing.T) { if shell, ok := shell.CurrentUserShell(); shell == "" || !ok { t.Errorf("shell.CurrentUserShell() == %v, %v, want !\"\", true", shell, ok) } } go-shell-0.3.1/shell_windows.go000066400000000000000000000007031413376654000164460ustar00rootroot00000000000000package shell import ( "os" ) // CurrentUserShell returns the current user's shell. func CurrentUserShell() (string, bool) { // If the SHELL environment variable is set, use it. if shell, ok := os.LookupEnv("SHELL"); ok { return shell, true } // If the ComSpec environment variable is set, use it. if comSpec, ok := os.LookupEnv("ComSpec"); ok { return comSpec, true } // Fallback to the default shell. return DefaultShell(), false } go-shell-0.3.1/shellcgo.go000066400000000000000000000022021413376654000153610ustar00rootroot00000000000000//go:build (cgo && aix) || (cgo && android) || (cgo && darwin) || (cgo && dragonfly) || (cgo && freebsd) || (cgo && illumos) || (cgo && linux) || (cgo && netbsd) || (cgo && openbsd) || (cgo && solaris) // +build cgo,aix cgo,android cgo,darwin cgo,dragonfly cgo,freebsd cgo,illumos cgo,linux cgo,netbsd cgo,openbsd cgo,solaris package shell // #cgo solaris CFLAGS: -D_POSIX_PTHREAD_SEMANTICS=1 // #cgo illumos CFLAGS: -D_POSIX_PTHREAD_SEMANTICS=1 // #include // #include // #include // #include // #include import "C" import ( "unsafe" ) func cgoGetUserShell(name string) (string, bool) { buflen := C.sysconf(C._SC_GETPW_R_SIZE_MAX) if buflen == -1 { buflen = 1024 } for { var ( cName = C.CString(name) pwd C.struct_passwd buf = make([]byte, buflen) result *C.struct_passwd ) //nolint:gocritic rc := C.getpwnam_r(cName, &pwd, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &result) C.free(unsafe.Pointer(cName)) switch rc { case 0: return C.GoString(result.pw_shell), true case C.ERANGE: buflen *= 2 default: return "", false } } } go-shell-0.3.1/shellnocgo.go000066400000000000000000000001661413376654000157250ustar00rootroot00000000000000//go:build !cgo // +build !cgo package shell func cgoGetUserShell(name string) (string, bool) { return "", false }