Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit dc790d5

Browse files
authored
Merge pull request #1172 from docker/ls_filters
introduce `--filter` option on `compose ls`
2 parents 2dc0616 + f33eff5 commit dc790d5

File tree

12 files changed

+48
-30
lines changed

12 files changed

+48
-30
lines changed

aci/compose.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,17 @@ func (cs *aciComposeService) Ps(ctx context.Context, project string) ([]compose.
163163
return res, nil
164164
}
165165

166-
func (cs *aciComposeService) List(ctx context.Context, project string) ([]compose.Stack, error) {
166+
func (cs *aciComposeService) List(ctx context.Context) ([]compose.Stack, error) {
167167
containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
168168
if err != nil {
169169
return nil, err
170170
}
171171

172-
stacks := []compose.Stack{}
172+
var stacks []compose.Stack
173173
for _, group := range containerGroups {
174174
if _, found := group.Tags[composeContainerTag]; !found {
175175
continue
176176
}
177-
if project != "" && *group.Name != project {
178-
continue
179-
}
180177
state := compose.RUNNING
181178
for _, container := range *group.ContainerGroupProperties.Containers {
182179
containerState := convert.GetStatus(container, group)

api/client/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (c *composeService) Ps(context.Context, string) ([]compose.ContainerSummary
6464
return nil, errdefs.ErrNotImplemented
6565
}
6666

67-
func (c *composeService) List(context.Context, string) ([]compose.Stack, error) {
67+
func (c *composeService) List(context.Context) ([]compose.Stack, error) {
6868
return nil, errdefs.ErrNotImplemented
6969
}
7070

api/compose/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type Service interface {
4444
// Ps executes the equivalent to a `compose ps`
4545
Ps(ctx context.Context, projectName string) ([]ContainerSummary, error)
4646
// List executes the equivalent to a `docker stack ls`
47-
List(ctx context.Context, projectName string) ([]Stack, error)
47+
List(ctx context.Context) ([]Stack, error)
4848
// Convert translate compose model into backend's native format
4949
Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
5050
// RunOneOffContainer creates a service oneoff container and starts its dependencies

cli/cmd/compose/list.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"strings"
2525

26+
"github.com/docker/cli/opts"
2627
"github.com/spf13/cobra"
2728

2829
"github.com/docker/compose-cli/api/client"
@@ -33,10 +34,11 @@ import (
3334
type lsOptions struct {
3435
Format string
3536
Quiet bool
37+
Filter opts.FilterOpt
3638
}
3739

3840
func listCommand() *cobra.Command {
39-
opts := lsOptions{}
41+
opts := lsOptions{Filter: opts.NewFilterOpt()}
4042
lsCmd := &cobra.Command{
4143
Use: "ls",
4244
Short: "List running compose projects",
@@ -45,16 +47,28 @@ func listCommand() *cobra.Command {
4547
},
4648
}
4749
lsCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
48-
lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
50+
lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs.")
51+
lsCmd.Flags().Var(&opts.Filter, "filter", "Filter output based on conditions provided.")
52+
4953
return lsCmd
5054
}
5155

56+
var acceptedListFilters = map[string]bool{
57+
"name": true,
58+
}
59+
5260
func runList(ctx context.Context, opts lsOptions) error {
61+
filters := opts.Filter.Value()
62+
err := filters.Validate(acceptedListFilters)
63+
if err != nil {
64+
return err
65+
}
66+
5367
c, err := client.NewWithDefaultLocalBackend(ctx)
5468
if err != nil {
5569
return err
5670
}
57-
stackList, err := c.ComposeService().List(ctx, "")
71+
stackList, err := c.ComposeService().List(ctx)
5872
if err != nil {
5973
return err
6074
}
@@ -64,6 +78,18 @@ func runList(ctx context.Context, opts lsOptions) error {
6478
}
6579
return nil
6680
}
81+
82+
if filters.Len() > 0 {
83+
var filtered []compose.Stack
84+
for _, s := range stackList {
85+
if filters.Contains("name") && !filters.Match("name", s.Name) {
86+
continue
87+
}
88+
filtered = append(filtered, s)
89+
}
90+
stackList = filtered
91+
}
92+
6793
view := viewFromStackList(stackList)
6894
return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
6995
for _, stack := range view {

cli/server/proxy/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServices
7777
}
7878

7979
func (p *proxy) Stacks(ctx context.Context, request *composev1.ComposeStacksRequest) (*composev1.ComposeStacksResponse, error) {
80-
stacks, err := Client(ctx).ComposeService().List(ctx, request.ProjectName)
80+
stacks, err := Client(ctx).ComposeService().List(ctx)
8181
if err != nil {
8282
return nil, err
8383
}

ecs/aws.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type API interface {
4949
UpdateStack(ctx context.Context, changeset string) error
5050
WaitStackComplete(ctx context.Context, name string, operation int) error
5151
GetStackID(ctx context.Context, name string) (string, error)
52-
ListStacks(ctx context.Context, name string) ([]compose.Stack, error)
52+
ListStacks(ctx context.Context) ([]compose.Stack, error)
5353
GetStackClusterID(ctx context.Context, stack string) (string, error)
5454
GetServiceTaskDefinition(ctx context.Context, cluster string, serviceArns []string) (map[string]string, error)
5555
ListStackServices(ctx context.Context, stack string) ([]string, error)

ecs/aws_mock.go

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecs/e2e/ecs/e2e-ecs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestCompose(t *testing.T) {
122122
})
123123

124124
t.Run("compose ls", func(t *testing.T) {
125-
res := c.RunDockerCmd("compose", "ls", "--project-name", stack)
125+
res := c.RunDockerCmd("compose", "ls", "--filter", "name="+stack)
126126
lines := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
127127

128128
assert.Equal(t, 2, len(lines))

ecs/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"github.com/docker/compose-cli/api/compose"
2424
)
2525

26-
func (b *ecsAPIService) List(ctx context.Context, project string) ([]compose.Stack, error) {
27-
stacks, err := b.aws.ListStacks(ctx, project)
26+
func (b *ecsAPIService) List(ctx context.Context) ([]compose.Stack, error) {
27+
stacks, err := b.aws.ListStacks(ctx)
2828
if err != nil {
2929
return nil, err
3030
}

ecs/local/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consum
161161
func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string) ([]compose.ContainerSummary, error) {
162162
return e.compose.Ps(ctx, projectName)
163163
}
164-
func (e ecsLocalSimulation) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
165-
return e.compose.List(ctx, projectName)
164+
func (e ecsLocalSimulation) List(ctx context.Context) ([]compose.Stack, error) {
165+
return e.compose.List(ctx)
166166
}
167167
func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
168168
return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")

0 commit comments

Comments
 (0)