...
1
16
17 package aggregate
18
19 import (
20 "fmt"
21 "os"
22
23 "github.com/spf13/cobra"
24 "golang.org/x/tools/cover"
25
26 "edge-infra.dev/third_party/gopherage/pkg/cov"
27 "edge-infra.dev/third_party/gopherage/pkg/util"
28 )
29
30 type flags struct {
31 OutputFile string
32 }
33
34
35 func MakeCommand() *cobra.Command {
36 flags := &flags{}
37 cmd := &cobra.Command{
38 Use: "aggregate [files...]",
39 Short: "Aggregates multiple Go coverage files.",
40 Long: `Given multiple Go coverage files from identical binaries recorded in
41 "count" or "atomic" mode, produces a new Go coverage file in the same mode
42 that counts how many of those coverage profiles hit a block at least once.`,
43 Run: func(cmd *cobra.Command, args []string) {
44 run(flags, cmd, args)
45 },
46 }
47 cmd.Flags().StringVarP(&flags.OutputFile, "output", "o", "-", "output file")
48 return cmd
49 }
50
51 func run(flags *flags, cmd *cobra.Command, args []string) {
52 if len(args) == 0 {
53 fmt.Println("Expected at least one file.")
54 cmd.Usage()
55 os.Exit(2)
56 }
57
58 profiles := make([][]*cover.Profile, 0, len(args))
59 for _, path := range args {
60 profile, err := util.LoadProfile(path)
61 if err != nil {
62 fmt.Fprintf(os.Stderr, "failed to open %s: %v", path, err)
63 os.Exit(1)
64 }
65 profiles = append(profiles, profile)
66 }
67
68 aggregated, err := cov.AggregateProfiles(profiles)
69 if err != nil {
70 fmt.Fprintf(os.Stderr, "failed to aggregate files: %v", err)
71 os.Exit(1)
72 }
73
74 if err := util.DumpProfile(flags.OutputFile, aggregated); err != nil {
75 fmt.Fprintln(os.Stderr, err)
76 os.Exit(1)
77 }
78 }
79
View as plain text