Skip to content

Commit

Permalink
refactor: pipeline and linter (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Nov 21, 2023
1 parent 13c2509 commit 76c9d70
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 74 deletions.
31 changes: 11 additions & 20 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,17 @@ on:
- main

jobs:
linting:
golangci:
name: lint
runs-on: ubuntu-latest

steps:
- name: Install Go
uses: actions/setup-go@v3
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Checkout code
uses: actions/checkout@v3

- name: Installing golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/[email protected]

- name: Installing gofumpt
run: go install mvdan.cc/gofumpt@latest

- name: Installing goimports
run: go install golang.org/x/tools/cmd/goimports@latest

- name: Formatting and linting the project
run: make check
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54

64 changes: 64 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
linters:
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- gocognit
- godox
- gocritic
- gci
- containedctx
- contextcheck
- exhaustive
- wastedassign
- nonamedreturns
- ireturn
- gofumpt
- tagalign
- errname
- errorlint


linters-settings:
gosimple:
checks: ["all"]

govet:
enable-all: true
disable: fieldalignment
shadow:
strict: true

godox:
severity: warning

predeclared:
# Comma-separated list of predeclared identifiers to not report on.
# Default: ""
ignore: "len"
# Include method names and field names (i.e., qualified names) in checks.
# Default: false
q: true

tagliatelle:
# Check the struct tag name case.
case:
use-field-name: false
rules:
json: snake
yaml: snake

issues:
exclude-rules:
# disable funlen for all _test.go files
- path: _test.go
linters:
- funlen

- linters:
- govet
text: "shadow: declaration of \"err\" shadows"

50 changes: 21 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
### Testing
unit_test:
go test ./...
go test $(PACKAGES)

test:
go test ./... -covermode=atomic

race_test:
go test ./... --race

fmt:
gofumpt -l -w .

install-tools:
### dev tools
devtools:
@echo "Installing devtools"
go install golang.org/x/tools/cmd/goimports@latest
go install mvdan.cc/gofumpt@latest
go install github.com/golangci/golangci-lint/cmd/[email protected]

install-packages:
packages:
go mod tidy


### Formatting, linting, and vetting
fmt:
gofumpt -l -w .
go mod tidy
godot -w .

check:
golangci-lint run \
--build-tags "${BUILD_TAG}" \
--timeout=20m0s \
--enable=gofmt \
--enable=unconvert \
--enable=unparam \
--enable=asciicheck \
--enable=misspell \
--enable=revive \
--enable=decorder \
--enable=reassign \
--enable=usestdlibvars \
--enable=nilerr \
--enable=gosec \
--enable=exportloopref \
--enable=whitespace \
--enable=goimports \
--enable=gocyclo \
--enable=lll


build-bot:
go build -o build/main cmd/app.go
golangci-lint run --build-tags "${BUILD_TAG}" --timeout=20m0s

### building
build:
go build -o build/main cmd/app.go

6 changes: 3 additions & 3 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os/signal"
"syscall"

"pactus-bot/config"
"pactus-bot/discord"
"pactus-bot/wallet"
"github.com/kehiy/RoboPac/config"
"github.com/kehiy/RoboPac/discord"
"github.com/kehiy/RoboPac/wallet"
)

func main() {
Expand Down
3 changes: 1 addition & 2 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"pactus-bot/client"

"github.com/kehiy/RoboPac/client"
"github.com/yudai/pp"
)

Expand Down
7 changes: 3 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"path/filepath"
)

type Config struct {
Expand All @@ -19,18 +18,18 @@ type Config struct {
}

func Load(path string) (*Config, error) {
file, err := os.ReadFile(filepath.Join(path))
file, err := os.ReadFile(path)
if err != nil {
log.Printf("error loading configuration file: %v", err)
return nil, fmt.Errorf("error loading configuration file: %v", err)
return nil, fmt.Errorf("error loading configuration file: %w", err)
}

cfg := &Config{}
err = json.Unmarshal(file, cfg)

if err != nil {
log.Printf("error unmarshalling configuration file: %v", err)
return nil, fmt.Errorf("error unmarshalling configuration file: %v", err)
return nil, fmt.Errorf("error unmarshalling configuration file: %w", err)
}
return cfg, nil
}
12 changes: 7 additions & 5 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"strings"
"time"

"pactus-bot/client"
"pactus-bot/config"
"pactus-bot/wallet"

"github.com/bwmarrin/discordgo"
"github.com/kehiy/RoboPac/client"
"github.com/kehiy/RoboPac/config"
"github.com/kehiy/RoboPac/wallet"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/util"
Expand All @@ -32,6 +31,7 @@ type Bot struct {

func Start(cfg *config.Config, w *wallet.Wallet, ss *SafeStore) (*Bot, error) {
cm := client.NewClientMgr()

for _, s := range cfg.Servers {
c, err := client.NewClient(s)
if err != nil {
Expand Down Expand Up @@ -92,6 +92,7 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
return
}

if strings.ToLower(m.Content) == "address" {
msg := fmt.Sprintf("Faucet address is: %v", b.cfg.FaucetAddress)
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
Expand Down Expand Up @@ -190,7 +191,7 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
}

// help sends a message detailing how to use the bot discord-client side
// nolint
// nolint.
func help(s *discordgo.Session, m *discordgo.MessageCreate) {
_, _ = s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
Title: "Pactus Universal Robot",
Expand Down Expand Up @@ -234,6 +235,7 @@ func (b *Bot) validateInfo(address, discordID string) (string, string, bool, str
if err != nil {
return "", "", false, err.Error()
}

if isValidator {
return "", "", false, "Sorry. Your address is in the list of active validators. You do not need faucet again."
}
Expand Down
17 changes: 10 additions & 7 deletions discord/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"sync"

"pactus-bot/config"
"github.com/kehiy/RoboPac/config"
)

// Validator is a value stored in the cache.
Expand Down Expand Up @@ -38,7 +38,7 @@ func LoadData(cfg *config.Config) (*SafeStore, error) {
file, err := os.ReadFile(cfg.ValidatorDataPath)
if err != nil {
log.Printf("error loading validator data: %v", err)
return nil, fmt.Errorf("error loading data file: %v", err)
return nil, fmt.Errorf("error loading data file: %w", err)
}
if len(file) == 0 {
ss := &SafeStore{
Expand All @@ -51,7 +51,7 @@ func LoadData(cfg *config.Config) (*SafeStore, error) {
data, err := unmarshalJSON(file)
if err != nil {
log.Printf("error unmarshalling validator data: %v", err)
return nil, fmt.Errorf("error unmarshalling validator data: %v", err)
return nil, fmt.Errorf("error unmarshalling validator data: %w", err)
}
ss := &SafeStore{
syncMap: data,
Expand All @@ -60,7 +60,7 @@ func LoadData(cfg *config.Config) (*SafeStore, error) {
return ss, nil
}

// SetData Set a given value to the data storage
// SetData Set a given value to the data storage.
func (ss *SafeStore) SetData(peerID, address, discordName, discordID string, amount float64) error {
ss.syncMap.Store(peerID, &Validator{
DiscordName: discordName, DiscordID: discordID,
Expand All @@ -70,16 +70,16 @@ func (ss *SafeStore) SetData(peerID, address, discordName, discordID string, amo
data, err := marshalJSON(ss.syncMap)
if err != nil {
log.Printf("error marshalling validator data file: %v", err)
return fmt.Errorf("error marshalling validator data file: %v", err)
return fmt.Errorf("error marshalling validator data file: %w", err)
}
if err := os.WriteFile(ss.cfg.ValidatorDataPath, data, 0o600); err != nil {
log.Printf("failed to write to %s: %v", ss.cfg.ValidatorDataPath, err)
return fmt.Errorf("failed to write to %s: %v", ss.cfg.ValidatorDataPath, err)
return fmt.Errorf("failed to write to %s: %w", ss.cfg.ValidatorDataPath, err)
}
return nil
}

// GetData retrieves the given key from the storage
// GetData retrieves the given key from the storage.
func (ss *SafeStore) GetData(peerID string) (*Validator, bool) {
entry, found := ss.syncMap.Load(peerID)
if !found {
Expand All @@ -92,6 +92,7 @@ func (ss *SafeStore) GetData(peerID string) (*Validator, bool) {
func (ss *SafeStore) FindDiscordID(discordID string) (*Validator, bool) {
validator := &Validator{}
exists := false

ss.syncMap.Range(func(key, value any) bool {
v := value.(*Validator)
if validator.DiscordID == discordID {
Expand All @@ -106,6 +107,7 @@ func (ss *SafeStore) FindDiscordID(discordID string) (*Validator, bool) {
func (ss *SafeStore) GetDistribution() (uint, float64) {
totalDistribution := float64(0)
totalValidators := uint(0)

ss.syncMap.Range(func(key, value any) bool {
v := value.(*Validator)
if v != nil {
Expand All @@ -119,6 +121,7 @@ func (ss *SafeStore) GetDistribution() (uint, float64) {

func marshalJSON(m *sync.Map) ([]byte, error) {
tmpMap := make(map[string]*Validator)

m.Range(func(k, v interface{}) bool {
tmpMap[k.(string)] = v.(*Validator)
return true
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module pactus-bot
module github.com/kehiy/RoboPac

go 1.21

Expand Down
6 changes: 3 additions & 3 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"log"
"os"

"pactus-bot/config"

"github.com/kehiy/RoboPac/config"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/util"
Expand Down Expand Up @@ -86,6 +85,7 @@ func (w *Wallet) GetBalance() *Balance {
log.Printf("error getting staking amount: %v", err)
return balance
}

balance.Staked = util.ChangeToCoin(stake)
return balance
}
Expand All @@ -103,7 +103,7 @@ func IsValidData(address, pubKey string) bool {
return err == nil
}

// function to check if file exists
// function to check if file exists.
func doesWalletExist(fileName string) bool {
_, err := os.Stat(fileName)
return !os.IsNotExist(err)
Expand Down

0 comments on commit 76c9d70

Please sign in to comment.