Skip to content

Commit

Permalink
refactor: linting checks for all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Nov 12, 2020
1 parent 3c812f9 commit 8fb190e
Show file tree
Hide file tree
Showing 49 changed files with 537 additions and 443 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Expand Up @@ -9,6 +9,7 @@ root = true
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf

; Go Code - match go fmt
[*.go]
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 0 additions & 1 deletion .github/workflows/code.yml
Expand Up @@ -27,7 +27,6 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Golang CI
if: matrix.os == 'ubuntu-latest'
uses: golangci/golangci-lint-action@v2
with:
version: v1.31
Expand Down
Empty file modified .gitignore 100755 → 100644
Empty file.
50 changes: 50 additions & 0 deletions .golangci.yml
@@ -0,0 +1,50 @@
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dupl
- errcheck
- exhaustive
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- goprintffuncname
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- lll
linters-settings:
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-tags:
- experimental
disabled-checks:
- ifElseChain
lll:
line-length: 180
98 changes: 49 additions & 49 deletions docs/static/img/logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions engine.go
Expand Up @@ -27,7 +27,7 @@ func (e *engine) getPowerlineColor(foreground bool) string {
return e.previousActiveSegment.Background
}

func (e *engine) writePowerLineSeparator(background string, foreground string, end bool) {
func (e *engine) writePowerLineSeparator(background, foreground string, end bool) {
symbol := e.activeSegment.PowerlineSymbol
if end {
symbol = e.previousActiveSegment.PowerlineSymbol
Expand Down Expand Up @@ -75,7 +75,7 @@ func (e *engine) renderSegmentText(text string) {
e.renderPlainSegment(text)
case Diamond:
e.renderDiamondSegment(text)
default:
case Powerline:
e.renderPowerLineSegment(text)
}
e.previousActiveSegment = e.activeSegment
Expand Down Expand Up @@ -135,7 +135,7 @@ func (e *engine) render() {
cursorMove := e.renderer.setCursorForRightWrite(blockText, block.HorizontalOffset)
e.renderer.print(cursorMove)
e.renderer.print(blockText)
default:
case Left:
e.renderer.print(e.renderBlockSegments(block))
}
}
Expand Down
19 changes: 12 additions & 7 deletions environment.go
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/shirou/gopsutil/process"
)

const (
unknown = "unknown"
windowsPlatform = "windows"
)

type environmentInfo interface {
getenv(key string) string
getcwd() string
Expand All @@ -31,12 +36,12 @@ type environmentInfo interface {
getPlatform() string
hasCommand(command string) bool
runCommand(command string, args ...string) (string, error)
runShellCommand(shell string, command string) string
runShellCommand(shell, command string) string
lastErrorCode() int
getArgs() *args
getBatteryInfo() (*battery.Battery, error)
getShellName() string
getWindowTitle(imageName string, windowTitleRegex string) (string, error)
getWindowTitle(imageName, windowTitleRegex string) (string, error)
}

type environment struct {
Expand Down Expand Up @@ -124,8 +129,8 @@ func (env *environment) getRuntimeGOOS() string {
}

func (env *environment) getPlatform() string {
if runtime.GOOS == "windows" {
return "windows"
if runtime.GOOS == windowsPlatform {
return windowsPlatform
}
p, _, _, _ := host.PlatformInformation()

Expand All @@ -146,7 +151,7 @@ func (env *environment) runCommand(command string, args ...string) (string, erro
return strings.TrimSpace(string(out)), nil
}

func (env *environment) runShellCommand(shell string, command string) string {
func (env *environment) runShellCommand(shell, command string) string {
out, err := exec.Command(shell, "-c", command).Output()
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -177,14 +182,14 @@ func (env *environment) getShellName() string {
p, _ := process.NewProcess(int32(pid))
name, err := p.Name()
if err != nil {
return "unknown"
return unknown
}
if name == "cmd.exe" {
p, _ = p.Parent()
name, err = p.Name()
}
if err != nil {
return "unknown"
return unknown
}
shell := strings.Replace(name, ".exe", "", 1)
return strings.Trim(shell, " ")
Expand Down
2 changes: 1 addition & 1 deletion environment_unix.go 100755 → 100644
Expand Up @@ -15,6 +15,6 @@ func (env *environment) homeDir() string {
return os.Getenv("HOME")
}

func (env *environment) getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func (env *environment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
return "", errors.New("not implemented")
}
6 changes: 4 additions & 2 deletions environment_windows.go 100755 → 100644
Expand Up @@ -25,7 +25,9 @@ func (env *environment) isRunningAsRoot() bool {
if err != nil {
return false
}
defer windows.FreeSid(sid)
defer func() {
_ = windows.FreeSid(sid)
}()

// This appears to cast a null pointer so I'm not sure why this
// works, but this guy says it does and it Works for Me™:
Expand All @@ -48,6 +50,6 @@ func (env *environment) homeDir() string {
return home
}

func (env *environment) getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func (env *environment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
return getWindowTitle(imageName, windowTitleRegex)
}
22 changes: 12 additions & 10 deletions environment_windows_win32.go
Expand Up @@ -35,7 +35,7 @@ func getImagePid(imageName string) ([]int, error) {
}

// getWindowTitle returns the title of a window linked to a process name
func getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func getWindowTitle(imageName, windowTitleRegex string) (string, error) {
processPid, err := getImagePid(imageName)
if err != nil {
return "", nil
Expand Down Expand Up @@ -73,7 +73,9 @@ func processes() ([]WindowsProcess, error) {
if err != nil {
return nil, syscall.GetLastError()
}
defer windows.CloseHandle(handle)
defer func() {
_ = windows.CloseHandle(handle)
}()

// get process infor by looping through the snapshot
var entry windows.ProcessEntry32
Expand Down Expand Up @@ -109,8 +111,8 @@ var (
)

// EnumWindows call EnumWindows from user32 and returns all active windows
func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
func EnumWindows(enumFunc, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, enumFunc, lparam, 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
Expand All @@ -122,10 +124,10 @@ func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
}

// GetWindowText returns the title and text of a window from a window handle
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (length int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
length = int32(r0)
if length == 0 {
if e1 != 0 {
err = error(e1)
} else {
Expand All @@ -135,7 +137,7 @@ func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32,
return
}

// GetWindowTitle searchs for a window attached to the pid
// GetWindowTitle searches for a window attached to the pid
func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, error) {
var hwnd syscall.Handle
var title string
Expand Down Expand Up @@ -166,8 +168,8 @@ func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, e
return 1 // continue enumeration
})
// Enumerates all top-level windows on the screen
EnumWindows(cb, 0)
if hwnd == 0 {
err = EnumWindows(cb, 0)
if err != nil || hwnd == 0 {
return 0, "", fmt.Errorf("No window with title '%b' found", pid)
}
return hwnd, title, nil
Expand Down
Empty file modified main.go 100755 → 100644
Empty file.

0 comments on commit 8fb190e

Please sign in to comment.