tail add -n and -f

master
shengxiang 9 years ago
parent 0a2f8f2197
commit ba6326cdc0

@ -24,7 +24,7 @@ Go version at least `1.4`
$ gosuv stop timetest
program "timetest" stopped
$ gosuv tail timetest
$ gosuv tail -n 2 -f timetest
line 1
line 2
line ...

@ -166,7 +166,11 @@ func StopAction(ctx *cli.Context) {
}
func TailAction(ctx *cli.Context, client pb.ProgramClient) {
req := &pb.Request{Name: ctx.Args().First()}
req := &pb.TailRequest{
Name: ctx.Args().First(),
Number: int32(ctx.Int("number")),
Follow: ctx.Bool("follow"),
}
tailc, err := client.Tail(context.Background(), req)
if err != nil {
log.Fatal(err)
@ -300,6 +304,17 @@ func initCli() {
Name: "tail",
Usage: "tail log",
Action: wrap(TailAction),
Flags: []cli.Flag{
cli.IntFlag{
Name: "number, n",
Value: 10,
Usage: "The location is number lines.",
},
cli.BoolFlag{
Name: "follow, f",
Usage: "Constantly show log",
},
},
},
{
Name: "shutdown",

@ -12,6 +12,7 @@ It has these top-level messages:
NopRequest
Response
Request
TailRequest
ProgramInfo
ProgramStatus
StatusResponse
@ -57,6 +58,16 @@ func (m *Request) Reset() { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage() {}
type TailRequest struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
Number int32 `protobuf:"varint,2,opt,name=number" json:"number,omitempty"`
Follow bool `protobuf:"varint,3,opt,name=follow" json:"follow,omitempty"`
}
func (m *TailRequest) Reset() { *m = TailRequest{} }
func (m *TailRequest) String() string { return proto.CompactTextString(m) }
func (*TailRequest) ProtoMessage() {}
type ProgramInfo struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
Command string `protobuf:"bytes,2,opt,name=command" json:"command,omitempty"`
@ -220,7 +231,7 @@ var _GoSuv_serviceDesc = grpc.ServiceDesc{
type ProgramClient interface {
Start(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
Stop(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
Tail(ctx context.Context, in *Request, opts ...grpc.CallOption) (Program_TailClient, error)
Tail(ctx context.Context, in *TailRequest, opts ...grpc.CallOption) (Program_TailClient, error)
}
type programClient struct {
@ -249,7 +260,7 @@ func (c *programClient) Stop(ctx context.Context, in *Request, opts ...grpc.Call
return out, nil
}
func (c *programClient) Tail(ctx context.Context, in *Request, opts ...grpc.CallOption) (Program_TailClient, error) {
func (c *programClient) Tail(ctx context.Context, in *TailRequest, opts ...grpc.CallOption) (Program_TailClient, error) {
stream, err := grpc.NewClientStream(ctx, &_Program_serviceDesc.Streams[0], c.cc, "/gosuvpb.Program/Tail", opts...)
if err != nil {
return nil, err
@ -286,7 +297,7 @@ func (x *programTailClient) Recv() (*LogLine, error) {
type ProgramServer interface {
Start(context.Context, *Request) (*Response, error)
Stop(context.Context, *Request) (*Response, error)
Tail(*Request, Program_TailServer) error
Tail(*TailRequest, Program_TailServer) error
}
func RegisterProgramServer(s *grpc.Server, srv ProgramServer) {
@ -318,7 +329,7 @@ func _Program_Stop_Handler(srv interface{}, ctx context.Context, codec grpc.Code
}
func _Program_Tail_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(Request)
m := new(TailRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}

@ -13,6 +13,11 @@ message Request {
string name = 1;
}
message TailRequest {
string name = 1;
int32 number = 2; // the location of number lines
bool follow = 3;
}
message ProgramInfo {
string name = 1;
string command = 2;
@ -42,5 +47,5 @@ service GoSuv {
service Program {
rpc Start(Request) returns (Response) {}
rpc Stop(Request) returns (Response) {}
rpc Tail(Request) returns (stream LogLine) {}
rpc Tail(TailRequest) returns (stream LogLine) {}
}

@ -1,6 +1,7 @@
package main
import (
"fmt"
"net"
"os"
"os/exec"
@ -36,12 +37,16 @@ func (this *PbProgram) Stop(ctx context.Context, in *pb.Request) (res *pb.Respon
}
//func (this *PbProgram) Tail(ctx context.Context, in *pb.Request)(stream
func (c *PbProgram) Tail(in *pb.Request, stream pb.Program_TailServer) (err error) {
func (c *PbProgram) Tail(in *pb.TailRequest, stream pb.Program_TailServer) (err error) {
program, err := programTable.Get(in.Name)
if err != nil {
return
}
cmd := exec.Command("tail", "-n5", "-f", program.logFilePath())
args := []string{"-n", fmt.Sprintf("%d", in.Number)}
if in.Follow {
args = append(args, "-f")
}
cmd := exec.Command("tail", append(args, program.logFilePath())...)
rd, err := cmd.StdoutPipe()
go cmd.Run()
defer func() {

Loading…
Cancel
Save