...

Source file src/edge-infra.dev/third_party/gopherage/cmd/merge/merge.go

Documentation: edge-infra.dev/third_party/gopherage/cmd/merge

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // MakeCommand returns a `merge` command.
    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