Skip to content

Commit

Permalink
Update project infrastructure (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne authored Sep 27, 2023
1 parent f37a03e commit b7bedec
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 48 deletions.
30 changes: 11 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,27 @@ jobs:
test:
strategy:
matrix:
go-version:
- stable
- oldstable
os:
- macos-latest
- ubuntu-latest
- windows-latest
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go
uses: actions/setup-go@v1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe
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
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744
- name: build
run: go build ./...
- name: Test
- 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
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744
- uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc
with:
version: v1.42.1
version: v1.54.2
43 changes: 37 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- deadcode
- depguard
- containedctx
- contextcheck
- decorder
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exhaustivestruct
- exhaustruct
- exportloopref
- forcetypeassert
- gci
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoinits
- gocognit
- goconst
Expand All @@ -31,50 +39,73 @@ linters:
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- ifshort
- grouper
- importas
- ineffassign
- interfacebloat
- ireturn
- loggercheck
- makezero
- mirror
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- tagalign
- tagliatelle
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- varcheck
- usestdlibvars
- wastedassign
- whitespace
- wrapcheck
- zerologlint
disable:
- cyclop
- depguard
- forbidigo
- funlen
- gochecknoglobals
- godox
- gomnd
- lll
- maintidx
- nlreturn
- paralleltest
- testableexamples
- testpackage
- varnamelen
- wsl

linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/twpayne/go-shell)
gofumpt:
extra-rules: true
module-path: github.com/twpayne/go-shell
goimports:
local-prefixes: github.com/twpayne/go-shell
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# 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.
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/twpayne/go-shell

go 1.12
go 1.19

require github.com/alecthomas/assert/v2 v2.3.0

require (
github.com/alecthomas/repr v0.2.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
7 changes: 2 additions & 5 deletions shell_darwin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build darwin
// +build darwin

package shell

Expand All @@ -10,8 +9,7 @@ import (
"regexp"
)

//nolint:gochecknoglobals
var dsclUserShellRegexp = regexp.MustCompile(`\AUserShell:\s+(.*?)\s*\z`)
var dsclUserShellRegexp = regexp.MustCompile(`\AUserShell:\s+(.*?)\s*\z`) //nolint:gochecknoglobals

// CurrentUserShell returns the current user's shell.
func CurrentUserShell() (string, bool) {
Expand All @@ -33,8 +31,7 @@ func CurrentUserShell() (string, bool) {
}

// If dscl is available, use it.
//nolint:gosec
if output, err := exec.Command("dscl", ".", "-read", u.HomeDir, "UserShell").Output(); err == nil {
if output, err := exec.Command("dscl", ".", "-read", u.HomeDir, "UserShell").Output(); err == nil { //nolint:gosec
if m := dsclUserShellRegexp.FindSubmatch(output); m != nil {
return string(m[1]), true
}
Expand Down
1 change: 0 additions & 1 deletion shell_plan9.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build plan9
// +build plan9

package shell

Expand Down
10 changes: 6 additions & 4 deletions shell_posix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !darwin && !plan9 && !windows
// +build !darwin,!plan9,!windows
//go:build !darwin && !plan9 && unix

package shell

Expand Down Expand Up @@ -33,7 +32,9 @@ func CurrentUserShell() (string, bool) {
// 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 {
if fields := strings.SplitN(strings.TrimSuffix(string(output), "\n"), ":", 7); len(
fields,
) == 7 {
return fields[6], true
}
}
Expand All @@ -45,7 +46,8 @@ func CurrentUserShell() (string, bool) {

s := bufio.NewScanner(f)
for s.Scan() {
if fields := strings.SplitN(strings.TrimSuffix(s.Text(), "\n"), ":", 7); len(fields) == 7 && fields[0] == u.Username {
fields := strings.SplitN(strings.TrimSuffix(s.Text(), "\n"), ":", 7)
if len(fields) == 7 && fields[0] == u.Username {
return fields[6], true
}
}
Expand Down
10 changes: 6 additions & 4 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package shell_test
import (
"testing"

"github.com/alecthomas/assert/v2"

"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)
}
func TestCurrentUserShell(t *testing.T) {
shell, ok := shell.CurrentUserShell()
assert.True(t, ok)
assert.NotEqual(t, "", shell)
}
14 changes: 8 additions & 6 deletions shellcgo.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//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
//go:build (cgo && aix) || (cgo && android) || (cgo && darwin) || (cgo && dragonfly) || (cgo && freebsd) || (cgo && linux) || (cgo && netbsd) || (cgo && openbsd)

package shell

// #cgo solaris CFLAGS: -D_POSIX_PTHREAD_SEMANTICS=1
// #cgo illumos CFLAGS: -D_POSIX_PTHREAD_SEMANTICS=1
// #include <errno.h>
// #include <pwd.h>
// #include <stdlib.h>
Expand All @@ -29,8 +26,13 @@ func cgoGetUserShell(name string) (string, bool) {
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)
rc := C.getpwnam_r(
cName,
&pwd,
(*C.char)(unsafe.Pointer(&buf[0])),
C.size_t(buflen),
&result, //nolint:gocritic
)
C.free(unsafe.Pointer(cName))

switch rc {
Expand Down
1 change: 0 additions & 1 deletion shellnocgo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !cgo
// +build !cgo

package shell

Expand Down

0 comments on commit b7bedec

Please sign in to comment.