예제 #1
0
func (c *pipeRun) Run(a subcommands.Application, args []string) int {
	if len(args) != 0 {
		fmt.Fprintln(os.Stderr, "This tool doesn't accept positional command line arguments")
		return 1
	}
	state, err := c.commonOptions.processFlags()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	err1 := cloudtail.PipeFromReader(os.Stdin, cloudtail.StdParser(), state.buffer, state.logger)
	if err1 != nil {
		fmt.Fprintln(os.Stderr, err1)
	}
	abort := make(chan struct{}, 1)
	catchCtrlC(func() error {
		abort <- struct{}{}
		return nil
	})
	err2 := state.buffer.Stop(abort)
	if err2 != nil {
		fmt.Fprintln(os.Stderr, err2)
	}
	if err1 != nil || err2 != nil {
		return 1
	}
	return 0
}
예제 #2
0
func (c *tailRun) Run(a subcommands.Application, args []string) int {
	if len(args) != 0 {
		fmt.Fprintln(os.Stderr, "This tool doesn't accept positional command line arguments")
		return 1
	}
	if c.path == "" {
		fmt.Fprintln(os.Stderr, "-path is required")
		return 1
	}

	state, err := c.commonOptions.processFlags()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}

	tailer, err := cloudtail.NewTailer(cloudtail.TailerOptions{
		Path:       c.path,
		Parser:     cloudtail.StdParser(),
		PushBuffer: state.buffer,
		Logger:     state.logger,
		SeekToEnd:  true,
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	defer cloudtail.CleanupTailer()
	catchCtrlC(tailer.Stop)

	fail := false
	if err1 := tailer.Wait(); err1 != nil {
		fmt.Fprintln(os.Stderr, err1)
		fail = true
	}
	if err2 := state.buffer.Stop(nil); err2 != nil {
		fmt.Fprintln(os.Stderr, err2)
		fail = true
	}
	if fail {
		return 1
	}
	return 0
}