Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-kuendig committed Mar 29, 2024
1 parent febc87a commit 2f75f9e
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.54
version: v1.55

test:
runs-on: ubuntu-20.04
Expand All @@ -29,7 +29,7 @@ jobs:

goreleaser:
runs-on: ubuntu-latest
# needs: [test, lint]
needs: [test, lint]
if: startsWith(github.ref, 'refs/tags/v')
steps:
-
Expand Down
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ type PushNotificationRequest struct {
Title string `json:"title"`
PushToken string `json:"push_token"`
RegistrationInfo struct {
AppId string `json:"app_id"`
AppID string `json:"app_id"`
AppVersion string `json:"app_version"`
OsVersion string `json:"os_version"`
OSVersion string `json:"os_version"`
} `json:"registration_info"`
Data PushNotificationData `json:"data"`
}
Expand Down
3 changes: 2 additions & 1 deletion sensor/audiovolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package sensor
import (
"testing"

"github.com/stretchr/testify/require"
"hacompanion/entity"

"github.com/stretchr/testify/require"
)

func TestAudioVolume(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion sensor/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package sensor
import (
"testing"

"github.com/stretchr/testify/require"
"hacompanion/entity"

"github.com/stretchr/testify/require"
)

func TestCPUUsage(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion sensor/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package sensor
import (
"testing"

"github.com/stretchr/testify/require"
"hacompanion/entity"

"github.com/stretchr/testify/require"
)

func TestMemory(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions sensor/online.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sensor

import (
"context"
"errors"
"fmt"
"net/http"
"os/exec"
Expand Down Expand Up @@ -46,6 +47,7 @@ func (o OnlineCheck) Run(ctx context.Context) (*entity.Payload, error) {
}
}

//nolint:nilerr
func (o OnlineCheck) checkHTTP(ctx context.Context) (*entity.Payload, error) {
p := entity.NewPayload()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, o.target, nil)
Expand All @@ -61,6 +63,8 @@ func (o OnlineCheck) checkHTTP(ctx context.Context) (*entity.Payload, error) {
return p, nil
}

defer resp.Body.Close()

p.State = true
p.Attributes["status"] = resp.Status

Expand All @@ -69,14 +73,14 @@ func (o OnlineCheck) checkHTTP(ctx context.Context) (*entity.Payload, error) {

func (o OnlineCheck) checkPing(ctx context.Context) (*entity.Payload, error) {
p := entity.NewPayload()
//nolint:gosec
cmd := exec.CommandContext(ctx, "ping", "-c 2", "-w 4", o.target)
err := cmd.Run()
if err != nil {
p.State = false
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
p.Attributes["err"] = fmt.Sprintf("could not reach %s", o.target)
} else {
p.Attributes["err"] = fmt.Sprintf("failed to execute ping: %s", err)
}
return p, nil
}
Expand Down
6 changes: 3 additions & 3 deletions sensor/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func (pwr Power) Run(ctx context.Context) (*entity.Payload, error) {
dir := fmt.Sprintf("/sys/class/power_supply/%s", pwr.Battery)
_, err := os.Stat(dir)
if os.IsNotExist(err) {
return nil, fmt.Errorf("failed to read battery status from %s: %s", dir, err)
return nil, fmt.Errorf("failed to read battery status from %s: %w", dir, err)
}
realPath, err := filepath.EvalSymlinks(dir)
if err != nil {
return nil, fmt.Errorf("failed to eval symlink %s: %s", dir, err)
return nil, fmt.Errorf("failed to eval symlink %s: %w", dir, err)
}
p := entity.NewPayload()
err = filepath.WalkDir(realPath, func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -64,7 +64,7 @@ func (pwr Power) Run(ctx context.Context) (*entity.Payload, error) {
// Check if a power cable is attached.
acLink := "/sys/class/power_supply/AC"
if exists, _ := util.FileExists(acLink); exists {
if realPath, err := filepath.EvalSymlinks(acLink); err == nil {
if realPath, fileErr := filepath.EvalSymlinks(acLink); fileErr == nil {
acInfo := filepath.Join(realPath, "online")
if exists, _ := util.FileExists(acLink); exists {
p.Attributes["ac_connected"] = util.StringToOnOff(pwr.optimisticRead(acInfo))
Expand Down
1 change: 1 addition & 0 deletions sensor/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (s Script) Run(ctx context.Context) (*entity.Payload, error) {
var out bytes.Buffer

// Call the custom script.
//nolint:gosec
cmd := exec.CommandContext(ctx, s.cfg.Path)
cmd.Stdout = &out
if err = cmd.Run(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sensor/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (u Uptime) process(output string) (*entity.Payload, error) {
}
seconds, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return nil, fmt.Errorf("failed to parse seconds from /proc/uptime (%s): %s", output, err)
return nil, fmt.Errorf("failed to parse seconds from /proc/uptime (%s): %w", output, err)
}

p.State = time.Now().Add(-time.Second * time.Duration(seconds)).Format(time.RFC3339)
Expand Down
4 changes: 2 additions & 2 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
// RespondError returns a JSON error response.
//
//nolint:errcheck
func RespondError(w http.ResponseWriter, error string, status int) {
func RespondError(w http.ResponseWriter, msg string, status int) {
var resp struct {
Error string `json:"errorMessage"`
}
resp.Error = error
resp.Error = msg
b, err := json.Marshal(resp)
if err != nil {
w.Write([]byte(fmt.Sprintf(`{"error": "%s"}`, string(b))))
Expand Down
1 change: 1 addition & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func RandomString(n int) string {

s := make([]rune, n)
for i := range s {
//nolint:gosec
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
Expand Down

0 comments on commit 2f75f9e

Please sign in to comment.