Skip to content

Commit

Permalink
feat: Add --tag flag to filter services in tsuru service list (#234)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
prbn97 authored Oct 9, 2024
1 parent 823d8f5 commit 83be8cd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tsuru/client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type serviceFilter struct {
plan string
service string
teamOwner string
tags cmd.StringSliceFlag
}

func (f *serviceFilter) queryString() (url.Values, error) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions tsuru/client/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 83be8cd

Please sign in to comment.