Skip to content

Commit

Permalink
refactor: flag to filter services instances
Browse files Browse the repository at this point in the history
  • Loading branch information
prbn021 committed Oct 3, 2024
1 parent b80d0d9 commit 5f90c74
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
5 changes: 5 additions & 0 deletions tsuru/client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,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 @@ -188,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
15 changes: 5 additions & 10 deletions tsuru/client/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,14 @@ func (s *S) TestServiceListWithTags(c *check.C) {
ServiceInstances: []service.ServiceInstance{
{
Name: "mysql01",
Tags: []string{"production"},
},
},
},
})
c.Assert(err, check.IsNil)

ctx := cmd.Context{
Args: []string{"--tag", "production"},
Args: []string{},
Stdout: &stdout,
Stderr: &stderr,
}
Expand All @@ -183,19 +182,15 @@ func (s *S) TestServiceListWithTags(c *check.C) {
c.Assert(err, check.IsNil)

tags := req.Form["tag"]
c.Assert(tags, check.DeepEquals, []string{"production"})
c.Assert(tags, check.DeepEquals, []string{"tag1", "--tag"})

return strings.HasSuffix(req.URL.Path, "/services/instances")
},
}
serviceList := &ServiceList{
filter: serviceFilter{
tags: cmd.StringSliceFlag{"production"},
},
}
s.setupFakeTransport(&trans)

err = serviceList.Run(&ctx)
command := ServiceList{}
command.Flags().Parse(true, []string{"--tag", "tag1", "--tag", "tag2"})
err = command.Run(&ctx)
c.Assert(err, check.IsNil)

table := stdout.String()
Expand Down

0 comments on commit 5f90c74

Please sign in to comment.