github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/cmd/diff/diff.go (about) 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 diff 18 19 import ( 20 "fmt" 21 "os" 22 23 "github.com/spf13/cobra" 24 "k8s.io/test-infra/gopherage/pkg/cov" 25 "k8s.io/test-infra/gopherage/pkg/util" 26 ) 27 28 type flags struct { 29 OutputFile string 30 } 31 32 // MakeCommand returns a `diff` command. 33 func MakeCommand() *cobra.Command { 34 flags := &flags{} 35 cmd := &cobra.Command{ 36 Use: "diff [first] [second]", 37 Short: "Diffs two Go coverage files.", 38 Long: `Takes the difference between two Go coverage files, producing another Go coverage file 39 showing only what was covered between the two files being generated. This works best when using 40 files generated in "count" or "atomic" mode; "set" may drastically underreport. 41 42 It is assumed that both files came from the same execution, and so all values in the second file are 43 at least equal to those in the first file.`, 44 Run: func(cmd *cobra.Command, args []string) { 45 run(flags, cmd, args) 46 }, 47 } 48 cmd.Flags().StringVarP(&flags.OutputFile, "output", "o", "-", "output file") 49 return cmd 50 } 51 52 func run(flags *flags, cmd *cobra.Command, args []string) { 53 if len(args) != 2 { 54 fmt.Fprintln(os.Stderr, "Expected two files.") 55 cmd.Usage() 56 os.Exit(2) 57 } 58 59 before, 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 after, err := util.LoadProfile(args[1]) 66 if err != nil { 67 fmt.Fprintf(os.Stderr, "Couldn't load %s: %v.", args[0], err) 68 os.Exit(1) 69 } 70 71 diff, err := cov.DiffProfiles(before, after) 72 if err != nil { 73 fmt.Fprintf(os.Stderr, "failed to diff profiles: %v", err) 74 os.Exit(1) 75 } 76 77 if err := util.DumpProfile(flags.OutputFile, diff); err != nil { 78 fmt.Fprintln(os.Stderr, err) 79 os.Exit(1) 80 } 81 }