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

Commit

Permalink
feat(cli): navigate option for repl command
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jul 9, 2024
1 parent 28c5448 commit 67731de
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## TimeTrace

[![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/EvYB9ZgYvV)
[![Go](https://img.shields.io/badge/--00ADD8?logo=go&logoColor=ffffff)](https://golang.org/)

Time-trace is a software which allow you to store and stream your service logs, events and ..., in real-time.
When you are using time-trace you are using a completely new data structure and model which is strongly mixed with time.
Expand Down
96 changes: 83 additions & 13 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
package commands

import (
"bufio"
_ "embed"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/peterh/liner"
cobra "github.com/spf13/cobra"
"github.com/zurvan-lab/TimeTrace/utils/errors"
)

const PROMPT = "\n>> "
var (
//go:embed ttrace.txt
welcomeASCII []byte

func REPLCommand(parentCmd *cobra.Command) {
history = filepath.Join(os.TempDir(), ".time_trace_repl_history")

TQL_COMMANDS = [...]string{
"CON", "PING", "SET", "SSET", "PUSH", "GET", "CNTS",
"CNTSS", "CNTE", "CLN", "CLNS", "CLNSS", "DRPS", "DRPSS", "SS",
}

clear map[string]func()
)

func init() {
clear = make(map[string]func())
clear["linux"] = func() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
_ = cmd.Run()
}
clear["windows"] = func() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
_ = cmd.Run()
}
}

const (
PROMPT = ">> "
)

func ConnectCommand(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.",
Expand All @@ -32,27 +66,44 @@ func REPLCommand(parentCmd *cobra.Command) {
}
defer conn.Close()

conQuery := fmt.Sprintf("CON %v %v", *username, *password)
lnr := liner.NewLiner()
defer lnr.Close()

lnr.SetCtrlCAborts(true)
lnr.SetCompleter(completer)

if f, err := os.Open(history); err == nil {
_, _ = lnr.ReadHistory(f)
f.Close()
}

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

if response == "OK" {
reader := bufio.NewReader(os.Stdin)
cleanTerminal()
cmd.Println(string(welcomeASCII))

for {
fmt.Print(PROMPT)

input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")
if input, _ := lnr.Prompt(PROMPT); err == nil {
if input == "exit" {
os.Exit(0)
}

if input == "exit" {
break
lnr.AppendHistory(input)
cmd.Print(fmt.Sprintf("%s\n", do(conn, input)))
}

cmd.Print(do(conn, input))
}
} else {
ExitOnError(cmd, fmt.Errorf("%w: %s", errors.ErrInvalidCommand, response))
}

if f, err := os.Create(history); err != nil {
cmd.Printf("Error writing history file: %s\n", err)
} else {
_, _ = lnr.WriteHistory(f)
f.Close()
}
}
}

Expand All @@ -78,3 +129,22 @@ func do(conn net.Conn, q string) string {

return string(resBuf[:n])
}

func completer(line string) []string {
r := make([]string, 15)

for _, c := range TQL_COMMANDS {
if strings.Contains(c, line) {
r = append(r, c)
}
}

return r
}

func cleanTerminal() {
cf, ok := clear[runtime.GOOS]
if ok {
cf()
}
}
10 changes: 10 additions & 0 deletions cmd/commands/ttrace.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
888888 888888 88""Yb db dP""b8 888888
88 88 88__dP dPYb dP `" 88__
88 88 88"Yb dP__Yb Yb 88""
88 88 88 Yb dP""""Yb YboodP 888888

Welcome to time-trace REPL!
You can execute TQL command and apply changes to connected instance here.
see: https://github.com/zurvan-lab/TimeTrace/blob/main/doc/TQL/TQL.md

use exit to exit the repl gracefully.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
}

commands.RunCommand(rootCmd)
commands.REPLCommand(rootCmd)
commands.ConnectCommand(rootCmd)
commands.PingCommand(rootCmd)
commands.InitCommand(rootCmd)

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/zurvan-lab/TimeTrace
go 1.22.2

require (
github.com/peterh/liner v1.2.2
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
Expand All @@ -14,6 +15,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -24,6 +28,7 @@ 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.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-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 67731de

Please sign in to comment.