diff --git a/cmd/profile.go b/cmd/profile.go index 69abd47..6048908 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -27,11 +27,11 @@ var profileCmd = &cobra.Command{ } var ( - profileHost string - profileOutput string // --output flag - profileIds []string - profilePackages string - profileExtra string + profileHost string + profileOutput string // --output flag + profileIds []string + profileSkipPattern []string + profileExtra string ) func init() { @@ -44,7 +44,7 @@ func init() { add2Flags := func(f *pflag.FlagSet) { f.StringVarP(&profileOutput, "output", "o", "", "download cover profile") - f.StringVar(&profilePackages, "packages", "", "specify the regex expression of packages, only profile of these packages will be downloaded") + f.StringSliceVar(&profileSkipPattern, "skip", nil, "skip specific packages in the profile") } add1Flags(getProfileCmd.Flags()) @@ -63,7 +63,7 @@ var getProfileCmd = &cobra.Command{ } func getProfile(cmd *cobra.Command, args []string) { - client.GetProfile(profileHost, profileIds, profilePackages, profileExtra, profileOutput) + client.GetProfile(profileHost, profileIds, profileSkipPattern, profileExtra, profileOutput) } var clearProfileCmd = &cobra.Command{ diff --git a/pkg/client/profie.go b/pkg/client/profie.go index 5ac1ad2..4964380 100644 --- a/pkg/client/profie.go +++ b/pkg/client/profie.go @@ -25,11 +25,11 @@ import ( "github.com/qiniu/goc/v2/pkg/log" ) -func GetProfile(host string, ids []string, packages string, extra string, output string) { +func GetProfile(host string, ids []string, skips []string, extra string, output string) { gocClient := rest.NewV2Client(host) profiles, err := gocClient.Profile().Get(ids, - profile.WithPackagePattern(packages), + profile.WithPackagePattern(skips), profile.WithExtraPattern(extra)) if err != nil { log.Fatalf("fail to get profile from the goc server: %v, response: %v", err, profiles) diff --git a/pkg/client/rest/profile/profile.go b/pkg/client/rest/profile/profile.go index 85d9901..3744833 100644 --- a/pkg/client/rest/profile/profile.go +++ b/pkg/client/rest/profile/profile.go @@ -36,9 +36,9 @@ type ProfileInterface interface { } type profileClient struct { - c *resty.Client - packagePattern string - extraPattern string + c *resty.Client + skipPatterns []string + extraPattern string } func NewProfileClient(c *resty.Client) *profileClient { @@ -49,9 +49,9 @@ func NewProfileClient(c *resty.Client) *profileClient { type profileOption func(*profileClient) -func WithPackagePattern(pattern string) profileOption { +func WithPackagePattern(skips []string) profileOption { return func(pc *profileClient) { - pc.packagePattern = pattern + pc.skipPatterns = skips } } @@ -69,9 +69,10 @@ func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error) req := p.c.R() idQuery := strings.Join(ids, ",") + skipQuery := strings.Join(p.skipPatterns, ",") req.QueryParam.Add("id", idQuery) - req.QueryParam.Add("pattern", p.packagePattern) + req.QueryParam.Add("skippattern", skipQuery) req.QueryParam.Add("extra", p.extraPattern) res := struct { @@ -105,9 +106,10 @@ func (p *profileClient) Delete(ids []string, opts ...profileOption) error { req := p.c.R() idQuery := strings.Join(ids, ",") + skipQuery := strings.Join(p.skipPatterns, ",") req.QueryParam.Add("id", idQuery) - req.QueryParam.Add("pattern", p.packagePattern) + req.QueryParam.Add("skippattern", skipQuery) req.QueryParam.Add("extra", p.extraPattern) _, err := req. diff --git a/pkg/server/api.go b/pkg/server/api.go index 0a8a68b..e68d7c8 100644 --- a/pkg/server/api.go +++ b/pkg/server/api.go @@ -101,7 +101,12 @@ func (gs *gocServer) getProfiles(c *gin.Context) { idQuery := c.Query("id") ifInIdMap := idMaps(idQuery) - pattern := c.Query("pattern") + skippatternRaw := c.Query("skippattern") + var skippattern []string + if skippatternRaw != "" { + skippattern = strings.Split(skippatternRaw, ",") + } + extra := c.Query("extra") isExtra := filterExtra(extra) @@ -174,12 +179,8 @@ func (gs *gocServer) getProfiles(c *gin.Context) { return } - // check if pattern matches - newProfile, err := filterProfileByPattern(pattern, profile) - if err != nil { - log.Errorf("%v", err) - return - } + // check if skippattern matches + newProfile := filterProfileByPattern(skippattern, profile) mu.Lock() defer mu.Unlock() @@ -314,26 +315,28 @@ func (gs *gocServer) watchProfileUpdate(c *gin.Context) { <-gwc.exitCh } -func filterProfileByPattern(pattern string, profiles []*cover.Profile) ([]*cover.Profile, error) { +func filterProfileByPattern(skippattern []string, profiles []*cover.Profile) []*cover.Profile { - if strings.TrimSpace(pattern) == "" { - return profiles, nil + if len(skippattern) == 0 { + return profiles } var out = make([]*cover.Profile, 0) for _, profile := range profiles { - 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 { - out = append(out, profile) - break // no need to check again for the file + skip := false + for _, pattern := range skippattern { + if strings.Contains(profile.FileName, pattern) { + skip = true + break + } } + if !skip { + out = append(out, profile) + } } - return out, nil + return out } func idMaps(idQuery string) func(key string) bool {