Skip to content

Commit

Permalink
feat: print out logo and server version on cli startup
Browse files Browse the repository at this point in the history
  • Loading branch information
HotPotatoC committed Apr 14, 2021
1 parent 5998dde commit 3b4b8e4
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -11,8 +12,10 @@ import (
"syscall"

"github.com/HotPotatoC/kvstore/command"
"github.com/HotPotatoC/kvstore/packet"
"github.com/HotPotatoC/kvstore/pkg/comm"
"github.com/HotPotatoC/kvstore/pkg/utils"
"github.com/HotPotatoC/kvstore/server/stats"
)

// CLI represents the cli client
Expand All @@ -37,6 +40,19 @@ func New(addr string) *CLI {
// Start runs the CLI client
func (c *CLI) Start() {
go func() {
// Get server information on initial startup
stats := c.getServerInformation()

logo := "" +
" _ _ _ _\n" +
"| | | | | (_)\n" +
"| | ____ _____| |_ ___ _ __ ___ ______ ___| |_\n" +
"| |/ /\\ \\ / / __| __/ _ \\| '__/ _ \\______/ __| | |\n" +
"| < \\ V /\\__ \\ || (_) | | | __/ | (__| | |\n" +
"|_|\\_\\ \\_/ |___/\\__\\___/|_| \\___| \\___|_|_|\n\n"

fmt.Println(logo)
fmt.Printf("Connected to kvstore %s:%s server!\n\n", stats.Version, stats.Build)
start:
for {
fmt.Printf("%s> ", c.comm.Connection().RemoteAddr().String())
Expand Down Expand Up @@ -105,3 +121,29 @@ func (c *CLI) Start() {
c.comm.Connection().Close()
os.Exit(0)
}

func (c *CLI) getServerInformation() *stats.Stats {
serverStats := new(stats.Stats)
infoPacket := packet.NewPacket(command.INFO, []byte(""))
infoBuffer, err := infoPacket.Encode()
if err != nil {
log.Fatal(err)
}

err = c.comm.Send(infoBuffer.Bytes())
if err != nil && err != io.EOF {
log.Fatal(err)
}

infoMessage, n, err := c.comm.Read()
if err != nil && err != io.EOF {
log.Fatal(err)
}

err = json.Unmarshal(infoMessage[:n], &serverStats)
if err != nil {
log.Fatal(err)
}

return serverStats
}

0 comments on commit 3b4b8e4

Please sign in to comment.