Merge pull request #1144 from docker/down_with_file

run "down" using project if it was set for command
This commit is contained in:
Nicolas De loof 2021-01-18 10:53:46 +01:00 committed by GitHub
commit 268b3f65b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 47 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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()
name := opts.ProjectName
var project *types.Project
if opts.ProjectName == "" {
p, err := opts.toProject()
if err != nil {
return "", err
}
return projectName, c.ComposeService().Down(ctx, projectName, compose.DownOptions{
project = p
name = p.Name
}
return name, c.ComposeService().Down(ctx, name, compose.DownOptions{
RemoveOrphans: false,
Project: project,
})
})
return err

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -36,21 +36,24 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
eg, _ := errgroup.WithContext(ctx)
w := progress.ContextWriter(ctx)
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