...
1
16
17 package merge
18
19 import (
20 "fmt"
21 "os"
22
23 "edge-infra.dev/third_party/gopherage/pkg/cov"
24 "edge-infra.dev/third_party/gopherage/pkg/util"
25 "github.com/spf13/cobra"
26 "golang.org/x/tools/cover"
27 )
28
29 type flags struct {
30 OutputFile string
31 }
32
33
34 func MakeCommand() *cobra.Command {
35 flags := &flags{}
36 cmd := &cobra.Command{
37 Use: "merge [files...]",
38 Short: "Merge multiple coherent Go coverage files into a single file.",
39 Long: `merge will merge multiple Go coverage files into a single coverage file.
40 merge requires that the files are 'coherent', meaning that if they both contain references to the
41 same paths, then the contents of those source files were identical for the binary that generated
42 each file.
43
44 Merging a single file is a no-op, but is supported for convenience when shell scripting.`,
45 Run: func(cmd *cobra.Command, args []string) {
46 run(flags, cmd, args)
47 },
48 }
49 cmd.Flags().StringVarP(&flags.OutputFile, "output", "o", "-", "output file")
50 return cmd
51 }
52
53 func run(flags *flags, cmd *cobra.Command, args []string) {
54 if len(args) == 0 {
55 fmt.Fprintln(os.Stderr, "Expected at least one coverage file.")
56 cmd.Usage()
57 os.Exit(2)
58 }
59
60 profiles := make([][]*cover.Profile, len(args))
61 for _, path := range args {
62 profile, err := util.LoadProfile(path)
63 if err != nil {
64 fmt.Fprintf(os.Stderr, "failed to open %s: %v", path, err)
65 os.Exit(1)
66 }
67 profiles = append(profiles, profile)
68 }
69
70 merged, err := cov.MergeMultipleProfiles(profiles)
71 if err != nil {
72 fmt.Fprintf(os.Stderr, "failed to merge files: %v", err)
73 os.Exit(1)
74 }
75
76 if err := util.DumpProfile(flags.OutputFile, merged); err != nil {
77 fmt.Fprintln(os.Stderr, err)
78 os.Exit(1)
79 }
80 }
81
View as plain text