ricochet-go/cli/cli.go

107 lines
2.3 KiB
Go
Raw Normal View History

2016-08-02 04:58:10 +02:00
package main
import (
"fmt"
rpc "github.com/special/notricochet/rpc"
2016-08-02 04:58:10 +02:00
"golang.org/x/net/context"
"google.golang.org/grpc"
"gopkg.in/readline.v1"
2016-08-02 04:58:10 +02:00
"log"
"os"
"strings"
2016-08-02 04:58:10 +02:00
)
const (
defaultAddress = "127.0.0.1:58281"
)
func main() {
conn, err := grpc.Dial(defaultAddress, grpc.WithInsecure())
2016-08-03 01:28:26 +02:00
if err != nil {
fmt.Printf("connection failed: %v\n", err)
os.Exit(1)
2016-08-03 01:28:26 +02:00
}
defer conn.Close()
2016-08-03 01:28:26 +02:00
input, err := readline.NewEx(&readline.Config{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer input.Close()
log.SetOutput(input.Stdout())
c := &Client{
Backend: rpc.NewRicochetCoreClient(conn),
Input: input,
}
if err := c.Initialize(); err != nil {
fmt.Println(err)
os.Exit(1)
}
input.SetPrompt("> ")
for {
line := input.Line()
if line.CanContinue() {
continue
} else if line.CanBreak() {
break
}
words := strings.SplitN(line.Line, " ", 1)
switch words[0] {
case "clear":
readline.ClearScreen(readline.Stdout)
case "quit":
os.Exit(0)
2016-09-16 23:26:14 +02:00
case "status":
fmt.Printf("server: %v\n", c.ServerStatus)
fmt.Printf("identity: %v\n", c.Identity)
case "connect":
status, err := c.Backend.StartNetwork(context.Background(), &rpc.StartNetworkRequest{})
if err != nil {
fmt.Printf("start network error: %v\n", err)
} else {
fmt.Printf("network started: %v\n", status)
}
2016-09-16 23:26:14 +02:00
case "disconnect":
status, err := c.Backend.StopNetwork(context.Background(), &rpc.StopNetworkRequest{})
if err != nil {
fmt.Printf("stop network error: %v\n", err)
} else {
fmt.Printf("network stopped: %v\n", status)
}
2016-09-16 23:26:14 +02:00
2016-08-30 04:46:41 +02:00
case "contacts":
byStatus := make(map[rpc.Contact_Status][]*rpc.Contact)
2016-09-16 23:26:14 +02:00
for _, contact := range c.Contacts {
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)
}
2016-08-30 04:46:41 +02:00
}
2016-09-16 23:26:14 +02:00
case "help":
fallthrough
2016-09-16 23:26:14 +02:00
default:
fmt.Println("Commands: clear, quit, status, connect, disconnect, contacts, help")
}
}
2016-08-02 04:58:10 +02:00
}