Skip to content

Commit

Permalink
feat: adding REPL to command line tool (zurvan-lab#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Dec 27, 2023
1 parent debc9ee commit e119892
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Thumbs.db
cmd/config.yaml
/client
log.ttrace
build/
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ devtools:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.1
go install mvdan.cc/gofumpt@latest

## Building
build-cli:
go build -o ./build/ttrace ./cmd/main.go

### Testing
unit_test:
go test $(PACKAGES)
Expand Down
75 changes: 75 additions & 0 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package commands

import (
"bufio"
"errors"
"fmt"
"net"
"os"
"strings"
"time"

cobra "github.com/spf13/cobra"
)

const PROMPT = "\n>> "

func REPLCommand(parentCmd *cobra.Command) {
connect := &cobra.Command{
Use: "connect",
Short: "Connects you to a time trace instance and you can interact with it in a REPL interface.",
}
parentCmd.AddCommand(connect)

address := connect.Flags().StringP("address", "a", "localhost:7070", "remote address of your time trace instance.")
username := connect.Flags().StringP("username", "u", "root", "username of the user you are going to connect with.")
password := connect.Flags().StringP("password", "p", "", "password of user trying to connect with.")

connect.Run = func(cmd *cobra.Command, args []string) {
conn, err := net.Dial("tcp", *address)
if err != nil {
dead(cmd, err)
}
defer conn.Close()

conQuery := fmt.Sprintf("CON %v %v", *username, *password)

response := do(conn, conQuery)
if response == "DONE" {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Print(PROMPT)

input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")

if input == "exit" {
break
}

cmd.Print(do(conn, input))
}
} else {
dead(cmd, errors.New(response)) //nolint
}
}
}

func do(conn net.Conn, q string) string {
resBuf := make([]byte, 1024)

_, err := conn.Write([]byte(q))
if err != nil {
return err.Error()
}

time.Sleep(time.Second * 1)

n, err := conn.Read(resBuf)
if err != nil {
return err.Error()
}

return string(resBuf[:n])
}
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

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

commands.RunCommand(rootCmd)
commands.REPLCommand(rootCmd)

err := rootCmd.Execute()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions utils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ var (

// server.
ErrAuth = errors.New("authentication error")

// CLI.
ErrInvalidUserOrPassword = errors.New("user or user information you provided is invalid")
)

0 comments on commit e119892

Please sign in to comment.