From 83be8cde51a32a97bc17a4cc66aa476bb0e1083c Mon Sep 17 00:00:00 2001 From: Paulo Reis <110939784+prbn97@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:44:57 -0300 Subject: [PATCH] feat: Add --tag flag to filter services in tsuru service list (#234) * feat: Add --tag flag to filter services in tsuru service list * refactor: flag to filter services instances * fix(tests): syntax error and shadowed declaration in service list test --- tsuru/client/services.go | 13 +++++++++- tsuru/client/services_test.go | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) 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{