diff --git a/cmd/commands/run.go b/cmd/commands/run.go new file mode 100644 index 0000000..991199b --- /dev/null +++ b/cmd/commands/run.go @@ -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) + } +} diff --git a/cmd/commands/util.go b/cmd/commands/util.go new file mode 100644 index 0000000..2caf39c --- /dev/null +++ b/cmd/commands/util.go @@ -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) +} diff --git a/cmd/main.go b/cmd/main.go index 06ab7d0..548afff 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) + } +} diff --git a/config/config.go b/config/config.go index 89420dd..2936623 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` @@ -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 { @@ -55,7 +50,7 @@ func (conf *Config) BasicCheck() error { } if allCmds && len(u.Cmds) > 1 { - return ErrSpecificAndAllCommandSameAtTime + return tte.ErrSpecificAndAllCommandSameAtTime } } @@ -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() @@ -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 } diff --git a/config/config_test.go b/config/config_test.go index 777258a..e1d2bed 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + tte "github.com/zurvan-lab/TimeTrace/utils/errors" ) func TestDefaultConfig(t *testing.T) { @@ -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) @@ -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) } diff --git a/core/TQL/execute/execute_test.go b/core/TQL/execute/execute_test.go index 1a04d5d..c748250 100644 --- a/core/TQL/execute/execute_test.go +++ b/core/TQL/execute/execute_test.go @@ -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) diff --git a/core/database/database.go b/core/database/database.go index c0d7e4e..a9fd848 100644 --- a/core/database/database.go +++ b/core/database/database.go @@ -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, } } diff --git a/core/database/database_test.go b/core/database/database_test.go index 8d160ea..ff8246c 100644 --- a/core/database/database_test.go +++ b/core/database/database_test.go @@ -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"}) diff --git a/go.mod b/go.mod index 12bc2a3..c7ef3ca 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 275bb04..42b272d 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/utils/.keep b/utils/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/utils/errors/errors.go b/utils/errors/errors.go new file mode 100644 index 0000000..39e7c92 --- /dev/null +++ b/utils/errors/errors.go @@ -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") +) diff --git a/version.go b/version.go index f638175..c44ac3b 100644 --- a/version.go +++ b/version.go @@ -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)