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

feat: initing cli tool in cmd #82

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package commands

import (
cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/TimeTrace/core/database"
tte "github.com/zurvan-lab/TimeTrace/utils/errors"
)

func RunCommand(parentCmd *cobra.Command) {
run := &cobra.Command{
Use: "run",
Short: "Runs an instance of time trace.",
}
parentCmd.AddCommand(run)

confingPath := run.Flags().StringP("config", "c", "", "Path to your config.yaml file.")

run.Run = func(cmd *cobra.Command, args []string) {
if confingPath == nil || *confingPath == "" {
dead(cmd, tte.ErrInavlidConfigPath)
}

cfg, err := config.LoadFromFile(*confingPath)
if err != nil {
dead(cmd, err)
}

_ = database.Init(cfg)
}
}
12 changes: 12 additions & 0 deletions cmd/commands/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package commands

import (
"os"

"github.com/spf13/cobra"
)

func dead(cmd *cobra.Command, err error) {
cmd.PrintErr(err)
os.Exit(1)
}
20 changes: 20 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
package main

import (
"github.com/spf13/cobra"
timetrace "github.com/zurvan-lab/TimeTrace"
"github.com/zurvan-lab/TimeTrace/cmd/commands"
)

func main() {
rootCmd := &cobra.Command{
Use: "time-trace",
Version: timetrace.StringVersion(),
}

commands.RunCommand(rootCmd)

err := rootCmd.Execute()
if err != nil {
panic(err)
}
}
27 changes: 11 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ package config
import (
"bytes"
_ "embed"
"errors"
"os"

tte "github.com/zurvan-lab/TimeTrace/utils/errors"
"gopkg.in/yaml.v2"
)

//go:embed config.yaml
var configBytes []byte

var (
ErrInvalidUserLength = errors.New("invalid user length")
ErrSpecificAndAllCommandSameAtTime = 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 @@ -42,7 +37,7 @@ type User struct {

func (conf *Config) BasicCheck() error {
if len(conf.Users) == 0 {
return ErrInvalidUserLength
return tte.ErrInvalidUsers
}

for _, u := range conf.Users {
Expand All @@ -55,7 +50,7 @@ func (conf *Config) BasicCheck() error {
}

if allCmds && len(u.Cmds) > 1 {
return ErrSpecificAndAllCommandSameAtTime
return tte.ErrSpecificAndAllCommandSameAtTime
}
}

Expand Down Expand Up @@ -85,10 +80,10 @@ func DefaultConfig() *Config {
return config
}

func LoadFromFile(path string) *Config {
func LoadFromFile(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
panic(err)
return nil, err
}
defer file.Close()

Expand All @@ -98,25 +93,25 @@ func LoadFromFile(path string) *Config {

err = decoder.Decode(&config)
if err != nil {
panic(err)
return nil, err
}

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

return config
return config, nil
}

func (conf *Config) ToYAML() []byte {
func (conf *Config) ToYAML() ([]byte, error) {
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)

err := encoder.Encode(conf)
if err != nil {
panic(err)
return nil, err
}

return buf.Bytes()
return buf.Bytes(), nil
}
18 changes: 12 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
tte "github.com/zurvan-lab/TimeTrace/utils/errors"
)

func TestDefaultConfig(t *testing.T) {
Expand All @@ -22,21 +23,26 @@ func TestDefaultConfig(t *testing.T) {
}

defaultConf := DefaultConfig()
defaultFunction := string(defaultConf.ToYAML())
defaultFunction, err := defaultConf.ToYAML()
assert.NoError(t, err)

defaultFunctionStr := string(defaultFunction)

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")
defaultFunctionStr = strings.ReplaceAll(defaultFunctionStr, "\n\n", "\n")

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

func TestLoadFromFile(t *testing.T) {
config := LoadFromFile("./config.yaml")
config, err := LoadFromFile("./config.yaml")
assert.NoError(t, err)

dConfig := DefaultConfig()

assert.Equal(t, "time_trace", config.Name)
Expand All @@ -52,10 +58,10 @@ func TestBasicCheck(t *testing.T) {

c.Users = []User{}
err = c.BasicCheck()
assert.Error(t, ErrInvalidUserLength, err)
assert.Error(t, tte.ErrInvalidUsers, err)

c.Users = []User{DefaultConfig().Users[0]}
c.Users[0].Cmds = []string{"*", "GET"}
err = c.BasicCheck()
assert.Error(t, ErrSpecificAndAllCommandSameAtTime, err)
assert.Error(t, tte.ErrSpecificAndAllCommandSameAtTime, err)
}
3 changes: 2 additions & 1 deletion core/TQL/execute/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zurvan-lab/TimeTrace/config"
"github.com/zurvan-lab/TimeTrace/core/TQL/parser"
"github.com/zurvan-lab/TimeTrace/core/database"
)

func TestExecute(t *testing.T) {
db := database.Init("../../../config/config.yaml")
db := database.Init(config.DefaultConfig())

q := core.ParseQuery("SET testSet")
eResult := Execute(q, db)
Expand Down
4 changes: 2 additions & 2 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type Database struct {
Config *config.Config
}

func Init(path string) IDataBase {
func Init(cfg *config.Config) IDataBase {
return &Database{
Sets: make(Sets, 1024),
Config: config.LoadFromFile(path),
Config: cfg,
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/zurvan-lab/TimeTrace/config"
)

func TestDataBase(t *testing.T) {
db := Init("../../config/config.yaml")
db := Init(config.DefaultConfig())

t.Run("addSetTest", func(t *testing.T) {
result := db.AddSet([]string{"testSet"})
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand All @@ -13,6 +16,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Empty file removed utils/.keep
Empty file.
9 changes: 9 additions & 0 deletions utils/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package errors

import "errors"

var (
ErrInavlidConfigPath = errors.New("invalid config path")
ErrInvalidUsers = errors.New("invalid user(s)")
ErrSpecificAndAllCommandSameAtTime = errors.New("can't have all cmds and specific cmd at same time")
)
4 changes: 0 additions & 4 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ var version = Version{
Meta: "beta",
}

func Agent() string {
return fmt.Sprintf("timetrace/%s", StringVersion())
}

func StringVersion() string {
v := fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch)

Expand Down