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

Commit

Permalink
feat(CLI): init command (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jul 9, 2024
1 parent f977919 commit 28c5448
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 12 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ linters:
- containedctx
- contextcheck
- exhaustive
- goerr113
- wastedassign
- nonamedreturns
- nlreturn
Expand Down
39 changes: 39 additions & 0 deletions cmd/commands/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package commands

import (
"fmt"
"os"

cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/config"
)

func InitCommand(parentCmd *cobra.Command) {
init := &cobra.Command{
Use: "init",
Short: "Init makes a default config file for you to run time-trace.",
}
parentCmd.AddCommand(init)

path := init.Flags().StringP("path", "p", "./config.yml", "config file path")

init.Run = func(cmd *cobra.Command, args []string) {
_, err := os.Stat(*path)
if !os.IsNotExist(err) {
ExitOnError(cmd, fmt.Errorf("a config file already exist on %s path", *path))
}

cfg := config.DefaultConfig()

ymlCfg, err := cfg.ToYAML()
if err != nil {
ExitOnError(cmd, err)
}

if err = os.WriteFile(*path, ymlCfg, 0o600); err != nil {
ExitOnError(cmd, err)
}

cmd.Printf("Config created at %s successfully!\n", *path)
}
}
3 changes: 1 addition & 2 deletions cmd/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func PingCommand(parentCmd *cobra.Command) {
cmd.Println("PONG, everything is ok.")
os.Exit(0)
} else {
cmd.Printf("something is wrong: %v", response)
os.Exit(1)
ExitOnError(cmd, fmt.Errorf("something went wrong: %v", response))
}
}
}
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func main() {
commands.RunCommand(rootCmd)
commands.REPLCommand(rootCmd)
commands.PingCommand(rootCmd)
commands.InitCommand(rootCmd)

err := rootCmd.Execute()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion core/TQL/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package core

import (
"github.com/stretchr/testify/assert"
"testing"

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

func TestParseQuery(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions core/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestDataBase(t *testing.T) {

time := time.Now()
timeStr := strconv.Itoa(int(time.Unix()))

for i := 0; i < 50; i++ {
db.PushElement([]string{"testSet", "subSetOne", "testValue", timeStr})
}
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.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=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
8 changes: 4 additions & 4 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import "fmt"

// These constants follow the semantic versioning 2.0.0 spec (http://semver.org/)
type Version struct {
Meta string `json:"meta" xml:"meta"`
Major uint8 `json:"major" xml:"major"`
Minor uint8 `json:"minor" xml:"minor"`
Patch uint8 `json:"patch" xml:"patch"`
Meta string
Major uint8
Minor uint8
Patch uint8
}

var version = Version{
Expand Down

0 comments on commit 28c5448

Please sign in to comment.