Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
chore: Improving golang ci config (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Kayhan Alizadeh <[email protected]>
  • Loading branch information
bansaltushar014 and kehiy authored Nov 12, 2023
1 parent 28aacc7 commit 558cd25
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 47 deletions.
61 changes: 21 additions & 40 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
linters:
enable-all: true
disable:
- scopelint # deprecated
- maligned # deprecated
- nosnakecase # deprecated
- interfacer # deprecated
- deadcode # deprecated
- ifshort # deprecated
- golint # deprecated
- structcheck # deprecated
- varcheck # deprecated
- exhaustivestruct # deprecated
- varnamelen
- wrapcheck
- paralleltest
- interfacebloat
- depguard
- gochecknoglobals
- gomnd
- cyclop
- nestif
- gochecknoinits
- dupl
- goconst
- gocognit # Consider to enable it
- gci # Consider to enable it (add it to make fmt)
- godox # Consider to enable it (only show warning)
- gocritic # Consider to enable it
- wsl # Consider to enable it
- testpackage # Consider to enable it (??)
- containedctx # Consider to enable it
- contextcheck # Consider to enable it
- exhaustive # Consider to enable it
- exhaustruct # Consider to enable it
- goerr113 # Consider to enable it
- wastedassign # Consider to enable it
- nonamedreturns # Consider to enable it
- nlreturn # Consider to enable it
- ireturn # Consider to enable it (??)
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- gocognit
- godox
- gocritic
- wsl
- containedctx
- contextcheck
- exhaustive
- goerr113
- wastedassign
- nonamedreturns
- nlreturn
- ireturn


linters-settings:
gosimple:
Expand Down Expand Up @@ -77,4 +58,4 @@ issues:

- linters:
- govet
text: "shadow: declaration of \"err\" shadows"
text: "shadow: declaration of \"err\" shadows"
19 changes: 15 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
//go:embed config.yaml
var configBytes []byte

var (
InvalidUserLength = errors.New("invalid user length")
CommandAtSameTime = errors.New("can't have all cmds and specific cmd at same time")
)

type Config struct {
Name string `yaml:"name"`
Server Server `yaml:"server"`
Expand All @@ -36,21 +41,24 @@ type User struct {
}

func (conf *Config) BasicCheck() error {
if len(conf.Users) <= 0 {
return errors.New("invalid user length")
if len(conf.Users) == 0 {
return InvalidUserLength
}

for _, u := range conf.Users {
allCmds := false

for _, c := range u.Cmds {
if c == "*" {
allCmds = true
}
}

if allCmds && len(u.Cmds) > 1 {
return errors.New("can't have all cmds and specific cmd at same time")
return CommandAtSameTime
}
}

return nil
}

Expand All @@ -64,8 +72,9 @@ func DefaultConfig() *Config {
WriteToFile: true,
Path: "log.ttrace",
},
Name: "time_trace",
Name: "time_trace",
}

rootUser := User{
Name: "root",
Password: "super_secret_password",
Expand All @@ -86,6 +95,7 @@ func LoadFromFile(path string) *Config {
config := &Config{}

decoder := yaml.NewDecoder(file)

err = decoder.Decode(&config)
if err != nil {
panic(err)
Expand All @@ -102,6 +112,7 @@ func LoadFromFile(path string) *Config {
func (conf *Config) ToYAML() []byte {
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)

err := encoder.Encode(conf)
if err != nil {
panic(err)
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"errors"
"strings"
"testing"

Expand All @@ -11,6 +10,7 @@ import (
func TestDefaultConfig(t *testing.T) {
lines := strings.Split(string(configBytes), "\n")
defaultYaml := ""

for _, line := range lines {
if !(strings.HasPrefix(line, "# ") ||
strings.HasPrefix(line, "###") ||
Expand Down Expand Up @@ -52,10 +52,10 @@ func TestBasicCheck(t *testing.T) {

c.Users = []User{}
err = c.BasicCheck()
assert.Error(t, errors.New("invalid user length"), err)
assert.Error(t, InvalidUserLength, err)

c.Users = []User{DefaultConfig().Users[0]}
c.Users[0].Cmds = []string{"*", "GET"}
err = c.BasicCheck()
assert.Error(t, errors.New("can't have all cmds and specific cmd at same time"), err)
assert.Error(t, CommandAtSameTime, err)
}
14 changes: 14 additions & 0 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Init(path string) *Database {

func (db *Database) AddSet(name string) string {
db.Sets[name] = make(Set)

return "DONE"
}

Expand All @@ -26,7 +27,9 @@ func (db *Database) AddSubSet(set, name string) string {
if !ok {
return "SETNF"
}

s[name] = make(SubSet, 0)

return "DONE"
}

Expand All @@ -35,7 +38,9 @@ func (db *Database) PushElement(set, subset string, e Element) string {
if !ok {
return "SUBSETNF"
}

db.Sets[set][subset] = append(db.Sets[set][subset], e)

return "DONE"
}

Expand All @@ -44,7 +49,9 @@ func (db *Database) DropSet(name string) string {
if !ok {
return "SETNF"
}

delete(db.Sets, name)

return "DONE"
}

Expand All @@ -53,12 +60,15 @@ func (db *Database) DropSubSet(set, subset string) string {
if !ok {
return "SUBETNF"
}

delete(db.Sets[set], subset)

return "DONE"
}

func (db *Database) CleanSets() string {
db.Sets = make(Sets)

return "DONE"
}

Expand All @@ -67,7 +77,9 @@ func (db *Database) CleanSet(name string) string {
if !ok {
return "SETNF"
}

db.Sets[name] = make(Set)

return "DONE"
}

Expand All @@ -76,6 +88,8 @@ func (db *Database) CleanSubSet(set, subset string) string {
if !ok {
return "SUBETNF"
}

db.Sets[set][subset] = make(SubSet, 0)

return "DONE"
}
2 changes: 2 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func addFields(event *zerolog.Event, keyvals ...interface{}) *zerolog.Event {
if len(keyvals)%2 != 0 {
keyvals = append(keyvals, "!MISSING-VALUE!")
}

for i := 0; i < len(keyvals); i += 2 {
key, ok := keyvals[i].(string)
if !ok {
Expand All @@ -29,6 +30,7 @@ func addFields(event *zerolog.Event, keyvals ...interface{}) *zerolog.Event {
event.Any(key, v)
}
}

return event
}

Expand Down

0 comments on commit 558cd25

Please sign in to comment.