From 45612d6f63f174fa9d45d691ebd0af74fa7fe0fd Mon Sep 17 00:00:00 2001 From: jichangjun Date: Fri, 12 Jun 2020 14:08:15 +0800 Subject: [PATCH] add list command --- cmd/list.go | 47 ++++++++++++++++++++++++++++++++++++++++ pkg/cover/client.go | 53 ++++++++++++++++++++++++++++----------------- pkg/cover/cover.go | 4 ++-- pkg/cover/server.go | 9 +++++++- 4 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 cmd/list.go diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..2855778 --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,47 @@ +/* + Copyright 2020 Qiniu Cloud (qiniu.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package cmd + +import ( + "fmt" + "log" + "os" + + "github.com/qiniu/goc/pkg/cover" + "github.com/spf13/cobra" +) + +var listCmd = &cobra.Command{ + Use: "list", + Short: "Lists all the registered services", + Long: "Lists all the registered services", + Example: ` +goc list [flags] +`, + Run: func(cmd *cobra.Command, args []string) { + res, err := cover.NewWorker().ListServices(center) + if err != nil { + log.Fatalf("list failed, err: %v", err) + } + fmt.Fprint(os.Stdout, string(res)) + }, +} + +func init() { + listCmd.Flags().StringVarP(¢er, "center", "", "http://127.0.0.1:7777", "cover profile host center") + rootCmd.AddCommand(listCmd) +} diff --git a/pkg/cover/client.go b/pkg/cover/client.go index a1415e4..d0540ab 100644 --- a/pkg/cover/client.go +++ b/pkg/cover/client.go @@ -24,21 +24,24 @@ import ( "net/http" ) -// Action provides methods to contact with the coverd service under test +// Action provides methods to contact with the covered service under test type Action interface { Profile(host string) ([]byte, error) Clear(host string) ([]byte, error) InitSystem(host string) ([]byte, error) + ListServices(host string) ([]byte, error) } -// CoverProfileAPI is provided by the covered service to get profiles -const CoverProfileAPI = "/v1/cover/profile" - -// CoverProfileClearAPI is provided by the covered service to clear profiles -const CoverProfileClearAPI = "/v1/cover/clear" - -// CoverInitSystemAPI prepare a new round of testing -const CoverInitSystemAPI = "/v1/cover/init" +const ( + //CoverInitSystemAPI prepare a new round of testing + CoverInitSystemAPI = "/v1/cover/init" + //CoverProfileAPI is provided by the covered service to get profiles + CoverProfileAPI = "/v1/cover/profile" + //CoverProfileClearAPI is provided by the covered service to clear profiles + CoverProfileClearAPI = "/v1/cover/clear" + //CoverServicesListAPI list all the registered services + CoverServicesListAPI = "/v1/cover/list" +) type client struct { client *http.Client @@ -51,36 +54,46 @@ func NewWorker() Action { } } -func (w *client) Profile(host string) ([]byte, error) { - u := fmt.Sprintf("%s%s", host, CoverProfileAPI) - profile, err := w.do("GET", u, nil) +func (c *client) ListServices(host string) ([]byte, error) { + u := fmt.Sprintf("%s%s", host, CoverServicesListAPI) + services, err := c.do("GET", u, nil) if err != nil && isNetworkError(err) { - profile, err = w.do("GET", u, nil) + services, err = c.do("GET", u, nil) + } + + return services, err +} + +func (c *client) Profile(host string) ([]byte, error) { + u := fmt.Sprintf("%s%s", host, CoverProfileAPI) + profile, err := c.do("GET", u, nil) + if err != nil && isNetworkError(err) { + profile, err = c.do("GET", u, nil) } return profile, err } -func (w *client) Clear(host string) ([]byte, error) { +func (c *client) Clear(host string) ([]byte, error) { u := fmt.Sprintf("%s%s", host, CoverProfileClearAPI) - resp, err := w.do("POST", u, nil) + resp, err := c.do("POST", u, nil) if err != nil && isNetworkError(err) { - resp, err = w.do("POST", u, nil) + resp, err = c.do("POST", u, nil) } return resp, err } -func (w *client) InitSystem(host string) ([]byte, error) { +func (c *client) InitSystem(host string) ([]byte, error) { u := fmt.Sprintf("%s%s", host, CoverInitSystemAPI) - return w.do("POST", u, nil) + return c.do("POST", u, nil) } -func (w *client) do(method, url string, body io.Reader) ([]byte, error) { +func (c *client) do(method, url string, body io.Reader) ([]byte, error) { req, err := http.NewRequest(method, url, body) if err != nil { return nil, err } - res, err := w.client.Do(req) + res, err := c.client.Do(req) if err != nil { return nil, err } diff --git a/pkg/cover/cover.go b/pkg/cover/cover.go index d06b558..3e8db5b 100644 --- a/pkg/cover/cover.go +++ b/pkg/cover/cover.go @@ -58,7 +58,7 @@ type FileVar struct { } // Package map a package output by go list -// this is subset of pakcage struct in: https://github.com/golang/go/blob/master/src/cmd/go/internal/load/pkg.go#L58 +// this is subset of package struct in: https://github.com/golang/go/blob/master/src/cmd/go/internal/load/pkg.go#L58 type Package struct { Dir string `json:"Dir"` // directory containing package sources ImportPath string `json:"ImportPath"` // import path of package in dir @@ -157,7 +157,7 @@ func AddCounters(pkg *Package, mode, newgopath string) (*PackageCover, error) { cmd := buildCoverCmd(file, coverVar, pkg, mode, newgopath) out, err := cmd.CombinedOutput() if err != nil { - return nil, fmt.Errorf("execuate go tool cover -mode=atomic -var %s -o %s/%s failed, err: %v, out: %s", coverVar.Var, pkg.Dir, file, err, string(out)) + return nil, fmt.Errorf("execute go tool cover -mode=atomic -var %s -o %s/%s failed, err: %v, out: %s", coverVar.Var, pkg.Dir, file, err, string(out)) } } diff --git a/pkg/cover/server.go b/pkg/cover/server.go index 721ee05..9f62abf 100644 --- a/pkg/cover/server.go +++ b/pkg/cover/server.go @@ -54,7 +54,7 @@ func StartServer(port string) { gin.DefaultWriter = io.MultiWriter(f, os.Stdout) r := gin.Default() - // api to show the registerd services + // api to show the registered services r.StaticFile(PersistenceFile, "./"+PersistenceFile) v1 := r.Group("/v1") @@ -63,6 +63,7 @@ func StartServer(port string) { v1.GET("/cover/profile", profile) v1.POST("/cover/clear", clear) v1.POST("/cover/init", initSystem) + v1.GET("/cover/list", listServices) } log.Fatal(r.Run(port)) @@ -74,6 +75,12 @@ type Service struct { Address string `form:"address" json:"address" binding:"required"` } +//listServices list all the registered services +func listServices(c *gin.Context) { + services := LocalStore.GetAll() + c.JSON(http.StatusOK, services) +} + func registerService(c *gin.Context) { var service Service if err := c.ShouldBind(&service); err != nil {