diff --git a/cmd/commands/repl.go b/cmd/commands/repl.go index 2dbcb79..767e478 100644 --- a/cmd/commands/repl.go +++ b/cmd/commands/repl.go @@ -2,7 +2,6 @@ package commands import ( "bufio" - "errors" "fmt" "net" "os" @@ -10,6 +9,7 @@ import ( "time" cobra "github.com/spf13/cobra" + "github.com/zurvan-lab/TimeTrace/utils/errors" ) const PROMPT = "\n>> " @@ -51,15 +51,20 @@ func REPLCommand(parentCmd *cobra.Command) { cmd.Print(do(conn, input)) } } else { - Dead(cmd, errors.New(response)) //nolint + Dead(cmd, fmt.Errorf("%w: %s", errors.ErrInvalidCommand, response)) } } } func do(conn net.Conn, q string) string { resBuf := make([]byte, 1024) + query := []byte(q) - _, err := conn.Write([]byte(q)) + if len(query) < 1 { + return "INVALID" + } + + _, err := conn.Write(query) if err != nil { return err.Error() } diff --git a/core/TQL/parser/parser.go b/core/TQL/parser/parser.go index 5cfd99f..62acebc 100644 --- a/core/TQL/parser/parser.go +++ b/core/TQL/parser/parser.go @@ -8,20 +8,10 @@ import ( // parsing TQL queries. see: docs/TQL. func ParseQuery(query string) database.Query { - command := "" - args := []string{} + q := strings.Split(query, " ") - for _, word := range strings.Split(query, " ") { - if word == "" { - continue - } - - if command != "" { - args = append(args, word) - } else { - command = word - } + return database.Query{ + Command: q[0], + Args: q[1:], } - - return database.Query{Command: command, Args: args} } diff --git a/utils/errors/errors.go b/utils/errors/errors.go index 520df83..39e955b 100644 --- a/utils/errors/errors.go +++ b/utils/errors/errors.go @@ -13,4 +13,5 @@ var ( // CLI. ErrInvalidUserOrPassword = errors.New("user or user information you provided is invalid") + ErrInvalidCommand = errors.New("invalid command") )