diff --git a/cmd/diff_test.go b/cmd/diff_test.go index 5fa4670..d8a6426 100644 --- a/cmd/diff_test.go +++ b/cmd/diff_test.go @@ -104,7 +104,3 @@ func TestDoDiffForLocalProfiles(t *testing.T) { } } - -func TestDoDiffUnderProw(t *testing.T) { - -} diff --git a/cmd/profile.go b/cmd/profile.go index 16e98fd..7c84d47 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -19,11 +19,11 @@ package cmd import ( "bytes" "fmt" - log "github.com/sirupsen/logrus" "io" "os" "github.com/qiniu/goc/pkg/cover" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -74,16 +74,24 @@ goc profile --force }, } -var output string -var force bool -var svrList []string -var addrList []string +var ( + svrList []string // --service flag + addrList []string // --address flag + force bool // --force flag + output string // --output flag + coverPkg []string // --coverpkg flag +) func init() { profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile") profileCmd.Flags().StringSliceVarP(&svrList, "service", "", nil, "service name to fetch profile, see 'goc list' for all services.") 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(&coverPkg, "coverpkg", "", nil, "only output coverage data of the packages matching the patterns") addBasicFlags(profileCmd.Flags()) rootCmd.AddCommand(profileCmd) } + +func filterProfile() { + +} diff --git a/pkg/cover/server.go b/pkg/cover/server.go index 0d09f52..a039588 100644 --- a/pkg/cover/server.go +++ b/pkg/cover/server.go @@ -25,6 +25,7 @@ import ( "net/http" "net/url" "os" + "regexp" "strconv" "github.com/gin-gonic/gin" @@ -83,11 +84,13 @@ type Service struct { Address string `form:"address" json:"address" binding:"required"` } -// ProfileParam is param of profile API (TODO) +// ProfileParam is param of profile API type ProfileParam struct { Force bool `form:"force"` Service []string `form:"service" json:"service"` Address []string `form:"address" json:"address"` + + CoverPkg []string } //listServices list all the registered services @@ -138,6 +141,7 @@ func profile(c *gin.Context) { c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"}) return } + serviceList := removeDuplicateElement(c.QueryArray("service")) addressList := removeDuplicateElement(c.QueryArray("address")) allInfos := DefaultStore.GetAll() @@ -183,6 +187,25 @@ func profile(c *gin.Context) { } } +// filterProfile output profiles of the packages matching the coverPkg +func filterProfile(coverPkg []string, profiles []*cover.Profile) ([]*cover.Profile, error) { + var out = make([]*cover.Profile, 0) + + for _, profile := range profiles { + for _, pattern := range coverPkg { + matched, err := regexp.MatchString(pattern, profile.FileName) + if err != nil { + return nil, fmt.Errorf("filterProfile failed with pattern %s for profile %s", pattern, profile.FileName) + } + if matched { + out = append(out, profile) + } + } + } + + return out, nil +} + func clear(c *gin.Context) { svrsUnderTest := DefaultStore.GetAll() for svc, addrs := range svrsUnderTest { diff --git a/pkg/cover/server_test.go b/pkg/cover/server_test.go index 806c63a..4df5c45 100644 --- a/pkg/cover/server_test.go +++ b/pkg/cover/server_test.go @@ -214,3 +214,12 @@ func TestInitService(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Contains(t, w.Body.String(), "lala error") } + +func TestFilterProfile(t *testing.T) { + // var tcs = []struct { + // pattern []string + // profile []*cover.Profile + // expected []*cover.Profile + // err error + // }{} +}