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

Commit

Permalink
feat: config main functions and test
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Oct 23, 2023
1 parent c0a9bf3 commit 4165998
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 43 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ linters:
- dupl
- goconst
- gocognit # Consider to enable it
- funlen # Consider to enable it
- forbidigo # 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
Expand Down
80 changes: 54 additions & 26 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package config

import (
"bytes"
_ "embed"
"errors"
"os"

"github.com/zurvan-lab/TimeTrace/log"
"gopkg.in/yaml.v2"
)

type Config struct {
Name string `yaml:"name"`
Authorization bool `yaml:"authorization"`
Proc Proc `yaml:"proc"`
Listen Listen `yaml:"server"`
Log Log `yaml:"log"`
FirstUser FirstUser `yaml:"user"`
}
//go:embed config.yaml
var configBytes []byte

type Proc struct {
Cores int `yaml:"cores"`
Threads int `yaml:"threads"`
type Config struct {
Name string `yaml:"name"`
Server Server `yaml:"server"`
Log Log `yaml:"log"`
Users []User `yaml:"users"`
}

type Listen struct {
type Server struct {
IP string `yaml:"listen"`
Port string `yaml:"port"`
}
Expand All @@ -30,35 +28,65 @@ type Log struct {
Path string `yaml:"path"`
}

type FirstUser struct {
Name string `yaml:"name"`
Token string `yaml:"token"`
Cmd []string `yaml:"cmd"`
type User struct {
Name string `yaml:"name"`
PassWord string `yaml:"pass_word"`
Cmds []string `yaml:"cmds"`
}

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

func (c Config) BasicCheck() error {
return nil
func DefaultConfig() *Config {
config := &Config{}
config.Log.Path = "ttrace.log"
config.Name = "time_trace"
config.Server.IP = "localhost"
config.Server.Port = "7070"
rootUser := User{
Name: "root",
PassWord: "super_secret_password",
Cmds: []string{"*"},
}
config.Users = append(config.Users, rootUser)

return config
}

func LoadFromFile(path string) *Config {
file, err := os.Open(path)
if err != nil {
log.Error("Can not open the config file", "error: ", err)
panic(err)
}
defer file.Close()

config := createConfig()
config := &Config{}

decoder := yaml.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Error("Can not decode the Config Yaml file", "error: ", err)
panic(err)
}
// TODO: validate config
// TODO: Log

err = config.BasicCheck()
if err != nil {
panic(err)
}

return config
}

func (conf *Config) ToYAML() []byte {
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
err := encoder.Encode(conf)
if err != nil {
panic(err)
}

return buf.Bytes()
}
33 changes: 20 additions & 13 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
name: TimeTraceConfig
authorization: false

proc:
cores: 2
threads: 4
name: time_trace

server:
listen: localhost
port: 7070
port: "7070"

log:
path: ./log
path: ttrace.log

user:
name: root
token: super_secret_token
cmd:
- all
users:
- name: root
pass_word: super_secret_password
cmds:
- '*'
# Also you can use this for multiple users and limited commands
# users:
# - name: root
# pass_word: super_secret_password
# cmd:
# - * # all commands.
# - name: developer
# password: secret_password
# cmd:
# - GET
# - PUSH
# - DEL
56 changes: 56 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package config

import (
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDefaultConfig(t *testing.T) {
lines := strings.Split(string(configBytes), "\n")
defaultYaml := ""
for _, line := range lines {
if !(strings.HasPrefix(line, "# ") ||
strings.HasPrefix(line, "###") ||
strings.HasPrefix(line, " # ") ||
strings.HasPrefix(line, " # ")) {
defaultYaml += line
defaultYaml += "\n"
}
}

defaultConf := DefaultConfig()
defaultFunction := string(defaultConf.ToYAML())

defaultYaml = strings.ReplaceAll(defaultYaml, "##", "")
defaultYaml = strings.ReplaceAll(defaultYaml, "# all commands.", "")
defaultYaml = strings.ReplaceAll(defaultYaml, "\r\n", "\n") // For Windows
defaultYaml = strings.ReplaceAll(defaultYaml, "\n\n", "\n")
defaultFunction = strings.ReplaceAll(defaultFunction, "\n\n", "\n")

// fmt.Println(defaultFunction)
// fmt.Println(defaultYaml)
assert.Equal(t, defaultFunction, defaultYaml)
}

func TestLoadFromFile(t *testing.T) {
config := LoadFromFile("./config.yaml")
dConfig := DefaultConfig()

assert.Equal(t, "time_trace", config.Name)
assert.Equal(t, []string{"*"}, config.Users[0].Cmds)
assert.Equal(t, "7070", config.Server.Port)
assert.Equal(t, dConfig, config)
}

func TestBasicCheck(t *testing.T) {
c := DefaultConfig()
err := c.BasicCheck()
assert.NoError(t, err)

c.Users = []User{}
err = c.BasicCheck()
assert.Error(t, errors.New("invalid user length"), err)
}
2 changes: 0 additions & 2 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"bytes"
"fmt"
"testing"

"github.com/rs/zerolog/log"
Expand All @@ -22,7 +21,6 @@ func TestLogger(t *testing.T) {

out := buf.String()

fmt.Println(out)
assert.Contains(t, out, "010203")
assert.Contains(t, out, "!INVALID-KEY!")
assert.Contains(t, out, "!MISSING-VALUE!")
Expand Down

0 comments on commit 4165998

Please sign in to comment.