Skip to content

Commit

Permalink
build: use golangci-lint CLI to lint files (#80)
Browse files Browse the repository at this point in the history
## Description

This PR updates the linting process to use the `golangci-lint` CLI. This should guarantee major forward compatibility as the golangci-lint version is now tracked within the codebase along with other dependencies

Depends-On: #79 

## Checklist
- [x] Targeted PR against correct branch.
- [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
- [ ] Wrote unit tests.  
- [x] Re-reviewed `Files changed` in the Github PR explorer.
  • Loading branch information
RiccardoM committed Sep 20, 2022
1 parent 8c0f7f1 commit 8848496
Show file tree
Hide file tree
Showing 21 changed files with 742 additions and 352 deletions.
22 changes: 13 additions & 9 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 6
steps:
- name: Checkout
- uses: actions/checkout@v3
- name: Compute Diff
- uses: technote-space/get-diff-action@v4
- name: Checkout 🛎️
uses: actions/checkout@v3

- name: Compute diff 📜
uses: technote-space/get-diff-action@v6.1.0
with:
SUFFIX_FILTER: |
.go
.mod
.sum
- name: Run lint
uses: golangci/golangci-lint-action@v3
- name: Setup Go 🧰
if: "env.GIT_DIFF != ''"
uses: actions/setup-go@v3
with:
version: v1.28
args: --timeout 10m
github-token: ${{ secrets.GITHUB_TOKEN }}
go-version: 1.18

- name: Run lint ✅
if: "env.GIT_DIFF != ''"
run: make lint
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.18
- name: Test & Create coverage report
run: make install test-unit
- name: Upload cove coverage
Expand Down
78 changes: 64 additions & 14 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,72 @@
run:
tests: false
# # timeout for analysis, e.g. 30s, 5m, default is 1m
# timeout: 5m

linters:
disable-all: true
enable:
- errcheck
- bodyclose
- deadcode
- depguard
- dogsled
- gocritic
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- maligned
- misspell
- nakedret
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unused
- unparam
- misspell
- govet
- gocyclo
linters-settings:
gocyclo:
min-complexity: 11
errcheck:
ignore: fmt:.*,io/ioutil:^Read.*,github.com/spf13/viper:BindPFlag
golint:
min-confidence: 1.1
- nolintlint

issues:
exclude:
- composite
run:
tests: false
exclude-rules:
- text: "Use of weak random number generator"
linters:
- gosec
- text: "comment on exported var"
linters:
- golint
- text: "don't use an underscore in package name"
linters:
- golint
- text: "ST1003:"
linters:
- stylecheck
# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
# https://github.com/dominikh/go-tools/issues/389
- text: "ST1016:"
linters:
- stylecheck

- text: "singleCaseSwitch: should rewrite switch statement to if statement"
linters:
- gocritic

max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
nolintlint:
allow-unused: false
allow-leading-space: true
require-explanation: false
require-specific: false
43 changes: 33 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ install: go.sum
@go install -mod=readonly $(BUILD_FLAGS) ./cmd/juno
.PHONY: install

###############################################################################
### Tools & Dependencies ###
###############################################################################

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
@go mod verify
@go mod tidy

clean:
rm -rf $(BUILDDIR)/

.PHONY: go-mod-cache go.sum clean

###############################################################################
### Tests & Simulation ###
###############################################################################
Expand All @@ -65,20 +83,25 @@ test-unit: start-docker-test
@go test -mod=readonly -v -coverprofile coverage.txt ./...
.PHONY: test-unit

###############################################################################
### Linting ###
###############################################################################
golangci_lint_cmd=github.com/golangci/golangci-lint/cmd/golangci-lint

lint:
golangci-lint run --out-format=tab
.PHONY: lint
@echo "--> Running linter"
@go run $(golangci_lint_cmd) run --timeout=10m

lint-fix:
golangci-lint run --fix --out-format=tab --issues-exit-code=0
.PHONY: lint-fix
@echo "--> Running linter"
@go run $(golangci_lint_cmd) run --fix --out-format=tab --issues-exit-code=0

.PHONY: lint lint-fix

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs goimports -w -local github.com/forbole/juno
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs goimports -w -local github.com/forbole/juno
.PHONY: format

clean:
rm -f tools-stamp ./build/**
.PHONY: clean
.PHONY: lint lint-fix format
4 changes: 2 additions & 2 deletions cmd/migrate/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ type LoggingConfig struct {
}

type ParsingConfig struct {
GenesisFilePath string `toml:"genesis_file_path"`
Workers int64 `toml:"workers"`
StartHeight int64 `toml:"start_height"`
ParseNewBlocks bool `toml:"listen_new_blocks"`
ParseOldBlocks bool `toml:"parse_old_blocks"`
GenesisFilePath string `toml:"genesis_file_path"`
ParseGenesis bool `toml:"parse_genesis"`
StartHeight int64 `toml:"start_height"`
FastSync bool `toml:"fast_sync"`
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/v2/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func RunMigration(_ *parsecmdtypes.Config) error {
}

v2File := config.GetConfigFilePath()
return ioutil.WriteFile(v2File, bz, 0666)
return ioutil.WriteFile(v2File, bz, 0600)
}

func migrateConfig() (Config, error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type Config struct {
}

type ParserConfig struct {
GenesisFilePath string `yaml:"genesis_file_path,omitempty"`
Workers int64 `yaml:"workers"`
StartHeight int64 `yaml:"start_height"`
ParseNewBlocks bool `yaml:"listen_new_blocks"`
ParseOldBlocks bool `yaml:"parse_old_blocks"`
GenesisFilePath string `yaml:"genesis_file_path,omitempty"`
ParseGenesis bool `yaml:"parse_genesis"`
StartHeight int64 `yaml:"start_height"`
FastSync bool `yaml:"fast_sync,omitempty"`

// Following there are the new fields that have been added into v3. We use pointers and the "omitempty" clause
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func RunMigration(parseConfig *parsecmdtypes.Config) error {
return fmt.Errorf("error while serializing config: %s", err)
}

err = ioutil.WriteFile(config.GetConfigFilePath(), bz, 0666)
err = ioutil.WriteFile(config.GetConfigFilePath(), bz, 0600)
if err != nil {
return fmt.Errorf("error while writing v3 config: %s", err)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/start/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func NewStartCmd(cmdCfg *parsecmdtypes.Config) *cobra.Command {
}
}

return StartParsing(context)
return startParsing(context)
},
}
}

// StartParsing represents the function that should be called when the parse command is executed
func StartParsing(ctx *parser.Context) error {
// startParsing represents the function that should be called when the parse command is executed
func startParsing(ctx *parser.Context) error {
// Get the config
cfg := config.Cfg.Parser
logging.StartHeight.Add(float64(cfg.StartHeight))
Expand All @@ -76,7 +76,7 @@ func StartParsing(ctx *parser.Context) error {
exportQueue := types.NewQueue(25)

// Create workers
workers := make([]parser.Worker, cfg.Workers, cfg.Workers)
workers := make([]parser.Worker, cfg.Workers)
for i := range workers {
workers[i] = parser.NewWorker(ctx, exportQueue, i)
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func mustGetLatestHeight(ctx *parser.Context) int64 {
// trapSignal will listen for any OS signal and invoke Done on the main
// WaitGroup allowing the main process to gracefully exit.
func trapSignal(ctx *parser.Context) {
var sigCh = make(chan os.Signal)
var sigCh = make(chan os.Signal, 1)

signal.Notify(sigCh, syscall.SIGTERM)
signal.Notify(sigCh, syscall.SIGINT)
Expand Down
12 changes: 7 additions & 5 deletions database/legacy/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ func (db *Migrator) insertTransactionMessages(tx types.TransactionRow, partition
return fmt.Errorf("error while unmarshaling messages: %s", err)
}

for i, m := range msgs {
for i, msg := range msgs {
msg := msg

// Append params
msgType := m["@type"].(string)[1:] // remove head "/"
involvedAddresses := types.MessageParser(m)
delete(m, "@type")
msgType := msg["@type"].(string)[1:] // remove head "/"
involvedAddresses := types.MessageParser(msg)
delete(msg, "@type")

mBz, err := json.Marshal(&m)
mBz, err := json.Marshal(&msg)
if err != nil {
return fmt.Errorf("error while marshaling msg value to json: %s", err)
}
Expand Down
4 changes: 3 additions & 1 deletion database/migrate/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var DefaultAccountParser = []string{
}

func MessageParser(msg map[string]interface{}) (addresses string) {
accountParser := append(DefaultAccountParser, CustomAccountParser...)
var accountParser []string
accountParser = append(accountParser, DefaultAccountParser...)
accountParser = append(accountParser, CustomAccountParser...)

addresses += "{"
for _, role := range accountParser {
Expand Down
11 changes: 6 additions & 5 deletions database/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ func (db *Database) GetLastBlockHeight() (int64, error) {
stmt := `SELECT height FROM block ORDER BY height DESC LIMIT 1;`

var height int64
if err := db.SQL.QueryRow(stmt).Scan(&height); err != nil {
err := db.SQL.QueryRow(stmt).Scan(&height)
if err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
// If no rows stored in block table, return 0 as height
return 0, nil
}
return 0, fmt.Errorf("error while getting last block height, error: %s", err)
}

if height == 0 {
return 0, fmt.Errorf("cannot get block height, no blocks saved")
}

return height, nil
}

Expand Down

0 comments on commit 8848496

Please sign in to comment.