// getLineType return data source type func getLineType(line string) int { if line == "" { return TYPE_EMPTY_LINE } if strutil.Head(line, 3) == "-- " { return TYPE_SEPARATOR } if strutil.Head(line, 2) == "+ " { if sliceutil.Contains(headers, strutil.Substr(line, 2, 99)) { return TYPE_HEADER } } if strutil.Head(line, 2) == " " && strings.Contains(line, ":") { return TYPE_RECORD } return TYPE_DATA }
// readFile starts file reading loop func readFile(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() stat, err := fd.Stat() if err != nil { printError("Can't read file stats %s: %v", file, err) os.Exit(1) } if stat.Size() > 2048 { fd.Seek(-2048, 2) } reader := bufio.NewReader(fd) nearRecordFound := false var currentSection = "" var dataSections = []string{"REQUEST BODY", "RESPONSE BODY"} for { line, err := reader.ReadString('\n') if err != nil { time.Sleep(time.Millisecond * 250) continue } line = strings.TrimRight(line, "\n") line = strings.TrimRight(line, "\r") if !nearRecordFound { if strutil.Head(line, 3) != "-- " { continue } nearRecordFound = true } rt := getLineType(line) if rt == TYPE_HEADER { currentSection = extractHeaderName(line) renderLine(line, rt) continue } if sliceutil.Contains(dataSections, currentSection) { fmtc.Println(line) continue } renderLine(line, rt) } }