Skip to content

Commit

Permalink
remove many and removeForce
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed May 31, 2021
1 parent f9b31c4 commit f383d1c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
23 changes: 19 additions & 4 deletions src/cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (

// New creates a cobra command
func New() *cobra.Command {
return &cobra.Command{
var options options

cmd := &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Short: "Removes context container",
Expand All @@ -23,12 +25,20 @@ func New() *cobra.Command {
if len(args) == 1 {
name = args[0]
}
return run(cmd.Context(), name)
return run(cmd.Context(), name, options)
},
}

cmd.Flags().BoolVarP(&options.force, "force", "f", false, "force removes container")

return cmd
}

func run(ctx context.Context, name string) error {
type options struct {
force bool
}

func run(ctx context.Context, name string, options options) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
Expand All @@ -49,5 +59,10 @@ func run(ctx context.Context, name string) error {

container := yey.ContainerName(contexts.Path, context)

return docker.Remove(ctx, container)
var removeOptions []docker.RemoveOption
if options.force {
removeOptions = append(removeOptions, docker.ForceRemove)
}

return docker.Remove(ctx, container, removeOptions...)
}
28 changes: 21 additions & 7 deletions src/cmd/tidy/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ import (

// New creates a cobra command
func New() *cobra.Command {
return &cobra.Command{
var options options

cmd := &cobra.Command{
Use: "tidy",
Short: "Removes unreferenced containers",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd.Context())
return run(cmd.Context(), options)
},
}

cmd.Flags().BoolVarP(&options.force, "force", "f", false, "removes containers forcibly")

return cmd
}

type options struct {
force bool
}

func run(ctx context.Context) error {
func run(ctx context.Context, options options) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
Expand All @@ -44,17 +54,21 @@ func run(ctx context.Context) error {
return err
}

var unreferencedContainers []string
for _, container := range names {
if !strings.HasPrefix(container, prefix) {
continue
}
if _, ok := validNames[container]; ok {
continue
}
if err := docker.Remove(ctx, container); err != nil {
return err
}
unreferencedContainers = append(unreferencedContainers, container)
}

var removeOptions []docker.RemoveOption
if options.force {
removeOptions = append(removeOptions, docker.ForceRemove)
}

return nil
return docker.RemoveMany(ctx, unreferencedContainers, removeOptions...)
}
31 changes: 29 additions & 2 deletions src/internal/docker/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ func Start(ctx context.Context, yeyCtx yey.Context, containerName string) error
}
}

func Remove(ctx context.Context, containerName string) error {
type removeOption struct {
force bool
}

type RemoveOption func(ro *removeOption)

var ForceRemove RemoveOption = func(ro *removeOption) { ro.force = true }

func Remove(ctx context.Context, containerName string, options ...RemoveOption) error {
status, err := getContainerStatus(ctx, containerName)
if err != nil {
return err
Expand All @@ -44,7 +52,26 @@ func Remove(ctx context.Context, containerName string) error {
return nil
}

return attachStdPipes(exec.CommandContext(ctx, "docker", "rm", "-v", containerName)).Run()
return RemoveMany(ctx, []string{containerName}, options...)
}

func RemoveMany(ctx context.Context, containers []string, options ...RemoveOption) error {
if len(containers) == 0 {
return nil
}

var opts removeOption
for _, opt := range options {
opt(&opts)
}

args := []string{"rm", "-v"}
if opts.force {
args = append(args, "-f")
}
args = append(args, containers...)

return attachStdPipes(exec.CommandContext(ctx, "docker", args...)).Run()
}

func Build(ctx context.Context, dockerPath string, imageTag string, buildArgs map[string]string, context string) error {
Expand Down

0 comments on commit f383d1c

Please sign in to comment.