Exemplo n.º 1
0
func main() {
	var (
		version    bool
		showLine   = true
		startTime  time.Time
		endTime    time.Time
		activeDate time.Time
		err        error
	)

	flag.StringVar(&match, "match", "", "Match text in annotations.")
	flag.StringVar(&start, "start", "", "Start of inclusive date range.")
	flag.StringVar(&end, "end", "", "End of inclusive date range.")
	flag.BoolVar(&asJSON, "json", false, "Output in JSON format.")
	flag.BoolVar(&help, "help", false, "Display this help document.")
	flag.BoolVar(&help, "h", false, "Display this help document.")
	flag.BoolVar(&version, "version", false, "Display version information.")
	flag.BoolVar(&version, "v", false, "Display version information.")
	flag.Parse()
	if help == true {
		usage(0, "")
	}

	if version == true {
		revision()
	}

	activeDate = time.Now()
	if start != "" {
		startTime, err = time.Parse("2006-01-02 15:04:05", start+" 00:00:00")
		if err != nil {
			log.Fatalf("Start date error: %s\n", err)
			os.Exit(1)
		}
		if end == "" {
			endTime = activeDate
		} else {
			endTime, err = time.Parse("2006-01-02 15:04:05", end+" 23:59:59")
			if err != nil {
				log.Fatalf("End date error: %s\n", err)
				os.Exit(1)
			}
		}
	}

	reader := bufio.NewReader(os.Stdin)

	entry := new(stn.Entry)
	lineNo := 0
	for {
		showLine = true
		line, err := reader.ReadString('\n')
		if err != nil {
			break
		}
		lineNo++
		if entry.FromString(line) != true {
			log.Fatalf("line no. %d: can't filter [%s]\n", lineNo, line)
			os.Exit(1)
		}
		if start != "" {
			showLine = entry.IsInRange(startTime, endTime)
		}
		if showLine == true && match != "" {
			showLine = entry.IsMatch(match)
		}
		if showLine == true {
			fmt.Printf("%s", line)
		}
	}
}