...

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

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

     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 filter
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"edge-infra.dev/third_party/gopherage/pkg/cov"
    26  	"edge-infra.dev/third_party/gopherage/pkg/util"
    27  )
    28  
    29  type flags struct {
    30  	OutputFile   string
    31  	IncludePaths []string
    32  	ExcludePaths []string
    33  }
    34  
    35  // MakeCommand returns a `filter` command.
    36  func MakeCommand() *cobra.Command {
    37  	flags := &flags{}
    38  	cmd := &cobra.Command{
    39  		Use:   "filter [file]",
    40  		Short: "Filters a Go coverage file.",
    41  		Long:  `Filters a Go coverage file, removing entries that do not match the given flags.`,
    42  		Run: func(cmd *cobra.Command, args []string) {
    43  			run(flags, cmd, args)
    44  		},
    45  	}
    46  	cmd.Flags().StringVarP(&flags.OutputFile, "output", "o", "-", "output file")
    47  	cmd.Flags().StringSliceVar(&flags.IncludePaths, "include-path", nil, "If specified at least once, only files with paths matching one of these regexes are included.")
    48  	cmd.Flags().StringSliceVar(&flags.ExcludePaths, "exclude-path", nil, "Files with paths matching one of these regexes are excluded. Can be used repeatedly.")
    49  	return cmd
    50  }
    51  
    52  func run(flags *flags, cmd *cobra.Command, args []string) {
    53  	if len(args) != 1 {
    54  		fmt.Fprintln(os.Stderr, "Expected one file.")
    55  		cmd.Usage()
    56  		os.Exit(2)
    57  	}
    58  
    59  	input, err := util.LoadProfile(args[0])
    60  	if err != nil {
    61  		fmt.Fprintf(os.Stderr, "Couldn't load %s: %v.", args[0], err)
    62  		os.Exit(1)
    63  	}
    64  
    65  	output := input
    66  	if len(flags.IncludePaths) > 0 {
    67  		output, err = cov.FilterProfilePaths(output, flags.IncludePaths, true)
    68  		if err != nil {
    69  			fmt.Fprintf(os.Stderr, "Couldn't filter by include paths: %v.", err)
    70  		}
    71  	}
    72  
    73  	if len(flags.ExcludePaths) > 0 {
    74  		output, err = cov.FilterProfilePaths(output, flags.ExcludePaths, false)
    75  		if err != nil {
    76  			fmt.Fprintf(os.Stderr, "Couldn't filter by exclude paths: %v.", err)
    77  		}
    78  	}
    79  
    80  	if err := util.DumpProfile(flags.OutputFile, output); err != nil {
    81  		fmt.Fprintln(os.Stderr, err)
    82  		os.Exit(1)
    83  	}
    84  }
    85  

View as plain text