示例#1
0
func Init() {
	runtime.GOMAXPROCS(1)

	args, errs := arg.Parse(argMap)

	if len(errs) != 0 {
		printError("Errors while argument parsing:")

		for _, err := range errs {
			printError("  %v", err)
		}

		os.Exit(1)
	}

	if arg.GetB(ARG_NO_COLOR) {
		fmtc.DisableColors = true
	}

	if arg.GetB(ARG_VER) {
		showAbout()
		return
	}

	if len(args) == 0 || arg.GetB(ARG_HELP) {
		showUsage()
		return
	}

	file := findFile(args[0])

	for {
		if fsutil.CheckPerms("FRS", file) {
			break
		}

		time.Sleep(time.Millisecond * 500)
	}

	if arg.Has(ARG_DATE_FROM) || arg.Has(ARG_DATE_TO) {
		filterFile(file)
	} else {
		readFile(file)
	}
}
示例#2
0
// filterFile read file and show records only between given time range
func filterFile(file string) {
	fd, err := os.OpenFile(file, os.O_RDONLY, 0644)

	if err != nil {
		printError("Can't open file %s: %v", file, err)
		os.Exit(1)
	}

	defer fd.Close()

	reader := bufio.NewReader(fd)

	var showSection = false
	var currentSection = ""
	var dataSections = []string{"REQUEST BODY", "RESPONSE BODY"}

	fromDate := time.Date(2016, 1, 1, 0, 0, 0, 0, time.Local)
	toDate := time.Now()

	if arg.Has(ARG_DATE_FROM) {
		fromDate, err = parseRangeDate(arg.GetS(ARG_DATE_FROM))

		if err != nil {
			printError("Can't parse range start: %v", err)
			os.Exit(1)
		}
	}

	if arg.Has(ARG_DATE_TO) {
		toDate, err = parseRangeDate(arg.GetS(ARG_DATE_TO))

		if err != nil {
			printError("Can't parse range end: %v", err)
			os.Exit(1)
		}
	}

	for {
		line, err := reader.ReadString('\n')

		if err != nil {
			break
		}

		line = strings.TrimRight(line, "\n")
		line = strings.TrimRight(line, "\r")

		rt := getLineType(line)

		if rt == TYPE_SEPARATOR {
			recDateStr := extractTimeFromSeparator(line)
			recDate, _ := time.Parse(separatorTimeLayout, recDateStr)

			showSection = recDate.Unix() >= fromDate.Unix() && recDate.Unix() <= toDate.Unix()
		}

		if !showSection {
			continue
		}

		if rt == TYPE_HEADER {
			currentSection = extractHeaderName(line)
			renderLine(line, rt)
			continue
		}

		if sliceutil.Contains(dataSections, currentSection) {
			fmtc.Println(line)
			continue
		}

		renderLine(line, rt)
	}
}