@@ -19,22 +19,18 @@ package compose
1919import (
2020 "context"
2121 "fmt"
22- "io"
2322 "sort"
24- "strconv"
2523 "strings"
26- "time"
2724
2825 "github.com/docker/compose/v2/cmd/formatter"
26+ "github.com/docker/compose/v2/pkg/api"
2927 "github.com/docker/compose/v2/pkg/utils"
30- "github.com/docker/docker/api/types"
3128
32- formatter2 "github.com/docker/cli/cli/command/formatter"
33- "github.com/docker/go-units"
29+ "github.com/docker/cli/cli/command"
30+ cliformatter "github.com/docker/cli/cli/command/formatter"
31+ cliflags "github.com/docker/cli/cli/flags"
3432 "github.com/pkg/errors"
3533 "github.com/spf13/cobra"
36-
37- "github.com/docker/compose/v2/pkg/api"
3834)
3935
4036type psOptions struct {
@@ -66,7 +62,7 @@ func (p *psOptions) parseFilter() error {
6662 return nil
6763}
6864
69- func psCommand (p * ProjectOptions , streams api. Streams , backend api.Service ) * cobra.Command {
65+ func psCommand (p * ProjectOptions , dockerCli command. Cli , backend api.Service ) * cobra.Command {
7066 opts := psOptions {
7167 ProjectOptions : p ,
7268 }
@@ -77,12 +73,12 @@ func psCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
7773 return opts .parseFilter ()
7874 },
7975 RunE : Adapt (func (ctx context.Context , args []string ) error {
80- return runPs (ctx , streams , backend , args , opts )
76+ return runPs (ctx , dockerCli , backend , args , opts )
8177 }),
8278 ValidArgsFunction : completeServiceNames (p ),
8379 }
8480 flags := psCmd .Flags ()
85- flags .StringVar (& opts .Format , "format" , "table" , "Format the output. Values: [table | json]" )
81+ flags .StringVar (& opts .Format , "format" , "table" , cliflags . FormatHelp )
8682 flags .StringVar (& opts .Filter , "filter" , "" , "Filter services by a property (supported filters: status)." )
8783 flags .StringArrayVar (& opts .Status , "status" , []string {}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]" )
8884 flags .BoolVarP (& opts .Quiet , "quiet" , "q" , false , "Only display IDs" )
@@ -91,7 +87,7 @@ func psCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
9187 return psCmd
9288}
9389
94- func runPs (ctx context.Context , streams api. Streams , backend api.Service , services []string , opts psOptions ) error {
90+ func runPs (ctx context.Context , dockerCli command. Cli , backend api.Service , services []string , opts psOptions ) error {
9591 project , name , err := opts .projectOrName (services ... )
9692 if err != nil {
9793 return err
@@ -125,38 +121,32 @@ func runPs(ctx context.Context, streams api.Streams, backend api.Service, servic
125121
126122 if opts .Quiet {
127123 for _ , c := range containers {
128- fmt .Fprintln (streams .Out (), c .ID )
124+ fmt .Fprintln (dockerCli .Out (), c .ID )
129125 }
130126 return nil
131127 }
132128
133129 if opts .Services {
134130 services := []string {}
135- for _ , s := range containers {
136- if ! utils .StringContains (services , s .Service ) {
137- services = append (services , s .Service )
131+ for _ , c := range containers {
132+ s := c .Service
133+ if ! utils .StringContains (services , s ) {
134+ services = append (services , s )
138135 }
139136 }
140- fmt .Fprintln (streams .Out (), strings .Join (services , "\n " ))
137+ fmt .Fprintln (dockerCli .Out (), strings .Join (services , "\n " ))
141138 return nil
142139 }
143140
144- return formatter .Print (containers , opts .Format , streams .Out (),
145- writer (containers ),
146- "NAME" , "IMAGE" , "COMMAND" , "SERVICE" , "CREATED" , "STATUS" , "PORTS" )
147- }
141+ if opts .Format == "" {
142+ opts .Format = dockerCli .ConfigFile ().PsFormat
143+ }
148144
149- func writer (containers []api.ContainerSummary ) func (w io.Writer ) {
150- return func (w io.Writer ) {
151- for _ , container := range containers {
152- ports := displayablePorts (container )
153- createdAt := time .Unix (container .Created , 0 )
154- created := units .HumanDuration (time .Now ().UTC ().Sub (createdAt )) + " ago"
155- status := container .Status
156- command := formatter2 .Ellipsis (container .Command , 20 )
157- _ , _ = fmt .Fprintf (w , "%s\t %s\t %s\t %s\t %s\t %s\t %s\n " , container .Name , container .Image , strconv .Quote (command ), container .Service , created , status , ports )
158- }
145+ containerCtx := cliformatter.Context {
146+ Output : dockerCli .Out (),
147+ Format : formatter .NewContainerFormat (opts .Format , opts .Quiet , false ),
159148 }
149+ return formatter .ContainerWrite (containerCtx , containers )
160150}
161151
162152func filterByStatus (containers []api.ContainerSummary , statuses []string ) []api.ContainerSummary {
@@ -177,21 +167,3 @@ func hasStatus(c api.ContainerSummary, statuses []string) bool {
177167 }
178168 return false
179169}
180-
181- func displayablePorts (c api.ContainerSummary ) string {
182- if c .Publishers == nil {
183- return ""
184- }
185-
186- ports := make ([]types.Port , len (c .Publishers ))
187- for i , pub := range c .Publishers {
188- ports [i ] = types.Port {
189- IP : pub .URL ,
190- PrivatePort : uint16 (pub .TargetPort ),
191- PublicPort : uint16 (pub .PublishedPort ),
192- Type : pub .Protocol ,
193- }
194- }
195-
196- return formatter2 .DisplayablePorts (ports )
197- }
0 commit comments