Skip to content

Commit

Permalink
Merge pull request #14 from threefoldtech/development_fix_linter
Browse files Browse the repository at this point in the history
fix linter
  • Loading branch information
rawdaGastan authored Oct 20, 2024
2 parents 1975919 + f12561f commit 78ce6f4
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 97 deletions.
17 changes: 17 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
linters:
enable:
- errcheck
- gofmt
- govet
- ineffassign
- unconvert
- staticcheck
- gocyclo
enable-all: false
linters-settings:
gocyclo:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 100
run:
timeout: 20m
44 changes: 7 additions & 37 deletions pkg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,21 @@ GOPATH := $(shell go env GOPATH)

all: build

getdeps:
@echo "Installing golint" && go install github.com/golangci/golangci-lint/cmd/[email protected]
@echo "Installing gocyclo" && go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
@echo "Installing deadcode" && go install github.com/remyoudompheng/go-misc/deadcode@latest
@echo "Installing misspell" && go install github.com/client9/misspell/cmd/misspell@latest
@echo "Installing ineffassign" && go install github.com/gordonklaus/ineffassign@latest
@echo "Installing staticcheck" && go install honnef.co/go/tools/cmd/staticcheck@latest

verifiers: vet fmt lint cyclo spelling static #deadcode

vet:
@echo "Running $@"
@go vet -atomic -bool -copylocks -nilfunc -printf -rangeloops -unreachable -unsafeptr -unusedresult ./...
all: getdeps test

fmt:
@echo "Running $@"
@gofmt -d .
getdeps:
@echo "Installing golangci-lint" && go get github.com/golangci/golangci-lint/cmd/golangci-lint && go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy

lint:
@echo "Running $@"
@${GOPATH}/bin/golangci-lint run

ineffassign:
@echo "Running $@"
@${GOPATH}/bin/ineffassign .

cyclo:
@echo "Running $@"
@${GOPATH}/bin/gocyclo -over 100 .

deadcode:
@echo "Running $@"
@${GOPATH}/bin/deadcode -test $(shell go list ./...) || true

spelling:
@${GOPATH}/bin/misspell -i "monitord,forumla,etherent" -error `find .`

static:
@${GOPATH}/bin/staticcheck -- ./...
@${GOPATH}/bin/golangci-lint run -c ../.golangci.yml

check: test
test: verifiers build
test: lint build
go test -vet=off -v $(shell go list ./... | grep -Ev "stubs|network" )

testrace: verifiers build
testrace: lint build
go test -vet=off -v $(shell go list ./... | grep -Ev "stubs|network" )

generate:
Expand Down
4 changes: 2 additions & 2 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (c *Module) start(ns, id string) error {

ctx := namespaces.WithNamespace(context.Background(), ns)

container, err := client.LoadContainer(ctx, string(id))
container, err := client.LoadContainer(ctx, id)
if err != nil {
return err
}
Expand Down Expand Up @@ -615,7 +615,7 @@ func (c *Module) Delete(ns string, id pkg.ContainerID) error {
_ = task.Kill(ctx, syscall.SIGTERM)
select {
case <-exitC:
case <-time.After(time.Duration(shutdownTimeout)):
case <-time.After(shutdownTimeout):
log.Debug().Str("id", string(id)).Int("signal", int(syscall.SIGKILL)).Msg("sending signal")
_ = task.Kill(ctx, syscall.SIGKILL)
select {
Expand Down
2 changes: 1 addition & 1 deletion pkg/container/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Monitor(addr string, ns string, id string, backend io.WriteCloser) error {

ctx := namespaces.WithNamespace(context.Background(), ns)

container, err := client.LoadContainer(ctx, string(id))
container, err := client.LoadContainer(ctx, id)
if err != nil {
log.Error().Err(err).Msg("metric container")
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func TestPyNACLCompatibilityEncryption(t *testing.T) {
sk := ed25519.NewKeyFromSeed(seed)

chiper := ""
fmt.Sscanf("0bfe9e3b9ce17fe6d570b165ea2a01034326b8c81d5f2c5384c8fe886552f074ec43017465598c4f5a857b495b445be46c3df48d14878bd0b1b907", "%x", &chiper)
_, err := fmt.Sscanf("0bfe9e3b9ce17fe6d570b165ea2a01034326b8c81d5f2c5384c8fe886552f074ec43017465598c4f5a857b495b445be46c3df48d14878bd0b1b907", "%x", &chiper)
require.NoError(t, err)

decrypted, err := Decrypt([]byte(chiper), sk)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/flist/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (f *flistModule) cleanCache(now time.Time, age time.Duration) error {

if sys, ok := sys.(*syscall.Stat_t); ok {
// int64 cast required for arm32 targets
atime := time.Unix(int64(sys.Atim.Sec), int64(sys.Atim.Nsec))
atime := time.Unix(sys.Atim.Sec, sys.Atim.Nsec)

if now.Sub(atime) > age {
if err := os.Remove(path); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gridtypes/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (i WorkloadID) Unique(n string) string {
b = b[:13]
}

return string(b)
return b
}

// IsValidName validates workload name
Expand Down
2 changes: 1 addition & 1 deletion pkg/gridtypes/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestTimestamp(t *testing.T) {
exp, err := json.Marshal(n)
require.NoError(err)

err = json.Unmarshal([]byte(exp), &v)
err = json.Unmarshal(exp, &v)
require.NoError(err)

require.Equal(Timestamp(n.Unix()), v)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gridtypes/zos/network_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NetworkID(twin uint32, network gridtypes.Name) NetID {
if len(b) > 13 {
b = b[:13]
}
return NetID(string(b))
return NetID(b)
}

func NetworkIDFromWorkloadID(wl gridtypes.WorkloadID) (NetID, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlight/ifaceutil/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func DeviceNameFromInputBytes(input []byte) string {
b = b[:13]
}

return string(b)
return b
}

func isHardwareAddrInValidRange(addr net.HardwareAddr) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlight/resource/mycelium.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func inspectMyceliumFile(path string) (inspection MyceliumInspection, err error)

// IP return the address in the 400::/7 subnet allocated by mycelium
func (m *MyceliumInspection) IP() net.IP {
return net.IP(m.Address)
return m.Address
}

// Subnet return the 400::/64 subnet allocated by mycelium
Expand Down
2 changes: 1 addition & 1 deletion pkg/perf/iperf/iperf_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (t *IperfTest) runIperfTest(ctx context.Context, clientIP string, tcp bool)
operation := func() error {
res := runIperfCommand(ctx, opts)
if res.Error == errServerBusy {
return fmt.Errorf(errServerBusy)
return errors.New(errServerBusy)
}

report = res
Expand Down
2 changes: 1 addition & 1 deletion pkg/perf/publicip/publicip_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stub
}

for _, node := range nodes {
if node.NodeID >= uint32(nodeID) {
if node.NodeID >= nodeID {
continue
}
n, err := substrateGateway.GetNode(ctx, node.NodeID)
Expand Down
2 changes: 1 addition & 1 deletion pkg/power/ethtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func valueOfFlag(output []byte, flag Flag) (string, error) {
if len(parts) != 2 {
return "", fmt.Errorf("invalid ethtool output format (%s)", line)
}
return strings.TrimSpace(string(parts[1])), nil
return strings.TrimSpace(parts[1]), nil
}

return "", ErrFlagNotFound
Expand Down
2 changes: 1 addition & 1 deletion pkg/primitives/vm-light/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (p *Manager) newPrivNetworkInterface(ctx context.Context, dl gridtypes.Depl
// privIP6,
},
Routes: iface.Routes,
IP4DefaultGateway: net.IP(iface.Routes[0].Gateway),
IP4DefaultGateway: iface.Routes[0].Gateway,
// IP6DefaultGateway: gw6,
PublicIPv4: false,
PublicIPv6: false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *substrateTwins) GetKey(id uint32) ([]byte, error) {

key := user.Account.PublicKey()
s.mem.Add(id, key)
return []byte(key), nil
return key, nil
}

type substrateAdmins struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func (e *NativeEngine) validate(ctx context.Context, dl *gridtypes.Deployment, n
return ctx, fmt.Errorf("substrate is not configured in engine")
}

contract, subErr := e.substrateGateway.GetContract(ctx, uint64(dl.ContractID))
contract, subErr := e.substrateGateway.GetContract(ctx, dl.ContractID)
if subErr.IsError() {
return nil, errors.Wrap(subErr.Err, "failed to get deployment contract")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/storage.fs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (s *Fs) byTwin(twin uint32) ([]uint64, error) {
continue
}

ids = append(ids, uint64(id))
ids = append(ids, id)
}

return ids, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestCacheResize(t *testing.T) {
Excl: 1,
},
}
vol.On("Limit", uint64(cacheSize)).Return(nil)
vol.On("Limit", cacheSize).Return(nil)
err := m.checkAndResizeCache(&vol, cacheSize)
require.NoError(t, err)

Expand Down Expand Up @@ -444,7 +444,7 @@ func TestCacheResize(t *testing.T) {
Excl: 0, // no files
},
}
vol.On("Limit", uint64(cacheSize)).Return(nil)
vol.On("Limit", cacheSize).Return(nil)
err = m.checkAndResizeCache(&vol, cacheSize)
require.NoError(t, err)

Expand All @@ -468,7 +468,7 @@ func TestCacheResize(t *testing.T) {
Excl: 91,
},
}
vol.On("Limit", uint64(100+cacheSize)).Return(nil)
vol.On("Limit", 100+cacheSize).Return(nil)
err = m.checkAndResizeCache(&vol, cacheSize)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion pkg/vm/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (m *Module) withLogs(path string, err error) error {
return errors.Wrapf(err, "failed to tail machine logs: %s", tailErr)
}

return errors.Wrapf(err, string(logs))
return errors.Wrap(err, logs)
}

func (m *Module) checkDevicesUsed(devices []string) error {
Expand Down
16 changes: 8 additions & 8 deletions pkg/zdb/admin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zdb

import (
"fmt"
"errors"
"strings"

"github.com/gomodule/redigo/redis"
Expand All @@ -18,7 +18,7 @@ func (c *clientImpl) CreateNamespace(name string) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *clientImpl) DeleteNamespace(name string) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand All @@ -70,7 +70,7 @@ func (c *clientImpl) NamespaceSetSize(name string, size uint64) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand All @@ -84,7 +84,7 @@ func (c *clientImpl) NamespaceSetPassword(name, password string) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand All @@ -98,7 +98,7 @@ func (c *clientImpl) NamespaceSetMode(name, mode string) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand All @@ -116,7 +116,7 @@ func (c *clientImpl) NamespaceSetLock(name string, lock bool) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand All @@ -136,7 +136,7 @@ func (c *clientImpl) NamespaceSetPublic(name string, public bool) error {
return err
}
if ok != "OK" {
return fmt.Errorf(ok)
return errors.New(ok)
}
return nil
}
Expand Down
35 changes: 4 additions & 31 deletions tools/zos-update-worker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,14 @@ ldflags='-w -s -X $(version).Branch=$(branch) -X $(version).Revision=$(revision)
all: getdeps test

getdeps:
@echo "Installing staticcheck" && go get -u honnef.co/go/tools/cmd/staticcheck && go install honnef.co/go/tools/cmd/staticcheck
@echo "Installing gocyclo" && go get -u github.com/fzipp/gocyclo/cmd/gocyclo && go install github.com/fzipp/gocyclo/cmd/gocyclo
@echo "Installing deadcode" && go get -u github.com/remyoudompheng/go-misc/deadcode && go install github.com/remyoudompheng/go-misc/deadcode
@echo "Installing misspell" && go get -u github.com/client9/misspell/cmd/misspell && go install github.com/client9/misspell/cmd/misspell
@echo "Installing golangci-lint" && go install github.com/golangci/golangci-lint/cmd/[email protected]
@echo "Installing golangci-lint" && go get github.com/golangci/golangci-lint/cmd/golangci-lint && go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy


verifiers: fmt lint cyclo deadcode spelling staticcheck

fmt:
@echo "Running $@"
@gofmt -d .

lint:
@echo "Running $@"
@${GOPATH}/bin/golangci-lint run

cyclo:
@echo "Running $@"
@${GOPATH}/bin/gocyclo -over 100 .

deadcode:
@echo "Running $@"
@${GOPATH}/bin/deadcode -test $(shell go list ./...) || true

spelling:
@echo "Running $@"
@${GOPATH}/bin/misspell -i monitord -error `find .`

staticcheck:
@echo "Running $@"
@${GOPATH}/bin/staticcheck -- ./...
golangci-lint run -c ../../.golangci.yml

test: verifiers
test: lint
go test -v -vet=off ./...

benchmarks:
Expand All @@ -53,7 +26,7 @@ coverage: clean
go test -v -vet=off ./... -coverprofile=coverage/coverage.out
go tool cover -html=coverage/coverage.out -o coverage/coverage.html

testrace: verifiers
testrace: lint
go test -v -race -vet=off ./...

run:
Expand Down

0 comments on commit 78ce6f4

Please sign in to comment.