...

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

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

     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 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  // MakeCommand returns an `aggregate` command.
    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