Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a basic CLI component. This closes #12 #13

Merged
merged 1 commit into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*.out

*~
wantslist
wantslist
wantslist_cli/wantslist_cli
92 changes: 92 additions & 0 deletions wantslist_cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"context"
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/brotherlogic/goserver/utils"
"google.golang.org/grpc"

pbgd "github.com/brotherlogic/godiscogs"
pbgs "github.com/brotherlogic/goserver/proto"
pbrc "github.com/brotherlogic/recordcollection/proto"
pbt "github.com/brotherlogic/tracer/proto"
pb "github.com/brotherlogic/wantslist/proto"

//Needed to pull in gzip encoding init
_ "google.golang.org/grpc/encoding/gzip"
)

func getRecordRep(ctx context.Context, id int32) string {
host, port, err := utils.Resolve("recordcollection")
if err != nil {
log.Fatalf("Unable to reach collection: %v", err)
}
conn, err := grpc.Dial(host+":"+strconv.Itoa(int(port)), grpc.WithInsecure())
defer conn.Close()

if err != nil {
log.Fatalf("Unable to dial: %v", err)
}

client := pbrc.NewRecordCollectionServiceClient(conn)
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
rel, err := client.GetRecords(ctx, &pbrc.GetRecordsRequest{Filter: &pbrc.Record{Release: &pbgd.Release{Id: id}}})
if err != nil || len(rel.GetRecords()) == 0 {
return ""
}
return fmt.Sprintf("%v", rel.GetRecords()[0].GetRelease().Title)
}

func main() {
host, port, err := utils.Resolve("wantslist")
if err != nil {
log.Fatalf("Unable to reach organiser: %v", err)
}
conn, err := grpc.Dial(host+":"+strconv.Itoa(int(port)), grpc.WithInsecure())
defer conn.Close()

if err != nil {
log.Fatalf("Unable to dial: %v", err)
}

client := pb.NewWantServiceClient(conn)
ctx, cancel := utils.BuildContext("wantslist-cli", "wantslist", pbgs.ContextType_LONG)
defer cancel()

switch os.Args[1] {
case "add":
list := &pb.WantList{Name: os.Args[2], Wants: []*pb.WantListEntry{}}
for i, v := range os.Args[3:] {
val, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("Cannot parse %v", v)
}
list.Wants = append(list.Wants, &pb.WantListEntry{Index: int32(i + 1), Want: int32(val)})
}

_, err := client.AddWantList(ctx, &pb.AddWantListRequest{Add: list})
if err != nil {
log.Fatalf("Error adding wantlist: %v", err)
}
case "get":
lists, err := client.GetWantList(ctx, &pb.GetWantListRequest{})
if err != nil {
log.Fatalf("Error getting wantlists: %v", err)
}

for i, list := range lists.Lists {
fmt.Printf("List %v. %v\n", (i + 1), list.Name)
for _, entry := range list.Wants {
fmt.Printf(" %v. %v (%v)\n", entry.Index, getRecordRep(ctx, entry.Want), entry.Want)
}
}
}

utils.SendTrace(ctx, "End of CLI", time.Now(), pbt.Milestone_END, "recordwants-cli")
}