@@ -19,22 +19,18 @@ package compose
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "io"
23
22
"sort"
24
- "strconv"
25
23
"strings"
26
- "time"
27
24
28
25
"github.com/docker/compose/v2/cmd/formatter"
26
+ "github.com/docker/compose/v2/pkg/api"
29
27
"github.com/docker/compose/v2/pkg/utils"
30
- "github.com/docker/docker/api/types"
31
28
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"
34
32
"github.com/pkg/errors"
35
33
"github.com/spf13/cobra"
36
-
37
- "github.com/docker/compose/v2/pkg/api"
38
34
)
39
35
40
36
type psOptions struct {
@@ -66,7 +62,7 @@ func (p *psOptions) parseFilter() error {
66
62
return nil
67
63
}
68
64
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 {
70
66
opts := psOptions {
71
67
ProjectOptions : p ,
72
68
}
@@ -77,12 +73,12 @@ func psCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
77
73
return opts .parseFilter ()
78
74
},
79
75
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 )
81
77
}),
82
78
ValidArgsFunction : completeServiceNames (p ),
83
79
}
84
80
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 )
86
82
flags .StringVar (& opts .Filter , "filter" , "" , "Filter services by a property (supported filters: status)." )
87
83
flags .StringArrayVar (& opts .Status , "status" , []string {}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]" )
88
84
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
91
87
return psCmd
92
88
}
93
89
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 {
95
91
project , name , err := opts .projectOrName (services ... )
96
92
if err != nil {
97
93
return err
@@ -125,38 +121,32 @@ func runPs(ctx context.Context, streams api.Streams, backend api.Service, servic
125
121
126
122
if opts .Quiet {
127
123
for _ , c := range containers {
128
- fmt .Fprintln (streams .Out (), c .ID )
124
+ fmt .Fprintln (dockerCli .Out (), c .ID )
129
125
}
130
126
return nil
131
127
}
132
128
133
129
if opts .Services {
134
130
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 )
138
135
}
139
136
}
140
- fmt .Fprintln (streams .Out (), strings .Join (services , "\n " ))
137
+ fmt .Fprintln (dockerCli .Out (), strings .Join (services , "\n " ))
141
138
return nil
142
139
}
143
140
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
+ }
148
144
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 ),
159
148
}
149
+ return formatter .ContainerWrite (containerCtx , containers )
160
150
}
161
151
162
152
func filterByStatus (containers []api.ContainerSummary , statuses []string ) []api.ContainerSummary {
@@ -177,21 +167,3 @@ func hasStatus(c api.ContainerSummary, statuses []string) bool {
177
167
}
178
168
return false
179
169
}
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