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

Commit

Permalink
introduce --rmi and --volumes option on down
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Feb 17, 2021
1 parent 65fc4ff commit f07a4a7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
4 changes: 4 additions & 0 deletions api/compose/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type DownOptions struct {
Project *types.Project
// Timeout override container stop timeout
Timeout *time.Duration
// Images remove image used by services. 'all': Remove all images. 'local': Remove only images that don't have a tag
Images string
// Volumes remove volumes, both declared in the `volumes` section and anonymous ones
Volumes bool
}

// ConvertOptions group options of the Convert API
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func Command(contextType string) *cobra.Command {

command.AddCommand(
upCommand(&opts, contextType),
downCommand(&opts),
downCommand(&opts, contextType),
startCommand(&opts),
stopCommand(&opts),
psCommand(&opts),
Expand Down
18 changes: 17 additions & 1 deletion cli/cmd/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package compose

import (
"context"
"fmt"
"github.com/docker/compose-cli/api/context/store"
"time"

"github.com/compose-spec/compose-go/types"
Expand All @@ -35,9 +37,11 @@ type downOptions struct {
removeOrphans bool
timeChanged bool
timeout int
volumes bool
images string
}

func downCommand(p *projectOptions) *cobra.Command {
func downCommand(p *projectOptions, contextType string) *cobra.Command {
opts := downOptions{
projectOptions: p,
}
Expand All @@ -46,13 +50,23 @@ func downCommand(p *projectOptions) *cobra.Command {
Short: "Stop and remove containers, networks",
RunE: func(cmd *cobra.Command, args []string) error {
opts.timeChanged = cmd.Flags().Changed("timeout")
if opts.images != "" {
if opts.images != "all" && opts.images != "local" {
return fmt.Errorf("invalid value for --rmi: %q", opts.images)
}
}
return runDown(cmd.Context(), opts)
},
}
flags := downCmd.Flags()
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")

switch contextType {
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
}
return downCmd
}

Expand Down Expand Up @@ -83,6 +97,8 @@ func runDown(ctx context.Context, opts downOptions) error {
RemoveOrphans: opts.removeOrphans,
Project: project,
Timeout: timeout,
Images: opts.images,
Volumes: opts.volumes,
})
})
return err
Expand Down
Loading

0 comments on commit f07a4a7

Please sign in to comment.