diff --git a/.golangci.yml b/.golangci.yml index e7eb054..68db5b0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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: @@ -77,4 +58,4 @@ issues: - linters: - govet - text: "shadow: declaration of \"err\" shadows" \ No newline at end of file + text: "shadow: declaration of \"err\" shadows" diff --git a/config/config.go b/config/config.go index 3a3c2bb..c200174 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` @@ -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 } @@ -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", @@ -86,6 +95,7 @@ func LoadFromFile(path string) *Config { config := &Config{} decoder := yaml.NewDecoder(file) + err = decoder.Decode(&config) if err != nil { panic(err) @@ -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) diff --git a/config/config_test.go b/config/config_test.go index 928dacb..89d062f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,7 +1,6 @@ package config import ( - "errors" "strings" "testing" @@ -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, "###") || @@ -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) } diff --git a/core/database/database.go b/core/database/database.go index 76e169b..643cff1 100644 --- a/core/database/database.go +++ b/core/database/database.go @@ -18,6 +18,7 @@ func Init(path string) *Database { func (db *Database) AddSet(name string) string { db.Sets[name] = make(Set) + return "DONE" } @@ -26,7 +27,9 @@ func (db *Database) AddSubSet(set, name string) string { if !ok { return "SETNF" } + s[name] = make(SubSet, 0) + return "DONE" } @@ -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" } @@ -44,7 +49,9 @@ func (db *Database) DropSet(name string) string { if !ok { return "SETNF" } + delete(db.Sets, name) + return "DONE" } @@ -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" } @@ -67,7 +77,9 @@ func (db *Database) CleanSet(name string) string { if !ok { return "SETNF" } + db.Sets[name] = make(Set) + return "DONE" } @@ -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" } diff --git a/log/logger.go b/log/logger.go index a2b1837..3c4c579 100644 --- a/log/logger.go +++ b/log/logger.go @@ -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 { @@ -29,6 +30,7 @@ func addFields(event *zerolog.Event, keyvals ...interface{}) *zerolog.Event { event.Any(key, v) } } + return event }