Skip to content

Commit

Permalink
apply golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Nov 29, 2021
1 parent 4b27fde commit 1a4e0b2
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 49 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.36
version: latest

test:
name: test
Expand All @@ -32,7 +31,7 @@ jobs:
uses: actions/checkout@v2

- name: Test
run: make test
run: make test-ci

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
Expand Down
43 changes: 43 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
run:
timeout: 1m

linters:
enable:
- asciicheck
- bodyclose
- deadcode
- depguard
- dogsled
- durationcheck
- errcheck
- errorlint
- exportloopref
- gci
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- govet
- importas
- ineffassign
- megacheck
- misspell
- nakedret
- nolintlint
- revive
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
linters-settings:
gosec:
# Exclude generated files
exclude-generated: true
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
20 changes: 7 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
# Run go fmt against code
fmt:
go fmt ./...
gofmt -s -w .

# Run go vet against code
vet:
go vet ./...

# Run golangci-lint
lint:
golangci-lint run
golangci-lint run --fix

# Run go mod tidy
tidy:
go mod tidy

# Run tests
test: mocks tidy fmt vet
test: test-ci fmt

# Run ci tests
test-ci: mocks tidy
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out

Expand All @@ -33,10 +27,10 @@ test-release:

semver:
ifeq (, $(shell which semver))
$(shell go get -u github.com/bakito/semver)
$(shell go install github.com/bakito/semver@latest)
endif

mockgen:
ifeq (, $(shell which mockgen))
$(shell go get github.com/golang/mock/mockgen@v1.5)
$(shell go install github.com/golang/mock/mockgen@v1.5)
endif
4 changes: 2 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"os"

"github.com/bakito/adguardhome-sync/pkg/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
)

var envVars = []string{
Expand All @@ -22,7 +23,6 @@ var envVars = []string{
}

var _ = Describe("Run", func() {

BeforeEach(func() {
for _, envVar := range envVars {
Ω(os.Unsetenv(envVar)).ShouldNot(HaveOccurred())
Expand Down
10 changes: 3 additions & 7 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"net/http/httptest"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/bakito/adguardhome-sync/pkg/client"
"github.com/bakito/adguardhome-sync/pkg/types"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var (
Expand All @@ -21,7 +20,6 @@ var (
)

var _ = Describe("Client", func() {

var (
cl client.Client
ts *httptest.Server
Expand Down Expand Up @@ -290,9 +288,7 @@ bar`)
})

Context("helper functions", func() {
var (
cl client.Client
)
var cl client.Client
BeforeEach(func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
Expand Down
8 changes: 4 additions & 4 deletions pkg/sync/http.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package sync

import (
"context"

// import embed for html page
_ "embed"

"context"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -50,7 +51,6 @@ func (w *worker) handleLogs(rw http.ResponseWriter, _ *http.Request) {

func (w *worker) basicAuth(h http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {

rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)

username, password, authOK := r.BasicAuth()
Expand Down Expand Up @@ -98,7 +98,7 @@ func (w *worker) listenAndServe() {
mux.HandleFunc("/", use(w.handleRoot, mw...))

go func() {
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
if err := httpServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
l.With("error", err).Fatalf("HTTP server ListenAndServe")
}
}()
Expand Down
6 changes: 1 addition & 5 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import (
"go.uber.org/zap"
)

var (
l = log.GetLogger("sync")
)
var l = log.GetLogger("sync")

// Sync config from origin to replica
func Sync(cfg *types.Config) error {

if cfg.Origin.URL == "" {
return fmt.Errorf("origin URL is required")
}
Expand Down Expand Up @@ -174,7 +171,6 @@ func (w *worker) sync() {
}

func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardInstance) {

rc, err := w.createClient(replica)
if err != nil {
l.With("error", err, "url", replica.URL).Error("Error creating replica client")
Expand Down
6 changes: 2 additions & 4 deletions pkg/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package sync
import (
"errors"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/bakito/adguardhome-sync/pkg/client"
clientmock "github.com/bakito/adguardhome-sync/pkg/mocks/client"
"github.com/bakito/adguardhome-sync/pkg/types"
gm "github.com/golang/mock/gomock"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Sync", func() {
Expand Down Expand Up @@ -426,7 +425,6 @@ var _ = Describe("Sync", func() {
})

Context("sync", func() {

It("should have no changes", func() {
w.cfg = &types.Config{
Origin: types.AdGuardInstance{},
Expand Down
9 changes: 4 additions & 5 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,10 @@ func (s Services) Equals(o Services) bool {
type Clients struct {
Clients []Client `json:"clients"`
AutoClients []struct {
IP string `json:"ip"`
Name string `json:"name"`
Source string `json:"source"`
WhoisInfo struct {
} `json:"whois_info"`
IP string `json:"ip"`
Name string `json:"name"`
Source string `json:"source"`
WhoisInfo struct{} `json:"whois_info"`
} `json:"auto_clients"`
SupportedTags []string `json:"supported_tags"`
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"encoding/json"
"io/ioutil"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/bakito/adguardhome-sync/pkg/types"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Types", func() {
Expand Down Expand Up @@ -219,9 +218,7 @@ var _ = Describe("Types", func() {
})
})
Context("Config", func() {
var (
cfg *types.Config
)
var cfg *types.Config
BeforeEach(func() {
cfg = &types.Config{}
})
Expand Down

0 comments on commit 1a4e0b2

Please sign in to comment.