1//usr/bin/env go run "$0" "$@"; exit $?2//3// From: https://git.lukeshu.com/go/cmd/gocovcat/4//5//go:build ignore6// +build ignore78// Copyright 2017 Luke Shumaker <lukeshu@parabola.nu>9//10// This program is free software: you can redistribute it and/or modify11// it under the terms of the GNU General Public License as published by12// the Free Software Foundation, either version 3 of the License, or13// (at your option) any later version.14//15// This program is distributed in the hope that it will be useful,16// but WITHOUT ANY WARRANTY; without even the implied warranty of17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18// GNU General Public License for more details.19//20// You should have received a copy of the GNU General Public License21// along with this program. If not, see <http://www.gnu.org/licenses/>.2223// Command gocovcat combines multiple go cover runs, and prints the24// result on stdout.25package main2627import (28 "bufio"29 "fmt"30 "os"31 "sort"32 "strconv"33 "strings"34)3536func handleErr(err error) {37 if err != nil {38 fmt.Fprintf(os.Stderr, "%v\n", err)39 os.Exit(1)40 }41}4243func main() {44 modeBool := false45 blocks := map[string]int{}46 for _, filename := range os.Args[1:] {47 file, err := os.Open(filename)48 handleErr(err)49 buf := bufio.NewScanner(file)50 for buf.Scan() {51 line := buf.Text()5253 if strings.HasPrefix(line, "mode: ") {54 m := strings.TrimPrefix(line, "mode: ")55 switch m {56 case "set":57 modeBool = true58 case "count", "atomic":59 // do nothing60 default:61 fmt.Fprintf(os.Stderr, "Unrecognized mode: %s\n", m)62 os.Exit(1)63 }64 } else {65 sp := strings.LastIndexByte(line, ' ')66 block := line[:sp]67 cntStr := line[sp+1:]68 cnt, err := strconv.Atoi(cntStr)69 handleErr(err)70 blocks[block] += cnt71 }72 }73 handleErr(buf.Err())74 }75 keys := make([]string, 0, len(blocks))76 for key := range blocks {77 keys = append(keys, key)78 }79 sort.Strings(keys)80 modeStr := "count"81 if modeBool {82 modeStr = "set"83 }84 fmt.Printf("mode: %s\n", modeStr)85 for _, block := range keys {86 cnt := blocks[block]87 if modeBool && cnt > 1 {88 cnt = 189 }90 fmt.Printf("%s %d\n", block, cnt)91 }92}