From e4682a05659056604e60d54cb4027cba0c089422 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 14 Jan 2021 15:59:57 +0100 Subject: [PATCH] run "down" using project if it was set for command Signed-off-by: Nicolas De Loof --- api/compose/api.go | 2 ++ cli/cmd/compose/build.go | 7 +------ cli/cmd/compose/compose.go | 20 ++++++++++++++------ cli/cmd/compose/convert.go | 8 +------- cli/cmd/compose/down.go | 18 ++++++++++++++---- cli/cmd/compose/pull.go | 7 +------ cli/cmd/compose/push.go | 7 +------ cli/cmd/compose/up.go | 7 +------ local/compose/down.go | 15 +++++++++------ 9 files changed, 44 insertions(+), 47 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index 72f3517e4..19a535b22 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -67,6 +67,8 @@ type UpOptions struct { type DownOptions struct { // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels RemoveOrphans bool + // Project is the compose project used to define this app. Might be nil if user ran `down` just with project name + Project *types.Project } // ConvertOptions group options of the Convert API diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index f2a82575c..2389f939e 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -19,7 +19,6 @@ package compose import ( "context" - "github.com/compose-spec/compose-go/cli" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -52,11 +51,7 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error { } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - options, err := opts.toProjectOptions() - if err != nil { - return "", err - } - project, err := cli.ProjectFromOptions(options) + project, err := opts.toProject() if err != nil { return "", err } diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 87f4ed0bd..7f9b44fa7 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -50,18 +50,26 @@ func (o *composeOptions) toProjectName() (string, error) { return o.ProjectName, nil } - options, err := o.toProjectOptions() - if err != nil { - return "", err - } - - project, err := cli.ProjectFromOptions(options) + project, err := o.toProject() if err != nil { return "", err } return project.Name, nil } +func (o *composeOptions) toProject() (*types.Project, error) { + options, err := o.toProjectOptions() + if err != nil { + return nil, err + } + + project, err := cli.ProjectFromOptions(options) + if err != nil { + return nil, err + } + return project, nil +} + func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) { return cli.NewProjectOptions(o.ConfigPaths, cli.WithOsEnv, diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go index 57a77bec8..8085b5f78 100644 --- a/cli/cmd/compose/convert.go +++ b/cli/cmd/compose/convert.go @@ -22,7 +22,6 @@ import ( "github.com/docker/compose-cli/api/compose" - "github.com/compose-spec/compose-go/cli" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -53,12 +52,7 @@ func runConvert(ctx context.Context, opts composeOptions) error { return err } - options, err := opts.toProjectOptions() - if err != nil { - return err - } - - project, err := cli.ProjectFromOptions(options) + project, err := opts.toProject() if err != nil { return err } diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index cfa3c43e7..dec17e311 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -19,6 +19,8 @@ package compose import ( "context" + "github.com/compose-spec/compose-go/types" + "github.com/docker/compose-cli/api/compose" "github.com/spf13/cobra" @@ -50,12 +52,20 @@ func runDown(ctx context.Context, opts composeOptions) error { } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - projectName, err := opts.toProjectName() - if err != nil { - return "", err + name := opts.ProjectName + var project *types.Project + if opts.ProjectName == "" { + p, err := opts.toProject() + if err != nil { + return "", err + } + project = p + name = p.Name } - return projectName, c.ComposeService().Down(ctx, projectName, compose.DownOptions{ + + return name, c.ComposeService().Down(ctx, name, compose.DownOptions{ RemoveOrphans: false, + Project: project, }) }) return err diff --git a/cli/cmd/compose/pull.go b/cli/cmd/compose/pull.go index 02243c34a..154cd6031 100644 --- a/cli/cmd/compose/pull.go +++ b/cli/cmd/compose/pull.go @@ -19,7 +19,6 @@ package compose import ( "context" - "github.com/compose-spec/compose-go/cli" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -53,11 +52,7 @@ func runPull(ctx context.Context, opts pullOptions, services []string) error { } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - options, err := opts.toProjectOptions() - if err != nil { - return "", err - } - project, err := cli.ProjectFromOptions(options) + project, err := opts.toProject() if err != nil { return "", err } diff --git a/cli/cmd/compose/push.go b/cli/cmd/compose/push.go index 86f92e8c5..49b028ab8 100644 --- a/cli/cmd/compose/push.go +++ b/cli/cmd/compose/push.go @@ -19,7 +19,6 @@ package compose import ( "context" - "github.com/compose-spec/compose-go/cli" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -53,11 +52,7 @@ func runPush(ctx context.Context, opts pushOptions, services []string) error { } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - options, err := opts.toProjectOptions() - if err != nil { - return "", err - } - project, err := cli.ProjectFromOptions(options) + project, err := opts.toProject() if err != nil { return "", err } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 6fbfa0196..6d0dfb5ac 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -28,7 +28,6 @@ import ( "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/cli/formatter" - "github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/types" "github.com/spf13/cobra" ) @@ -118,11 +117,7 @@ func setup(ctx context.Context, opts composeOptions, services []string) (*client return nil, nil, err } - options, err := opts.toProjectOptions() - if err != nil { - return nil, nil, err - } - project, err := cli.ProjectFromOptions(options) + project, err := opts.toProject() if err != nil { return nil, nil, err } diff --git a/local/compose/down.go b/local/compose/down.go index b104194c2..4bfcbd323 100644 --- a/local/compose/down.go +++ b/local/compose/down.go @@ -36,21 +36,24 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c eg, _ := errgroup.WithContext(ctx) w := progress.ContextWriter(ctx) - project, err := s.projectFromContainerLabels(ctx, projectName) - if err != nil { - return err + if options.Project == nil { + project, err := s.projectFromContainerLabels(ctx, projectName) + if err != nil { + return err + } + options.Project = project } var containers Containers - containers, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(project.Name)), + containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ + Filters: filters.NewArgs(projectFilter(options.Project.Name)), All: true, }) if err != nil { return err } - err = InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { + err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error { serviceContainers, others := containers.split(isService(service.Name)) s.removeContainers(ctx, w, eg, serviceContainers) containers = others