Skip to content

Commit

Permalink
[DVT-299] chore: improve ci/makefile and fix shadowed variable issues (
Browse files Browse the repository at this point in the history
…#17)

* fix: silence SA1019 errors raised by importing deprecated packages

* fix: shadowed variables issues raised by shadow

* ci: run shadow in linting job

* ci: update the ci to fail at any problem reported by golangci-lint

* chore: run gofmt by default when running golangci-lint

* chore: add phony targets in makefile
  • Loading branch information
leovct authored Nov 24, 2022
1 parent 75fd909 commit 35b331e
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 28 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ concurrency:

jobs:
lint:
# source: https://github.com/golangci/golangci-lint-action
name: Lint
runs-on: ubuntu-latest
if: (github.event.action != 'closed' || github.event.pull_request.merged == true)
Expand All @@ -27,7 +26,10 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: latest
continue-on-error: true
- name: Install shadow
run: go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
- name: Run shadow
run: shadow ./...
test:
if: (github.event.action != 'closed' || github.event.pull_request.merged == true)
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters:
enable:
- gofmt
- govet
- staticcheck
67 changes: 55 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,79 @@ LD_FLAGS += -X \"github.com/maticnetwork/polygon-cli/version.Date=$(CUR_DATE)\"
LD_FLAGS += -X \"github.com/maticnetwork/polygon-cli/version.BuiltBy=makefile\"
STATIC_LD_FLAGS=$(LD_FLAGS) -extldflags=-static

$(BUILD_DIR):
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Build

.PHONY: $(BUILD_DIR)
$(BUILD_DIR): ## Create the binary folder.
mkdir -p $(BUILD_DIR)

build: $(BUILD_DIR)
.PHONY: run
run: lint ## Run the go program.
go run main.go

.PHONY: build
build: lint $(BUILD_DIR) ## Build go binary.
go build -o $(BUILD_DIR)/$(BIN_NAME) main.go

cross: $(BUILD_DIR)
.PHONY: cross
cross: $(BUILD_DIR) lint ## Cross-compile go binaries using CGO.
env CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags "$(STATIC_LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/linux-arm64-$(BIN_NAME) main.go
env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags "$(STATIC_LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/linux-amd64-$(BIN_NAME) main.go

# # mac builds - this will be functional but will still have secp issues
# mac builds - this will be functional but will still have secp issues
env GOOS=darwin GOARCH=arm64 go build -ldflags "$(LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/darwin-arm64-$(BIN_NAME) main.go
env GOOS=darwin GOARCH=amd64 go build -ldflags "$(LD_FLAGS)" -tags netgo -o $(BUILD_DIR)/darwin-amd64-$(BIN_NAME) main.go

simplecross: $(BUILD_DIR)
.PHONY: simplecross
simplecross: $(BUILD_DIR) lint ## Cross-compile go binaries without using CGO.
env GOOS=linux GOARCH=arm64 go build -o $(BUILD_DIR)/linux-arm64-$(BIN_NAME) main.go
env GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/darwin-arm64-$(BIN_NAME) main.go
env GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/linux-amd64-$(BIN_NAME) main.go
env GOOS=darwin GOARCH=amd64 go build -o $(BUILD_DIR)/darwin-amd64-$(BIN_NAME) main.go

install: build
.PHONY: install
install: build ## Install the go binary.
$(RM) $(INSTALL_DIR)/$(BIN_NAME)
cp $(BUILD_DIR)/$(BIN_NAME) $(INSTALL_DIR)

clean:
.PHONY: clean
clean: ## Clean the binary folder.
$(RM) -r $(BUILD_DIR)

##@ Lint

.PHONY: tidy
tidy: ## Add missing and remove unused modules.
go mod tidy

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

# shadow reports shadowed variables
# https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/shadow
# go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
.PHONY: vet
vet: ## Run go vet and shadow against code.
go vet ./...
shadow ./...

# golangci-lint runs gofmt, govet, staticcheck and other linters
# https://golangci-lint.run/usage/install/#local-installation
lint:
golangci-lint run
.PHONY: golangci-lint
golangci-lint: ## Run golangci-lint against code.
golangci-lint run --fix

.PHONY: lint
lint: tidy vet golangci-lint ## Run all the linter tools against code.

##@ Test

.PHONY: test
test: lint ## Run tests.
go test ./... -coverprofile=coverage.out
go tool cover -func coverage.out

test:
go test -v ./... -covermode=atomic -coverprofile=coverage.out && go tool cover -func coverage.out
6 changes: 4 additions & 2 deletions cmd/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ block chain clients and tests.
BaseFee: big.NewInt(0),
}

jsonData, err := gen.MarshalJSON()
var jsonData []byte
jsonData, err = gen.MarshalJSON()
if err != nil {
return err
}
Expand All @@ -135,7 +136,8 @@ block chain clients and tests.
return err
}

gblock, err := gen.Commit(db)
var gblock *ethtypes.Block
gblock, err = gen.Commit(db)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (

"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/md4" //nolint:staticcheck
"golang.org/x/crypto/ripemd160" //nolint:staticcheck
"golang.org/x/crypto/sha3"
)

Expand Down
14 changes: 7 additions & 7 deletions cmd/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ func runLoadTest(ctx context.Context) error {
if *inputLoadTestParams.IsAvail {
log.Info().Msg("Running in Avail mode")
loopFunc = func() error {

api, err := gsrpc.NewSubstrateAPI(inputLoadTestParams.URL.String())
var api *gsrpc.SubstrateAPI
api, err = gsrpc.NewSubstrateAPI(inputLoadTestParams.URL.String())
if err != nil {
return err
}
Expand Down Expand Up @@ -424,7 +424,7 @@ func runLoadTest(ctx context.Context) error {
log.Info().Msg("Time's up")
case <-sigCh:
log.Info().Msg("Interrupted.. Stopping load test")
case err := <-errCh:
case err = <-errCh:
if err != nil {
log.Fatal().Err(err).Msg("Received critical error while running load test")
}
Expand Down Expand Up @@ -525,7 +525,8 @@ func mainLoop(ctx context.Context, c *ethclient.Client) error {
// block while the contract is pending
waitCounter := 30
for {
ltCounter, err := ltContract.GetCallCounter(cops)
var ltCounter *big.Int
ltCounter, err = ltContract.GetCallCounter(cops)

if err != nil {
log.Trace().Msg("Waiting for Load Test contract to deploy")
Expand Down Expand Up @@ -597,9 +598,8 @@ func mainLoop(ctx context.Context, c *ethclient.Client) error {
var endReq time.Time

for j = 0; j < requests; j = j + 1 {

if rl != nil {
err := rl.Wait(ctx)
err = rl.Wait(ctx)
if err != nil {
log.Error().Err(err).Msg("Encountered a rate limiting error")
}
Expand Down Expand Up @@ -952,7 +952,7 @@ func availLoop(ctx context.Context, c *gsrpc.SubstrateAPI) error {
for j = 0; j < requests; j = j + 1 {

if rl != nil {
err := rl.Wait(ctx)
err = rl.Wait(ctx)
if err != nil {
log.Error().Err(err).Msg("Encountered a rate limiting error")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ var monitorCmd = &cobra.Command{
errChan := make(chan error)
go func() {
for {
cs, err := getChainState(ctx, ec)
var cs *chainState
cs, err = getChainState(ctx, ec)
if err != nil {
log.Error().Err(err).Msg("Encountered issue fetching network information")
time.Sleep(5 * time.Second)
Expand Down
3 changes: 2 additions & 1 deletion cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ child accounts or generate new accmounts along with a seed phrase`,
}

if *inputRootOnly {
key, err := pw.ExportRootAddress()
var key *hdwallet.PolyWalletExport
key, err = pw.ExportRootAddress()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion hdwallet/hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/tyler-smith/go-bip39/wordlists"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/ripemd160" //nolint:staticcheck
"golang.org/x/crypto/sha3"
)

Expand Down

0 comments on commit 35b331e

Please sign in to comment.