diff --git a/tsuru/client/services.go b/tsuru/client/services.go index c70bedab..f07eece6 100644 --- a/tsuru/client/services.go +++ b/tsuru/client/services.go @@ -34,6 +34,7 @@ type serviceFilter struct { plan string service string teamOwner string + tags cmd.StringSliceFlag } func (f *serviceFilter) queryString() (url.Values, error) { @@ -53,6 +54,9 @@ func (f *serviceFilter) queryString() (url.Values, error) { if f.service != "" { result.Set("service", f.service) } + for _, tag := range f.tags { + result.Add("tag", tag) + } return result, nil } @@ -88,7 +92,9 @@ func (c *ServiceList) Flags() *gnuflag.FlagSet { c.fs.BoolVar(&c.simplified, "q", false, "Display only service instances name") c.fs.BoolVar(&c.json, "json", false, "Display in JSON format") c.fs.BoolVar(&c.justServiceNames, "j", false, "Display just service names") - + tagMessage := "Filter services by tag. Can be used multiple times" + c.fs.Var(&c.filter.tags, "tag", tagMessage) + c.fs.Var(&c.filter.tags, "g", tagMessage) } return c.fs } @@ -171,9 +177,11 @@ func (s ServiceList) Run(ctx *cmd.Context) error { if hasPool { header = append(header, "Pool") } + hasServiceWithInstances := false table.Headers = tablecli.Row(header) for _, s := range services { for _, instance := range s.ServiceInstances { + hasServiceWithInstances = true row := []string{s.Service, instance.Name} if hasPool { row = append(row, instance.Pool) @@ -182,6 +190,9 @@ func (s ServiceList) Run(ctx *cmd.Context) error { table.AddRow(r) } } + if !hasServiceWithInstances { + return nil + } _, err = ctx.Stdout.Write(table.Bytes()) return err diff --git a/tsuru/client/services_test.go b/tsuru/client/services_test.go index 2494a668..17c90de3 100644 --- a/tsuru/client/services_test.go +++ b/tsuru/client/services_test.go @@ -155,6 +155,54 @@ func (s *S) TestServiceList(c *check.C) { } +func (s *S) TestServiceListWithTags(c *check.C) { + var stdout, stderr bytes.Buffer + output, err := json.Marshal([]service.ServiceModel{ + { + Service: "mysql", + ServiceInstances: []service.ServiceInstance{ + { + Name: "mysql01", + }, + }, + }, + }) + c.Assert(err, check.IsNil) + + ctx := cmd.Context{ + Args: []string{}, + Stdout: &stdout, + Stderr: &stderr, + } + + trans := cmdtest.ConditionalTransport{ + Transport: cmdtest.Transport{Message: string(output), Status: http.StatusOK}, + CondFunc: func(req *http.Request) bool { + err = req.ParseForm() + c.Assert(err, check.IsNil) + + tags := req.Form["tag"] + c.Assert(tags, check.DeepEquals, []string{"tag1", "tag2"}) + + return strings.HasSuffix(req.URL.Path, "/services/instances") + }, + } + s.setupFakeTransport(&trans) + command := ServiceList{} + command.Flags().Parse(true, []string{"--tag", "tag1", "--tag", "tag2"}) + err = command.Run(&ctx) + c.Assert(err, check.IsNil) + + table := stdout.String() + + c.Assert(table, check.Equals, `+---------+----------+ +| Service | Instance | ++---------+----------+ +| mysql | mysql01 | ++---------+----------+ +`) +} + func (s *S) TestServiceListWithPool(c *check.C) { var stdout, stderr bytes.Buffer output, err := json.Marshal([]service.ServiceModel{