Skip to content

Commit ee63433

Browse files
authored
add skip pattern (#268)
* add skip pattern * fix * fix
1 parent 0a5062f commit ee63433

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

cmd/profile.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ var profileCmd = &cobra.Command{
2727
}
2828

2929
var (
30-
profileHost string
31-
profileOutput string // --output flag
32-
profileIds []string
33-
profilePackages string
34-
profileExtra string
30+
profileHost string
31+
profileOutput string // --output flag
32+
profileIds []string
33+
profileSkipPattern []string
34+
profileExtra string
3535
)
3636

3737
func init() {
@@ -44,7 +44,7 @@ func init() {
4444

4545
add2Flags := func(f *pflag.FlagSet) {
4646
f.StringVarP(&profileOutput, "output", "o", "", "download cover profile")
47-
f.StringVar(&profilePackages, "packages", "", "specify the regex expression of packages, only profile of these packages will be downloaded")
47+
f.StringSliceVar(&profileSkipPattern, "skip", nil, "skip specific packages in the profile")
4848
}
4949

5050
add1Flags(getProfileCmd.Flags())
@@ -63,7 +63,7 @@ var getProfileCmd = &cobra.Command{
6363
}
6464

6565
func getProfile(cmd *cobra.Command, args []string) {
66-
client.GetProfile(profileHost, profileIds, profilePackages, profileExtra, profileOutput)
66+
client.GetProfile(profileHost, profileIds, profileSkipPattern, profileExtra, profileOutput)
6767
}
6868

6969
var clearProfileCmd = &cobra.Command{

pkg/client/profie.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import (
2525
"github.com/qiniu/goc/v2/pkg/log"
2626
)
2727

28-
func GetProfile(host string, ids []string, packages string, extra string, output string) {
28+
func GetProfile(host string, ids []string, skips []string, extra string, output string) {
2929
gocClient := rest.NewV2Client(host)
3030

3131
profiles, err := gocClient.Profile().Get(ids,
32-
profile.WithPackagePattern(packages),
32+
profile.WithPackagePattern(skips),
3333
profile.WithExtraPattern(extra))
3434
if err != nil {
3535
log.Fatalf("fail to get profile from the goc server: %v, response: %v", err, profiles)

pkg/client/rest/profile/profile.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ type ProfileInterface interface {
3636
}
3737

3838
type profileClient struct {
39-
c *resty.Client
40-
packagePattern string
41-
extraPattern string
39+
c *resty.Client
40+
skipPatterns []string
41+
extraPattern string
4242
}
4343

4444
func NewProfileClient(c *resty.Client) *profileClient {
@@ -49,9 +49,9 @@ func NewProfileClient(c *resty.Client) *profileClient {
4949

5050
type profileOption func(*profileClient)
5151

52-
func WithPackagePattern(pattern string) profileOption {
52+
func WithPackagePattern(skips []string) profileOption {
5353
return func(pc *profileClient) {
54-
pc.packagePattern = pattern
54+
pc.skipPatterns = skips
5555
}
5656
}
5757

@@ -69,9 +69,10 @@ func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error)
6969
req := p.c.R()
7070

7171
idQuery := strings.Join(ids, ",")
72+
skipQuery := strings.Join(p.skipPatterns, ",")
7273

7374
req.QueryParam.Add("id", idQuery)
74-
req.QueryParam.Add("pattern", p.packagePattern)
75+
req.QueryParam.Add("skippattern", skipQuery)
7576
req.QueryParam.Add("extra", p.extraPattern)
7677

7778
res := struct {
@@ -105,9 +106,10 @@ func (p *profileClient) Delete(ids []string, opts ...profileOption) error {
105106
req := p.c.R()
106107

107108
idQuery := strings.Join(ids, ",")
109+
skipQuery := strings.Join(p.skipPatterns, ",")
108110

109111
req.QueryParam.Add("id", idQuery)
110-
req.QueryParam.Add("pattern", p.packagePattern)
112+
req.QueryParam.Add("skippattern", skipQuery)
111113
req.QueryParam.Add("extra", p.extraPattern)
112114

113115
_, err := req.

pkg/server/api.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ func (gs *gocServer) getProfiles(c *gin.Context) {
101101
idQuery := c.Query("id")
102102
ifInIdMap := idMaps(idQuery)
103103

104-
pattern := c.Query("pattern")
104+
skippatternRaw := c.Query("skippattern")
105+
var skippattern []string
106+
if skippatternRaw != "" {
107+
skippattern = strings.Split(skippatternRaw, ",")
108+
}
109+
105110
extra := c.Query("extra")
106111
isExtra := filterExtra(extra)
107112

@@ -174,12 +179,8 @@ func (gs *gocServer) getProfiles(c *gin.Context) {
174179
return
175180
}
176181

177-
// check if pattern matches
178-
newProfile, err := filterProfileByPattern(pattern, profile)
179-
if err != nil {
180-
log.Errorf("%v", err)
181-
return
182-
}
182+
// check if skippattern matches
183+
newProfile := filterProfileByPattern(skippattern, profile)
183184

184185
mu.Lock()
185186
defer mu.Unlock()
@@ -314,26 +315,28 @@ func (gs *gocServer) watchProfileUpdate(c *gin.Context) {
314315
<-gwc.exitCh
315316
}
316317

317-
func filterProfileByPattern(pattern string, profiles []*cover.Profile) ([]*cover.Profile, error) {
318+
func filterProfileByPattern(skippattern []string, profiles []*cover.Profile) []*cover.Profile {
318319

319-
if strings.TrimSpace(pattern) == "" {
320-
return profiles, nil
320+
if len(skippattern) == 0 {
321+
return profiles
321322
}
322323

323324
var out = make([]*cover.Profile, 0)
324325
for _, profile := range profiles {
325-
matched, err := regexp.MatchString(pattern, profile.FileName)
326-
if err != nil {
327-
return nil, fmt.Errorf("filterProfile failed with pattern %s for profile %s, err: %v", pattern, profile.FileName, err)
326+
skip := false
327+
for _, pattern := range skippattern {
328+
if strings.Contains(profile.FileName, pattern) {
329+
skip = true
330+
break
331+
}
328332
}
329-
if matched {
333+
334+
if !skip {
330335
out = append(out, profile)
331-
break // no need to check again for the file
332336
}
333-
334337
}
335338

336-
return out, nil
339+
return out
337340
}
338341

339342
func idMaps(idQuery string) func(key string) bool {

0 commit comments

Comments
 (0)