Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove many and removeForce #19

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.16

require (
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8
github.com/go-test/deep v1.0.7
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
18 changes: 14 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
}

type options struct {
force bool
}

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

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

return docker.Remove(ctx, container)
return docker.Remove(ctx, container, docker.WithForceRemove(options.force))
}
23 changes: 16 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
}

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

func run(ctx context.Context, options options) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
Expand All @@ -44,17 +54,16 @@ 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)
}

return nil
return docker.RemoveMany(ctx, unreferencedContainers, docker.WithForceRemove(options.force))
}
75 changes: 75 additions & 0 deletions src/fixture_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"bytes"
"context"
"io"
"os"
"os/exec"
"testing"

"github.com/Netflix/go-expect"
"github.com/hinshun/vt10x"
"github.com/stretchr/testify/require"
)

var rootDir = "_test_assets_"

type ConsoleTester struct {
c *expect.Console
r *require.Assertions
}

func (ct ConsoleTester) ExpectString(value string) {
_, err := ct.c.ExpectString(value)
ct.r.NoError(err)
}

func (ct ConsoleTester) ExpectEOF() {
_, err := ct.c.ExpectEOF()
ct.r.NoError(err)
}

func (ct ConsoleTester) SendLine(value string) {
_, err := ct.c.SendLine(value)
ct.r.NoError(err)
}

func ConsoleTest(t *testing.T, ctx context.Context, cmdArgs []string, test func(ct ConsoleTester)) ([]byte, error) {
t.Helper()
r := require.New(t)

buf := new(bytes.Buffer)
console, _, err := vt10x.NewVT10XConsole(expect.WithStdout(buf))
require.NoError(t, err)
ct := ConsoleTester{console, r}

cmd := exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...)
cmd.Dir = rootDir

pr, pw := io.Pipe()
tty := console.Tty()

go func() {
r := io.TeeReader(pr, os.Stdout)
io.Copy(tty, r)
}()

cmd.Stdout, cmd.Stderr, cmd.Stdin = pw, pw, console.Tty()

donec := make(chan struct{})
go func() {
defer close(donec)
if test != nil {
test(ct)
}
}()

err = cmd.Run()

// Close the slave end of the tty, and read the remaining bytes from the master end.
console.Tty().Close()
<-donec

return buf.Bytes(), err
}
8 changes: 8 additions & 0 deletions src/internal/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func TestClone(t *testing.T) {
"/local/mount1": "/container/mount1",
"/local/mount2": "/container/mount2",
},
Build: DockerBuild{
Dockerfile: "./Dockerfile",
Args: map[string]string{
"arg1": "value1",
"arg2": "value2",
},
Context: ".",
},
}

clone := original.Clone()
Expand Down
35 changes: 33 additions & 2 deletions src/internal/docker/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@ func Start(ctx context.Context, yeyCtx yey.Context, containerName string, opts .
}
}

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

type RemoveOption func(ro *removeOption)

func WithForceRemove(value bool) RemoveOption {
return func(ro *removeOption) {
ro.force = value
}
}

func Remove(ctx context.Context, containerName string, options ...RemoveOption) error {
status, err := getContainerStatus(ctx, containerName)
if err != nil {
return err
Expand All @@ -60,7 +72,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