This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding REPL to command line tool (#97)
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ Thumbs.db | |
cmd/config.yaml | ||
/client | ||
log.ttrace | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,10 @@ devtools: | |
go install github.com/golangci/golangci-lint/cmd/[email protected] | ||
go install mvdan.cc/gofumpt@latest | ||
|
||
## Building | ||
build-cli: | ||
go build -o ./build/ttrace ./cmd/main.go | ||
|
||
### Testing | ||
unit_test: | ||
go test $(PACKAGES) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters