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

Commit

Permalink
fix: add todos
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jul 9, 2024
1 parent 67731de commit 97f8856
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ linters:
- staticcheck
- unused
- gocognit
- godox
- godot
- gocritic
- wsl
Expand Down
61 changes: 21 additions & 40 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,19 @@ import (
"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"
)

var (
//go:embed ttrace.txt
welcomeASCII []byte

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()
)
/*
TODO: Add history support for me.
TODO: Add suggestion for me.
TODO: Add completer for me.
*/

func init() {
clear = make(map[string]func())
Expand All @@ -44,6 +34,18 @@ func init() {
}
}

var (
//go:embed ttrace.txt
welcomeASCII []byte

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

clear map[string]func()
)

const (
PROMPT = ">> "
)
Expand All @@ -70,12 +72,6 @@ func ConnectCommand(parentCmd *cobra.Command) {
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)
Expand All @@ -90,20 +86,17 @@ func ConnectCommand(parentCmd *cobra.Command) {
os.Exit(0)
}

if input == "clean" {
cleanTerminal()
}

lnr.AppendHistory(input)
cmd.Print(fmt.Sprintf("%s\n", 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 @@ -130,18 +123,6 @@ 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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/commands/ttrace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ 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.
You can clean the screen using clean command.
Use exit to exit the REPL gracefully.

0 comments on commit 97f8856

Please sign in to comment.