From ede278cb6484b251c101e3575a43cb0ddab9df57 Mon Sep 17 00:00:00 2001 From: jichangjun Date: Fri, 18 Dec 2020 10:43:48 +0800 Subject: [PATCH] feature: add skipFile flag for profile API --- cmd/profile.go | 3 +++ pkg/cover/server.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/cmd/profile.go b/cmd/profile.go index 7dfe51e..e6fdf6c 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -56,6 +56,7 @@ goc profile --force Service: svrList, Address: addrList, CoverFilePatterns: coverFilePatterns, + SkipFilePatterns: skipFilePatterns, } res, err := cover.NewWorker(center).Profile(p) if err != nil { @@ -84,6 +85,7 @@ var ( force bool // --force flag output string // --output flag coverFilePatterns []string // --coverfile flag + skipFilePatterns []string // --skipfile flag ) func init() { @@ -92,6 +94,7 @@ func init() { profileCmd.Flags().StringSliceVarP(&addrList, "address", "", nil, "address to fetch profile, see 'goc list' for all addresses.") profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force fetching all available profiles") profileCmd.Flags().StringSliceVarP(&coverFilePatterns, "coverfile", "", nil, "only output coverage data of the files matching the patterns") + profileCmd.Flags().StringSliceVarP(&skipFilePatterns, "skipfile", "", nil, "skip the files matching the patterns when outputing coverage data") addBasicFlags(profileCmd.Flags()) rootCmd.AddCommand(profileCmd) } diff --git a/pkg/cover/server.go b/pkg/cover/server.go index b8420b6..98403a3 100644 --- a/pkg/cover/server.go +++ b/pkg/cover/server.go @@ -108,6 +108,7 @@ type ProfileParam struct { Service []string `form:"service" json:"service"` Address []string `form:"address" json:"address"` CoverFilePatterns []string `form:"coverfile" json:"coverfile"` + SkipFilePatterns []string `form:"skipfile" json:"skipfile"` } //listServices list all the registered services @@ -209,6 +210,14 @@ func (s *server) profile(c *gin.Context) { } } + if len(body.SkipFilePatterns) > 0 { + merged, err = skipProfile(body.SkipFilePatterns, merged) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to skip profile based on the patterns: %v, error: %v", body.SkipFilePatterns, err)}) + return + } + } + if err := cov.DumpProfile(merged, c.Writer); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return @@ -234,6 +243,31 @@ func filterProfile(coverFile []string, profiles []*cover.Profile) ([]*cover.Prof return out, nil } +// skipProfile skips profiles of the packages matching the coverFile pattern +func skipProfile(skipFile []string, profiles []*cover.Profile) ([]*cover.Profile, error) { + var out = make([]*cover.Profile, 0) + for _, profile := range profiles { + var shouldSkip bool + for _, pattern := range skipFile { + matched, err := regexp.MatchString(pattern, profile.FileName) + if err != nil { + return nil, fmt.Errorf("filterProfile failed with pattern %s for profile %s, err: %v", pattern, profile.FileName, err) + } + + if matched { + shouldSkip = true + break // no need to check again for the file + } + } + + if !shouldSkip { + out = append(out, profile) + } + } + + return out, nil +} + func (s *server) clear(c *gin.Context) { var body ProfileParam if err := c.ShouldBind(&body); err != nil {