cli: Minor output cleanup and contacts command

This commit is contained in:
John Brooks 2016-09-29 22:17:03 -07:00
parent f56204116c
commit e1db24360b
1 changed files with 31 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
"gopkg.in/readline.v1" "gopkg.in/readline.v1"
"log" "log"
"os"
"strings" "strings"
) )
@ -17,13 +18,15 @@ const (
func main() { func main() {
conn, err := grpc.Dial(defaultAddress, grpc.WithInsecure()) conn, err := grpc.Dial(defaultAddress, grpc.WithInsecure())
if err != nil { if err != nil {
log.Fatalf("connection failed: %v", err) fmt.Printf("connection failed: %v\n", err)
os.Exit(1)
} }
defer conn.Close() defer conn.Close()
input, err := readline.NewEx(&readline.Config{}) input, err := readline.NewEx(&readline.Config{})
if err != nil { if err != nil {
log.Fatal(err) fmt.Println(err)
os.Exit(1)
} }
defer input.Close() defer input.Close()
log.SetOutput(input.Stdout()) log.SetOutput(input.Stdout())
@ -34,7 +37,8 @@ func main() {
} }
if err := c.Initialize(); err != nil { if err := c.Initialize(); err != nil {
log.Fatal(err) fmt.Println(err)
os.Exit(1)
} }
input.SetPrompt("> ") input.SetPrompt("> ")
@ -51,36 +55,52 @@ func main() {
case "clear": case "clear":
readline.ClearScreen(readline.Stdout) readline.ClearScreen(readline.Stdout)
case "quit":
os.Exit(0)
case "status": case "status":
log.Printf("server: %v", c.ServerStatus) fmt.Printf("server: %v\n", c.ServerStatus)
log.Printf("identity: %v", c.Identity) fmt.Printf("identity: %v\n", c.Identity)
case "connect": case "connect":
status, err := c.Backend.StartNetwork(context.Background(), &rpc.StartNetworkRequest{}) status, err := c.Backend.StartNetwork(context.Background(), &rpc.StartNetworkRequest{})
if err != nil { if err != nil {
log.Printf("start network error: %v", err) fmt.Printf("start network error: %v\n", err)
} else { } else {
log.Printf("network started: %v", status) fmt.Printf("network started: %v\n", status)
} }
case "disconnect": case "disconnect":
status, err := c.Backend.StopNetwork(context.Background(), &rpc.StopNetworkRequest{}) status, err := c.Backend.StopNetwork(context.Background(), &rpc.StopNetworkRequest{})
if err != nil { if err != nil {
log.Printf("stop network error: %v", err) fmt.Printf("stop network error: %v\n", err)
} else { } else {
log.Printf("network stopped: %v", status) fmt.Printf("network stopped: %v\n", status)
} }
case "contacts": case "contacts":
byStatus := make(map[rpc.Contact_Status][]*rpc.Contact)
for _, contact := range c.Contacts { for _, contact := range c.Contacts {
log.Printf(" %s (%s)", contact.Nickname, contact.Status.String()) byStatus[contact.Status] = append(byStatus[contact.Status], contact)
}
order := []rpc.Contact_Status{rpc.Contact_ONLINE, rpc.Contact_UNKNOWN, rpc.Contact_OFFLINE, rpc.Contact_REQUEST, rpc.Contact_REJECTED}
for _, status := range order {
contacts := byStatus[status]
if len(contacts) == 0 {
continue
}
fmt.Printf(". %s\n", strings.ToLower(status.String()))
for _, contact := range contacts {
fmt.Printf("... %s\n", contact.Nickname)
}
} }
case "help": case "help":
fallthrough fallthrough
default: default:
fmt.Println("Commands: clear, status, connect, disconnect, contacts, help") fmt.Println("Commands: clear, quit, status, connect, disconnect, contacts, help")
} }
} }
} }