From 5e8040ea18230db5d8a96c3168cd2fc2dc5d03fb Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 9 Mar 2022 16:20:02 +0100 Subject: [PATCH] don't fail trying to remove container with no candidate Signed-off-by: Nicolas De Loof --- pkg/compose/compose.go | 2 +- pkg/compose/remove.go | 6 +++++- pkg/e2e/compose_test.go | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 6dc9b387a..482e4832a 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -165,7 +165,7 @@ SERVICES: continue SERVICES } } - return project, errors.New("no such service: " + qs) + return project, errors.Wrapf(api.ErrNotFound, "no such service: %q", qs) } err := project.ForServices(services) if err != nil { diff --git a/pkg/compose/remove.go b/pkg/compose/remove.go index bba89af67..c5dd3dd84 100644 --- a/pkg/compose/remove.go +++ b/pkg/compose/remove.go @@ -32,6 +32,10 @@ import ( func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error { containers, _, err := s.actualState(ctx, projectName, options.Services) if err != nil { + if api.IsNotFoundError(err) { + fmt.Fprintln(s.stderr(), "No stopped containers") + return nil + } return err } @@ -45,7 +49,7 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options }) if len(names) == 0 { - fmt.Println("No stopped containers") + fmt.Fprintln(s.stderr(), "No stopped containers") return nil } msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", ")) diff --git a/pkg/e2e/compose_test.go b/pkg/e2e/compose_test.go index 98415aa52..d3497b7d9 100644 --- a/pkg/e2e/compose_test.go +++ b/pkg/e2e/compose_test.go @@ -188,6 +188,11 @@ func TestRm(t *testing.T) { assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined()) }) + t.Run("rm -sf ", func(t *testing.T) { + res := c.RunDockerComposeCmd("-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm", "-sf", "simple") + res.Assert(t, icmd.Expected{ExitCode: 0}) + }) + t.Run("down", func(t *testing.T) { c.RunDockerComposeCmd("-p", projectName, "down") })