ricochet-go/cli/cli.go

87 lines
1.7 KiB
Go
Raw Normal View History

2016-08-02 02:58:10 +00:00
package main
import (
"fmt"
rpc "github.com/special/notricochet/rpc"
2016-08-02 02:58:10 +00:00
"golang.org/x/net/context"
"google.golang.org/grpc"
"gopkg.in/readline.v1"
2016-08-02 02:58:10 +00:00
"log"
"strings"
2016-08-02 02:58:10 +00:00
)
const (
defaultAddress = "127.0.0.1:58281"
)
func main() {
conn, err := grpc.Dial(defaultAddress, grpc.WithInsecure())
2016-08-02 23:28:26 +00:00
if err != nil {
log.Fatalf("connection failed: %v", err)
2016-08-02 23:28:26 +00:00
}
defer conn.Close()
2016-08-02 23:28:26 +00:00
input, err := readline.NewEx(&readline.Config{})
if err != nil {
log.Fatal(err)
}
defer input.Close()
log.SetOutput(input.Stdout())
c := &Client{
Backend: rpc.NewRicochetCoreClient(conn),
Input: input,
}
if err := c.Initialize(); err != nil {
log.Fatal(err)
}
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)
2016-09-16 21:26:14 +00:00
case "status":
log.Printf("server: %v", c.ServerStatus)
log.Printf("identity: %v", c.Identity)
case "connect":
status, err := c.Backend.StartNetwork(context.Background(), &rpc.StartNetworkRequest{})
if err != nil {
log.Printf("start network error: %v", err)
} else {
log.Printf("network started: %v", status)
}
2016-09-16 21:26:14 +00:00
case "disconnect":
status, err := c.Backend.StopNetwork(context.Background(), &rpc.StopNetworkRequest{})
if err != nil {
log.Printf("stop network error: %v", err)
} else {
log.Printf("network stopped: %v", status)
}
2016-09-16 21:26:14 +00:00
2016-08-30 02:46:41 +00:00
case "contacts":
2016-09-16 21:26:14 +00:00
for _, contact := range c.Contacts {
log.Printf(" %s (%s)", contact.Nickname, contact.Status.String())
2016-08-30 02:46:41 +00:00
}
2016-09-16 21:26:14 +00:00
case "help":
fallthrough
2016-09-16 21:26:14 +00:00
default:
2016-09-16 21:26:14 +00:00
fmt.Println("Commands: clear, status, connect, disconnect, contacts, help")
}
}
2016-08-02 02:58:10 +00:00
}