Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --tag flag to filter services in tsuru service list #234

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading