diff --git a/cmd/commands/ping.go b/cmd/commands/ping.go index beb6a83..a09c1a9 100644 --- a/cmd/commands/ping.go +++ b/cmd/commands/ping.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "os" + "time" cobra "github.com/spf13/cobra" ) @@ -18,6 +19,7 @@ func PingCommand(parentCmd *cobra.Command) { address := ping.Flags().StringP("address", "a", "localhost:7070", "remote address of your time trace instance.") username := ping.Flags().StringP("username", "u", "root", "username of the user you are going to connect with.") password := ping.Flags().StringP("password", "p", "", "password of user trying to connect with.") + verbose := ping.Flags().BoolP("verbose", "v", false, "verbose mode.") ping.Run = func(cmd *cobra.Command, args []string) { conn, err := net.Dial("tcp", *address) @@ -30,12 +32,20 @@ func PingCommand(parentCmd *cobra.Command) { do(conn, conQuery) + details := "" + + t := time.Now() response := do(conn, "PING") + + if *verbose { + details = fmt.Sprintf("It toked %v to get the response", time.Since(t).Abs().String()) + } + if response == "PONG" { - cmd.Println("PONG, everything is ok.") + cmd.Printf("PONG, everything is ok.\n%s\n", details) os.Exit(0) } else { - ExitOnError(cmd, fmt.Errorf("something went wrong: %v", response)) + ExitOnError(cmd, fmt.Errorf("something went wrong: %v\n%s", response, details)) } } } diff --git a/cmd/commands/repl.go b/cmd/commands/repl.go index a7676a5..f4da40b 100644 --- a/cmd/commands/repl.go +++ b/cmd/commands/repl.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "runtime" - "time" "github.com/peterh/liner" cobra "github.com/spf13/cobra" @@ -100,29 +99,6 @@ func ConnectCommand(parentCmd *cobra.Command) { } } -func do(conn net.Conn, q string) string { - resBuf := make([]byte, 1024) - query := []byte(q) - - if len(query) < 1 { - return "INVALID" - } - - _, err := conn.Write(query) - 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]) -} - func cleanTerminal() { cf, ok := clear[runtime.GOOS] if ok { diff --git a/cmd/commands/util.go b/cmd/commands/util.go index 2f2ec1a..c3352c8 100644 --- a/cmd/commands/util.go +++ b/cmd/commands/util.go @@ -1,6 +1,7 @@ package commands import ( + "net" "os" "github.com/spf13/cobra" @@ -10,3 +11,25 @@ func ExitOnError(cmd *cobra.Command, err error) { cmd.PrintErrln(err) os.Exit(1) } + +func do(conn net.Conn, q string) string { + resBuf := make([]byte, 1024) + + query := []byte(q) + + if len(query) < 1 { + return "INVALID" + } + + _, err := conn.Write(query) + if err != nil { + return err.Error() + } + + n, err := conn.Read(resBuf) + if err != nil { + return err.Error() + } + + return string(resBuf[:n]) +}