Browse Source

don't fail trying to remove container with no candidate

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 3 years ago
parent
commit
5e8040ea18
3 changed files with 11 additions and 2 deletions
  1. 1 1
      pkg/compose/compose.go
  2. 5 1
      pkg/compose/remove.go
  3. 5 0
      pkg/e2e/compose_test.go

+ 1 - 1
pkg/compose/compose.go

@@ -165,7 +165,7 @@ SERVICES:
 				continue 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)
 	err := project.ForServices(services)
 	if err != nil {
 	if err != nil {

+ 5 - 1
pkg/compose/remove.go

@@ -32,6 +32,10 @@ import (
 func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error {
 func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error {
 	containers, _, err := s.actualState(ctx, projectName, options.Services)
 	containers, _, err := s.actualState(ctx, projectName, options.Services)
 	if err != nil {
 	if err != nil {
+		if api.IsNotFoundError(err) {
+			fmt.Fprintln(s.stderr(), "No stopped containers")
+			return nil
+		}
 		return err
 		return err
 	}
 	}
 
 
@@ -45,7 +49,7 @@ func (s *composeService) Remove(ctx context.Context, projectName string, options
 	})
 	})
 
 
 	if len(names) == 0 {
 	if len(names) == 0 {
-		fmt.Println("No stopped containers")
+		fmt.Fprintln(s.stderr(), "No stopped containers")
 		return nil
 		return nil
 	}
 	}
 	msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
 	msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))

+ 5 - 0
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())
 		assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
 	})
 	})
 
 
+	t.Run("rm -sf <none>", 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) {
 	t.Run("down", func(t *testing.T) {
 		c.RunDockerComposeCmd("-p", projectName, "down")
 		c.RunDockerComposeCmd("-p", projectName, "down")
 	})
 	})