diff --git a/gosuv.go b/gosuv.go index 7781517..2d72769 100644 --- a/gosuv.go +++ b/gosuv.go @@ -34,7 +34,7 @@ func MkdirIfNoExists(dir string) error { func wrapAction(f func(*cli.Context)) func(*cli.Context) { return func(c *cli.Context) { - // check if serer alive + // check if server alive _, err := goreq.Request{ Method: "GET", Uri: buildURI(c, "/api/version"), @@ -47,6 +47,30 @@ func wrapAction(f func(*cli.Context)) func(*cli.Context) { } } +func wrapPbProgramAction(f func(*cli.Context, pb.ProgramClient)) func(*cli.Context) { + return func(ctx *cli.Context) { + conn, err := connect(ctx) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + client := pb.NewProgramClient(conn) + f(ctx, client) + } +} + +func wrapPbServerAction(f func(*cli.Context, pb.GoSuvClient)) func(*cli.Context) { + return func(ctx *cli.Context) { + conn, err := connect(ctx) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + client := pb.NewGoSuvClient(conn) + f(ctx, client) + } +} + func ServAction(ctx *cli.Context) { host := ctx.GlobalString("host") port := ctx.GlobalInt("port") @@ -191,18 +215,13 @@ func ShutdownAction(ctx *cli.Context) { log.Println("Return code:", res.GetCode()) } -func VersionAction(ctx *cli.Context) { +func VersionAction(ctx *cli.Context, client pb.GoSuvClient) { fmt.Printf("Client: %s\n", GOSUV_VERSION) - res, err := goreq.Request{ - Method: "GET", - Uri: buildURI(ctx, "/api/version"), - }.Do() + res, err := client.Version(context.Background(), &pb.NopRequest{}) if err != nil { - panic(err) + log.Fatal(err) } - var reply JSONResponse - res.Body.FromJsonTo(&reply) - fmt.Printf("Server: %s\n", reply.Message) + fmt.Printf("Server: %s\n", res.GetMessage()) } var app *cli.App @@ -232,7 +251,7 @@ func init() { { Name: "version", Usage: "Show version", - Action: wrapAction(VersionAction), + Action: wrapPbServerAction(VersionAction), }, { Name: "status", diff --git a/gosuvpb/gosuv.pb.go b/gosuvpb/gosuv.pb.go index 0966211..5def88f 100644 --- a/gosuvpb/gosuv.pb.go +++ b/gosuvpb/gosuv.pb.go @@ -128,6 +128,7 @@ var _ grpc.ClientConn type GoSuvClient interface { Control(ctx context.Context, in *CtrlRequest, opts ...grpc.CallOption) (*CtrlResponse, error) Shutdown(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*Response, error) + Version(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*Response, error) } type goSuvClient struct { @@ -156,11 +157,21 @@ func (c *goSuvClient) Shutdown(ctx context.Context, in *NopRequest, opts ...grpc return out, nil } +func (c *goSuvClient) Version(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*Response, error) { + out := new(Response) + err := grpc.Invoke(ctx, "/gosuvpb.GoSuv/Version", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // Server API for GoSuv service type GoSuvServer interface { Control(context.Context, *CtrlRequest) (*CtrlResponse, error) Shutdown(context.Context, *NopRequest) (*Response, error) + Version(context.Context, *NopRequest) (*Response, error) } func RegisterGoSuvServer(s *grpc.Server, srv GoSuvServer) { @@ -191,6 +202,18 @@ func _GoSuv_Shutdown_Handler(srv interface{}, ctx context.Context, codec grpc.Co return out, nil } +func _GoSuv_Version_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) { + in := new(NopRequest) + if err := codec.Unmarshal(buf, in); err != nil { + return nil, err + } + out, err := srv.(GoSuvServer).Version(ctx, in) + if err != nil { + return nil, err + } + return out, nil +} + var _GoSuv_serviceDesc = grpc.ServiceDesc{ ServiceName: "gosuvpb.GoSuv", HandlerType: (*GoSuvServer)(nil), @@ -203,6 +226,10 @@ var _GoSuv_serviceDesc = grpc.ServiceDesc{ MethodName: "Shutdown", Handler: _GoSuv_Shutdown_Handler, }, + { + MethodName: "Version", + Handler: _GoSuv_Version_Handler, + }, }, Streams: []grpc.StreamDesc{}, } diff --git a/gosuvpb/gosuv.proto b/gosuvpb/gosuv.proto index 2796255..2a8721b 100644 --- a/gosuvpb/gosuv.proto +++ b/gosuvpb/gosuv.proto @@ -24,6 +24,7 @@ message Request { service GoSuv { rpc Control(CtrlRequest) returns (CtrlResponse) {}; rpc Shutdown(NopRequest) returns (Response) {}; + rpc Version(NopRequest) returns (Response) {}; } service Program { diff --git a/service.go b/service.go index c449974..1712dcd 100644 --- a/service.go +++ b/service.go @@ -55,3 +55,10 @@ func (s *PbSuvServer) Shutdown(ctx context.Context, in *pb.NopRequest) (*pb.Resp res.Code = proto.Int32(200) return res, nil } + +func (s *PbSuvServer) Version(ctx context.Context, in *pb.NopRequest) (res *pb.Response, err error) { + res = &pb.Response{ + Message: proto.String(GOSUV_VERSION), + } + return +}