Skip to content

Commit

Permalink
chore(lint): add multiple linters
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 13, 2024
1 parent 130ab00 commit 85780dc
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 25 deletions.
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ issues:
- containedctx
- dupl
- goerr113
- text: Function `exitHealthchecksio` should pass the context parameter
linters:
- contextcheck

linters:
enable:
Expand All @@ -18,6 +21,7 @@ linters:
- bidichk
- bodyclose
- containedctx
- contextcheck
- decorder
- dogsled
- dupl
Expand All @@ -34,6 +38,7 @@ linters:
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- gocognit
- goconst
- gocritic
Expand All @@ -46,8 +51,10 @@ linters:
- gomoddirectives
- goprintffuncname
- gosec
- gosmopolitan
- grouper
- importas
- inamedparam
- interfacebloat
- ireturn
- lll
Expand All @@ -63,13 +70,17 @@ linters:
- nolintlint
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
- rowserrcheck
- sloglint
- sqlclosecheck
- tagalign
- tenv
- thelper
- tparallel
Expand All @@ -78,3 +89,4 @@ linters:
- usestdlibvars
- wastedassign
- whitespace
- zerologlint
7 changes: 5 additions & 2 deletions cmd/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func logProvidersCount(providersCount int, logger log.LeveledLogger) {
case 1:
logger.Info("Found single setting to update record")
default:
logger.Info("Found " + fmt.Sprint(providersCount) + " settings to update records")
logger.Info("Found " + strconv.Itoa(providersCount) + " settings to update records")
}
}

Expand All @@ -340,7 +340,10 @@ func readRecords(providers []provider.Provider, persistentDB *persistence.Databa

func exitHealthchecksio(hioClient *healthchecksio.Client,
logger log.LoggerInterface, state healthchecksio.State) {
err := hioClient.Ping(context.Background(), state)
const timeout = 3 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := hioClient.Ping(ctx, state)
if err != nil {
logger.Error(err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions internal/health/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/qdm12/ddns-updater/internal/constants"
)

func MakeIsHealthy(db AllSelecter, resolver LookupIPer) func() error {
return func() (err error) {
return isHealthy(db, resolver)
func MakeIsHealthy(db AllSelecter, resolver LookupIPer) func(ctx context.Context) error {
return func(ctx context.Context) (err error) {
return isHealthy(ctx, db, resolver)
}
}

Expand All @@ -23,7 +23,7 @@ var (
)

// isHealthy checks all the records were updated successfully and returns an error if not.
func isHealthy(db AllSelecter, resolver LookupIPer) (err error) {
func isHealthy(ctx context.Context, db AllSelecter, resolver LookupIPer) (err error) {
records := db.SelectAll()
for _, record := range records {
if record.Status == constants.FAIL {
Expand All @@ -39,7 +39,7 @@ func isHealthy(db AllSelecter, resolver LookupIPer) (err error) {
return fmt.Errorf("%w: for hostname %s", ErrRecordIPNotSet, hostname)
}

lookedUpNetIPs, err := resolver.LookupIP(context.Background(), "ip", hostname)
lookedUpNetIPs, err := resolver.LookupIP(ctx, "ip", hostname)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions internal/health/handler.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package health

import (
"context"
"net/http"
)

func newHandler(healthcheck func() error) http.Handler {
func newHandler(healthcheck func(context.Context) error) http.Handler {
return &handler{
healthcheck: healthcheck,
}
}

type handler struct {
healthcheck func() error
healthcheck func(context.Context) error
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || (r.RequestURI != "" && r.RequestURI != "/") {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
err := h.healthcheck()
err := h.healthcheck(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
4 changes: 3 additions & 1 deletion internal/health/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package health

import (
"context"

"github.com/qdm12/goservices/httpserver"
)

func NewServer(address string, logger Logger, healthcheck func() error) (
func NewServer(address string, logger Logger, healthcheck func(context.Context) error) (
server *httpserver.Server, err error) {
name := "health"
return httpserver.New(httpserver.Settings{
Expand Down
6 changes: 3 additions & 3 deletions internal/params/retro_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package params

import (
"fmt"
"errors"
"net/netip"
"testing"

Expand All @@ -19,12 +19,12 @@ func Test_makeIPv6Suffix(t *testing.T) {
errMessage string
}{
"empty": {
errWrapped: fmt.Errorf(`IPv6 prefix format is incorrect: ` +
errWrapped: errors.New(`IPv6 prefix format is incorrect: ` +
`cannot parse "" as uint8`),
},
"malformed": {
prefixBitsString: "malformed",
errWrapped: fmt.Errorf(`IPv6 prefix format is incorrect: ` +
errWrapped: errors.New(`IPv6 prefix format is incorrect: ` +
`cannot parse "malformed" as uint8`),
},
"with_leading_slash": {
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/providers/aliyun/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package aliyun
import (
"crypto/rand"
"encoding/binary"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/qdm12/ddns-updater/internal/provider/headers"
Expand All @@ -23,7 +23,7 @@ func newURLValues(accessKeyID string) (values url.Values) {
values.Set("SignatureMethod", "HMAC-SHA1")
values.Set("Timestamp", time.Now().UTC().Format("2006-01-02T15:04:05Z"))
values.Set("SignatureVersion", "1.0")
values.Set("SignatureNonce", fmt.Sprint(randInt64))
values.Set("SignatureNonce", strconv.FormatInt(randInt64, 10))
return values
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/providers/hetzner/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p *Provider) updateRecord(ctx context.Context, client *http.Client,
u := url.URL{
Scheme: "https",
Host: "dns.hetzner.com",
Path: fmt.Sprintf("/api/v1/records/%s", recordID),
Path: "/api/v1/records/" + recordID,
}

requestData := struct {
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/providers/porkbun/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/qdm12/ddns-updater/internal/provider/errors"
)
Expand Down Expand Up @@ -93,7 +94,7 @@ func (p *Provider) createRecord(ctx context.Context, client *http.Client,
Content: ipStr,
Type: recordType,
Name: p.host,
TTL: fmt.Sprint(p.ttl),
TTL: strconv.FormatUint(uint64(p.ttl), 10),
}
buffer := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buffer)
Expand Down Expand Up @@ -141,7 +142,7 @@ func (p *Provider) updateRecord(ctx context.Context, client *http.Client,
APIKey: p.apiKey,
Content: ipStr,
Type: recordType,
TTL: fmt.Sprint(p.ttl),
TTL: strconv.FormatUint(uint64(p.ttl), 10),
Name: p.host,
}
buffer := bytes.NewBuffer(nil)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func (h *handlers) update(w http.ResponseWriter, _ *http.Request) {
start := h.timeNow()
errors := h.runner.ForceUpdate(h.ctx)
errors := h.runner.ForceUpdate(h.ctx) //nolint:contextcheck
duration := h.timeNow().Sub(start)
if len(errors) > 0 {
httpErrors(w, http.StatusInternalServerError, errors)
Expand Down
5 changes: 3 additions & 2 deletions internal/update/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/netip"
"strconv"
"time"

"github.com/qdm12/ddns-updater/internal/constants"
Expand Down Expand Up @@ -203,7 +204,7 @@ func (s *Service) shouldUpdateRecordWithLookup(ctx context.Context, hostname str
return false
}
s.logger.Warn("cannot DNS resolve " + hostname + " after " +
fmt.Sprint(tries) + " tries: " + err.Error()) // update anyway
strconv.Itoa(tries) + " tries: " + err.Error()) // update anyway
}

ipKind := ipVersionToIPKind(ipVersion)
Expand Down Expand Up @@ -346,7 +347,7 @@ func (s *Service) Start(ctx context.Context) (runError <-chan error, startErr er
s.runCancel = runCancel
done := make(chan struct{})
s.done = done
go s.run(runCtx, ready, done)
go s.run(runCtx, ready, done) //nolint:contextcheck
select {
case <-ready:
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (u *Updater) Update(ctx context.Context, id uint, ip netip.Addr) (err error
return err
}
record.Status = constants.SUCCESS
record.Message = fmt.Sprintf("changed to %s", ip.String())
record.Message = "changed to " + ip.String()
record.History = append(record.History, models.HistoryEvent{
IP: newIP,
Time: u.timeNow(),
Expand Down
3 changes: 2 additions & 1 deletion pkg/publicip/info/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type provider interface {
get(ctx context.Context, ip netip.Addr) (result Result, err error)
}

func newProvider(providerName Provider, client *http.Client) provider { //nolint:ireturn
//nolint:ireturn
func newProvider(providerName Provider, client *http.Client) provider {
switch providerName {
case Ipinfo:
return newIpinfo(client)
Expand Down
3 changes: 2 additions & 1 deletion pkg/publicip/subfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"sync/atomic"
)

func (f *Fetcher) getSubFetcher() ipFetcher { //nolint:ireturn
//nolint:ireturn
func (f *Fetcher) getSubFetcher() ipFetcher {
fetcher := f.fetchers[0]
if len(f.fetchers) > 1 { // cycling effect
index := int(atomic.AddUint32(f.counter, 1)) % len(f.fetchers)
Expand Down

0 comments on commit 85780dc

Please sign in to comment.