Last active
February 20, 2017 10:30
-
-
Save ru-rocker/8d9b4b9f89bcfaf9cb6d9eaaeb0fafb3 to your computer and use it in GitHub Desktop.
lorem-grpc client command
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
package main | |
import ( | |
"flag" | |
"time" | |
"log" | |
grpcClient "github.com/ru-rocker/gokit-playground/lorem-grpc/client" | |
"google.golang.org/grpc" | |
"golang.org/x/net/context" | |
"github.com/ru-rocker/gokit-playground/lorem-grpc" | |
"fmt" | |
"strconv" | |
) | |
func main() { | |
var ( | |
grpcAddr = flag.String("addr", ":8081", | |
"gRPC address") | |
) | |
flag.Parse() | |
ctx := context.Background() | |
conn, err := grpc.Dial(*grpcAddr, grpc.WithInsecure(), | |
grpc.WithTimeout(1*time.Second)) | |
if err != nil { | |
log.Fatalln("gRPC dial:", err) | |
} | |
defer conn.Close() | |
loremService := grpcClient.New(conn) | |
args := flag.Args() | |
var cmd string | |
cmd, args = pop(args) | |
switch cmd { | |
case "lorem": | |
var requestType, minStr, maxStr string | |
requestType, args = pop(args) | |
minStr, args = pop(args) | |
maxStr, args = pop(args) | |
min, _ := strconv.Atoi(minStr) | |
max, _ := strconv.Atoi(maxStr) | |
lorem(ctx, loremService, requestType, min, max) | |
default: | |
log.Fatalln("unknown command", cmd) | |
} | |
} | |
// parse command line argument one by one | |
func pop(s []string) (string, []string) { | |
if len(s) == 0 { | |
return "", s | |
} | |
return s[0], s[1:] | |
} | |
// call lorem service | |
func lorem(ctx context.Context, service lorem_grpc.Service, requestType string, min int, max int) { | |
mesg, err := service.Lorem(ctx, requestType, min, max) | |
if err != nil { | |
log.Fatalln(err.Error()) | |
} | |
fmt.Println(mesg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment