-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from brotherlogic/add_cli
Adds a basic CLI component. This closes #12
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
*.out | ||
|
||
*~ | ||
wantslist | ||
wantslist | ||
wantslist_cli/wantslist_cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |