diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99b9a45..d7a9c94 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,21 @@ jobs: run: | ./bin/macos-battery-exporter -v + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: "1.21" + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.55 + env: + VERBOSE: "true" + release-please: runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' @@ -35,7 +50,7 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 - - uses: actions/setup-go@v4 + - uses: actions/setup-go@v5 with: go-version: "1.21" - name: Run GoReleaser diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..d3ca897 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,87 @@ +linters-settings: + funlen: + lines: 300 + statements: 450 + golint: + min-confidence: 0 + govet: + enable-all: true + disable: + - fieldalignment + - shadow + lll: + line-length: 80 + tab-width: 4 + maligned: + suggest-new: true + misspell: + locale: US + +linters: + disable-all: true + enable: + - asciicheck + - bodyclose + - durationcheck + - errcheck + - errorlint + - exhaustive + - exportloopref + - funlen + - gochecknoinits + - goconst + - gocritic + - godot + - goimports + - gomodguard + - goprintffuncname + - gosec + - gosimple + - govet + - importas + - ineffassign + - lll + - misspell + - nakedret + - nilerr + - noctx + - nolintlint + - prealloc + - predeclared + - revive + - staticcheck + - typecheck + - unconvert + - unparam + - unused + - whitespace + +issues: + exclude: + - Using the variable on range scope `tt` in function literal + - Using the variable on range scope `tc` in function literal + exclude-rules: + - path: "_test\\.go" + linters: + - funlen + - dupl + - source: "^//go:generate " + linters: + - lll + - source: "`env:" + linters: + - lll + - source: "`json:" + linters: + - lll + - source: "`xml:" + linters: + - lll + - source: "`yaml:" + linters: + - lll + +run: + timeout: 2m + allow-parallel-runners: true + modules-download-mode: readonly diff --git a/battery/battery.go b/battery/battery.go index b741689..3dbe94f 100644 --- a/battery/battery.go +++ b/battery/battery.go @@ -15,12 +15,12 @@ type Battery struct { // BuiltIn indicates if the battery is built-in or not. BuiltIn bool - // ChargeRateAmps is the current charge rate in mAh. Negative values indicate - // discharge, positive values indicate charging. + // ChargeRateAmps is the current charge rate in mAh. Negative values + // indicate discharge, positive values indicate charging. ChargeRateAmps int64 - // ChargeRateWatts is the current charge rate in mWh. Negative values indicate - // discharge, positive values indicate charging. + // ChargeRateWatts is the current charge rate in mWh. Negative values + // indicate discharge, positive values indicate charging. ChargeRateWatts float64 // CurrentCapacityAmps is the current battery capacity in mAh. @@ -76,6 +76,7 @@ type Battery struct { func newBattery(b *batteryRaw) *Battery { volts := float64(b.Voltage) / 1000 + //nolint:lll return &Battery{ BatteryCellDisconnectCount: b.BatteryCellDisconnectCount, BuiltIn: b.BuiltIn, @@ -101,29 +102,31 @@ func newBattery(b *batteryRaw) *Battery { } func Get() (*Battery, error) { - batteriesRaw, err := getAllRaw() + raw, err := getAllRaw() if err != nil { return nil, err } - return newBattery(batteriesRaw[0]), nil + return newBattery(raw[0]), nil } func GetAll() ([]*Battery, error) { - batteriesRaw, err := getAllRaw() + raw, err := getAllRaw() if err != nil { return nil, err } batteries := []*Battery{} - for _, b := range batteriesRaw { + for _, b := range raw { batteries = append(batteries, newBattery(b)) } return batteries, nil } -// roundTo rounds a float64 to 'places' decimal places +// roundTo rounds a float64 to 'places' decimal places. +// +//nolint:unparam func roundTo(value float64, places int) float64 { shift := math.Pow(10, float64(places)) return math.Round(value*shift) / shift diff --git a/main.go b/main.go index 111b905..e7f0168 100644 --- a/main.go +++ b/main.go @@ -88,10 +88,10 @@ func mainE() error { if *outputFlag != "" { return writeToFile(sb.String(), *outputFlag) - } else { - fmt.Print(sb.String()) } + fmt.Print(sb.String()) + return nil } diff --git a/prom/collector.go b/prom/collector.go index 233e679..737631c 100644 --- a/prom/collector.go +++ b/prom/collector.go @@ -367,7 +367,9 @@ func boolToFloat64(b bool) float64 { return 0 } -// roundTo rounds a float64 to 'places' decimal places +// roundTo rounds a float64 to 'places' decimal places. +// +//nolint:unparam func roundTo(value float64, places int) float64 { shift := math.Pow(10, float64(places)) return math.Round(value*shift) / shift