Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add golangci-lint, fix lint warnings #227

Merged
merged 3 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/actions/tests/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ runs:
with:
go-version: ">=1.19"

- name: Check Go Fmt
shell: bash
run: |
go version
go fmt ./...
git diff --exit-code

- name: Check Go Mod
shell: bash
run: |
go version
go mod tidy
git diff --exit-code

- uses: golangci/golangci-lint-action@v2
with:
version: v1.51.2
args: --timeout 5m --config .github/lint/golangci.yaml

- name: Run Unit tests
shell: bash
run: |
Expand Down
18 changes: 18 additions & 0 deletions .github/lint/golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
run:
timeout: 3m
linters:
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- errcheck
19 changes: 16 additions & 3 deletions makefile → Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
-include .env

.PHONY: build run check fmt tidy lint test

build:
docker build . -t exportarr:local

run:
docker rm --force exportarr || echo ""
docker run --name exportarr \
Expand All @@ -11,7 +14,17 @@ run:
-e LOG_LEVEL="debug" \
-p 9707:9707 \
-d exportarr:local ${APP_NAME}
test:
go test -v -race -covermode atomic -coverprofile=covprofile ./...

check: fmt tidy lint test

fmt:
go fmt ./...

tidy:
go mod tidy
go mod tidy

lint:
golangci-lint run -c .github/lint/golangci.yaml

test:
go test -v -race -covermode atomic -coverprofile=covprofile ./...
9 changes: 7 additions & 2 deletions cmd/exportarr/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/onedr0p/exportarr/internal/commands"
import (
"github.com/onedr0p/exportarr/internal/commands"
)

var (
appName = "exportarr"
Expand All @@ -10,10 +12,13 @@ var (
)

func main() {
commands.Execute(commands.AppInfo{
err := commands.Execute(commands.AppInfo{
Name: appName,
Version: version,
BuildTime: buildTime,
Revision: revision,
})
if err != nil {
panic(err)
}
}
8 changes: 7 additions & 1 deletion internal/arr/client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func (a *FormAuth) Auth(req *http.Request) error {
}

u := a.AuthBaseURL.JoinPath("login")
u.Query().Add("ReturnUrl", "/general/settings")
vals := u.Query()
vals.Add("ReturnUrl", "/general/settings")
u.RawQuery = vals.Encode()

authReq, err := http.NewRequest("POST", u.String(), strings.NewReader(form.Encode()))
if err != nil {
Expand All @@ -114,12 +116,16 @@ func (a *FormAuth) Auth(req *http.Request) error {
return fmt.Errorf("Failed to renew FormAuth Cookie: Received Status Code %d", authResp.StatusCode)
}

found := false
for _, cookie := range authResp.Cookies() {
if strings.HasSuffix(cookie.Name, "arrAuth") {
copy := *cookie
a.cookie = &copy
found = true
break
}
}
if !found {
return fmt.Errorf("Failed to renew FormAuth Cookie: No Cookie with suffix 'arrAuth' found")
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/arr/client/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestRoundTrip_Retries(t *testing.T) {
{
name: "Err",
testFunc: func(req *http.Request) (*http.Response, error) {
return nil, &http.ProtocolError{}
return nil, http.ErrNotSupported
},
},
}
Expand Down
20 changes: 9 additions & 11 deletions internal/arr/collector/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ func (collector *radarrCollector) Collect(ch chan<- prometheus.Metric) {
}
var fileSize int64
var (
editions = 0
editions = 0
downloaded = 0
monitored = 0
unmonitored = 0
missing = 0
wanted = 0
qualities = map[string]int{}
tags = []struct {
Label string
tags = []struct {
Label string
Movies int
}{}
)
Expand Down Expand Up @@ -166,22 +166,20 @@ func (collector *radarrCollector) Collect(ch chan<- prometheus.Metric) {
tagObjects := model.TagMovies{}
// https://radarr.video/docs/api/#/TagDetails/get_api_v3_tag_detail
if err := c.DoRequest("tag/detail", &tagObjects); err != nil {
log.Errorw("Error getting Tags", "error", err)
ch <- prometheus.NewInvalidMetric(collector.errorMetric, err)
return
}
log.Errorw("Error getting Tags", "error", err)
ch <- prometheus.NewInvalidMetric(collector.errorMetric, err)
return
}
for _, s := range tagObjects {
tag := struct {
Label string
Label string
Movies int
}{
Label: s.Label,
Label: s.Label,
Movies: len(s.MovieIds),
}
tags = append(tags, tag)
}



ch <- prometheus.MustNewConstMetric(collector.movieEdition, prometheus.GaugeValue, float64(editions))
ch <- prometheus.MustNewConstMetric(collector.movieMetric, prometheus.GaugeValue, float64(len(movies)))
Expand Down
1 change: 0 additions & 1 deletion internal/arr/collector/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

type systemStatusCollector struct {
config *config.ArrConfig // App configuration
configFile *model.Config // *arr configuration from config.xml
systemStatus *prometheus.Desc // Total number of system statuses
errorMetric *prometheus.Desc // Error Description for use with InvalidMetric
}
Expand Down
12 changes: 6 additions & 6 deletions internal/arr/model/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ type Movie []struct {
HasFile bool `json:"hasFile"`
Available bool `json:"isAvailable"`
Monitored bool `json:"monitored"`
MovieFile struct {
Edition string `json:"edition"`
Size int64 `json:"size"`
MovieFile struct {
Edition string `json:"edition"`
Size int64 `json:"size"`
Quality struct {
Quality struct {
Name string `json:"name"`
Expand All @@ -19,7 +19,7 @@ type Movie []struct {
}

type TagMovies []struct {
ID int `json:"id"`
Label string `json:"label"`
MovieIds []int `json:"movieIds"`
ID int `json:"id"`
Label string `json:"label"`
MovieIds []int `json:"movieIds"`
}
1 change: 0 additions & 1 deletion internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func TestDoRequest_PanicRecovery(t *testing.T) {
require.NoError(err)
w.Write(s)
w.WriteHeader(http.StatusOK)
return
}))
defer ts.Close()

Expand Down
8 changes: 6 additions & 2 deletions internal/commands/arr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func init() {
func UsageOnError(cmd *cobra.Command, err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
cmd.Usage()
if err := cmd.Usage(); err != nil {
panic(err)
}
os.Exit(1)
}
}
Expand Down Expand Up @@ -178,7 +180,9 @@ var prowlarrCmd = &cobra.Command{
return err
}
c.ApiVersion = "v1"
c.LoadProwlarrConfig(cmd.PersistentFlags())
if err := c.LoadProwlarrConfig(cmd.PersistentFlags()); err != nil {
return err
}
if err := c.Prowlarr.Validate(); err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ func initConfig() {
conf, err = config.LoadConfig(rootCmd.PersistentFlags())
if err != nil {
fmt.Fprintln(os.Stderr, err)
rootCmd.Usage()
if err := rootCmd.Usage(); err != nil {
panic(err)
}
os.Exit(1)
}

if err := conf.Validate(); err != nil {
fmt.Fprintln(os.Stderr, err)
rootCmd.Usage()
if err := rootCmd.Usage(); err != nil {
panic(err)
}
os.Exit(1)
}
}
Expand Down Expand Up @@ -102,7 +106,7 @@ func initLogger() {

func finalizeLogger() {
// Flushes buffered log messages
zap.S().Sync()
zap.S().Sync() //nolint:errcheck
}

type registerFunc func(registry prometheus.Registerer)
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func LoadConfig(flags *flag.FlagSet) (*Config, error) {
return nil, fmt.Errorf("Couldn't Read API Key file %w", err)
}

k.Set("api-key", string(data))
if err := k.Set("api-key", string(data)); err != nil {
return nil, fmt.Errorf("Couldn't merge api-key into config: %w", err)
}
}

var out Config
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestLoadConfig_Flags(t *testing.T) {
require.True(config.DisableSSLVerify)

flags.Set("form-auth", "false")
config, err = LoadConfig(flags)
_, err = LoadConfig(flags)
require.NoError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (

func HealthzHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
w.Write([]byte("OK")) //nolint:errcheck
fmt.Fprint(w)
}
2 changes: 1 addition & 1 deletion internal/handlers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (

func IndexHandler(w http.ResponseWriter, _ *http.Request) {
response := `<h1>Exportarr</h1><p><a href='/metrics'>metrics</a></p>`
fmt.Fprintf(w, response)
fmt.Fprintln(w, response)
}
5 changes: 0 additions & 5 deletions internal/sabnzbd/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package model
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -178,10 +177,6 @@ func latestStat(m map[string]int) (string, int) {
return key, m[key]
}

func monadCast(i interface{}, t reflect.Type) {

}

// parseFloat is a monad version of strconv.ParseFloat
func parseFloat(s interface{}, prevErr error) (float64, error) {
if prevErr != nil {
Expand Down
Loading