(ctx context.Context, projectName string, options api.RemoveOptions)
| 28 | ) |
| 29 | |
| 30 | func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error { //nolint:gocyclo |
| 31 | projectName = strings.ToLower(projectName) |
| 32 | |
| 33 | if options.Stop { |
| 34 | err := s.Stop(ctx, projectName, api.StopOptions{ |
| 35 | Services: options.Services, |
| 36 | Project: options.Project, |
| 37 | }) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...) |
| 44 | if err != nil { |
| 45 | if api.IsNotFoundError(err) { |
| 46 | _, _ = fmt.Fprintln(s.stderr(), "No stopped containers") |
| 47 | return nil |
| 48 | } |
| 49 | return err |
| 50 | } |
| 51 | |
| 52 | if options.Project != nil { |
| 53 | containers = containers.filter(isService(options.Project.ServiceNames()...)) |
| 54 | } |
| 55 | |
| 56 | var stoppedContainers Containers |
| 57 | for _, ctr := range containers { |
| 58 | // We have to inspect containers, as State reported by getContainers suffers a race condition |
| 59 | inspected, err := s.apiClient().ContainerInspect(ctx, ctr.ID, client.ContainerInspectOptions{}) |
| 60 | if api.IsNotFoundError(err) { |
| 61 | // Already removed. Maybe configured with auto-remove |
| 62 | continue |
| 63 | } |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | if !inspected.Container.State.Running || (options.Stop && s.dryRun) { |
| 68 | stoppedContainers = append(stoppedContainers, ctr) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | var names []string |
| 73 | for _, c := range stoppedContainers { |
| 74 | names = append(names, getCanonicalContainerName(c)) |
| 75 | } |
| 76 | |
| 77 | if len(names) == 0 { |
| 78 | return api.ErrNoResources |
| 79 | } |
| 80 | |
| 81 | msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", ")) |
| 82 | if options.Force { |
| 83 | _, _ = fmt.Fprintln(s.stdout(), msg) |
| 84 | } else { |
| 85 | confirm, err := s.prompt(msg, false) |
| 86 | if err != nil { |
| 87 | return err |
nothing calls this directly
no test coverage detected