(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, services []string, opts psOptions)
| 93 | } |
| 94 | |
| 95 | func runPs(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, services []string, opts psOptions) error { //nolint:gocyclo |
| 96 | project, name, err := opts.projectOrName(ctx, dockerCli, services...) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | if project != nil { |
| 102 | names := project.ServiceNames() |
| 103 | if len(services) > 0 { |
| 104 | for _, service := range services { |
| 105 | if !slices.Contains(names, service) { |
| 106 | return fmt.Errorf("no such service: %s", service) |
| 107 | } |
| 108 | } |
| 109 | } else if !opts.Orphans { |
| 110 | // until user asks to list orphaned services, we only include those declared in project |
| 111 | services = names |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | containers, err := backend.Ps(ctx, name, api.PsOptions{ |
| 120 | Project: project, |
| 121 | All: opts.All || len(opts.Status) != 0, |
| 122 | Services: services, |
| 123 | }) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | if len(opts.Status) != 0 { |
| 129 | containers = filterByStatus(containers, opts.Status) |
| 130 | } |
| 131 | |
| 132 | sort.Slice(containers, func(i, j int) bool { |
| 133 | return containers[i].Name < containers[j].Name |
| 134 | }) |
| 135 | |
| 136 | if opts.Quiet { |
| 137 | for _, c := range containers { |
| 138 | _, _ = fmt.Fprintln(dockerCli.Out(), c.ID) |
| 139 | } |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | if opts.Services { |
| 144 | services := []string{} |
| 145 | for _, c := range containers { |
| 146 | s := c.Service |
| 147 | if !slices.Contains(services, s) { |
| 148 | services = append(services, s) |
| 149 | } |
| 150 | } |
| 151 | _, _ = fmt.Fprintln(dockerCli.Out(), strings.Join(services, "\n")) |
| 152 | return nil |
no test coverage detected