Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 19fa764

Browse files
authored
Merge pull request #361 from vdemeester/fix-master-ps-order
Fixing InfoSet String methods order
2 parents 81244f2 + baba785 commit 19fa764

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

cli/app/app.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func BeforeApp(c *cli.Context) error {
3232
if c.GlobalBool("verbose") {
3333
logrus.SetLevel(logrus.DebugLevel)
3434
}
35+
3536
if version.ShowWarning() {
3637
logrus.Warning("Note: This is an experimental alternate implementation of the Compose CLI (https://github.com/docker/compose)")
3738
}
@@ -52,11 +53,15 @@ func WithProject(factory ProjectFactory, action ProjectAction) func(context *cli
5253
// ProjectPs lists the containers.
5354
func ProjectPs(p project.APIProject, c *cli.Context) error {
5455
qFlag := c.Bool("q")
55-
allInfo, err := p.Ps(context.Background(), qFlag, c.Args()...)
56+
allInfo, err := p.Ps(context.Background(), c.Args()...)
5657
if err != nil {
5758
return cli.NewExitError(err.Error(), 1)
5859
}
59-
os.Stdout.WriteString(allInfo.String(!qFlag))
60+
columns := []string{"Name", "Command", "State", "Ports"}
61+
if qFlag {
62+
columns = []string{"Id"}
63+
}
64+
os.Stdout.WriteString(allInfo.String(columns, !qFlag))
6065
return nil
6166
}
6267

docker/container/container.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewInspected(client client.ContainerAPIClient, container *types.ContainerJS
6565
}
6666

6767
// Info returns info about the container, like name, command, state or ports.
68-
func (c *Container) Info(ctx context.Context, qFlag bool) (project.Info, error) {
68+
func (c *Container) Info(ctx context.Context) (project.Info, error) {
6969
infos, err := ListByFilter(ctx, c.client, map[string][]string{
7070
"name": {c.container.Name},
7171
})
@@ -75,14 +75,11 @@ func (c *Container) Info(ctx context.Context, qFlag bool) (project.Info, error)
7575
info := infos[0]
7676

7777
result := project.Info{}
78-
if qFlag {
79-
result["Id"] = c.container.ID
80-
} else {
81-
result["Name"] = name(info.Names)
82-
result["Command"] = info.Command
83-
result["State"] = info.Status
84-
result["Ports"] = portString(info.Ports)
85-
}
78+
result["Id"] = c.container.ID
79+
result["Name"] = name(info.Names)
80+
result["Command"] = info.Command
81+
result["State"] = info.Status
82+
result["Ports"] = portString(info.Ports)
8683

8784
return result, nil
8885
}

docker/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ func (s *Service) Run(ctx context.Context, commandParts []string, options option
290290

291291
// Info implements Service.Info. It returns an project.InfoSet with the containers
292292
// related to this service (can be multiple if using the scale command).
293-
func (s *Service) Info(ctx context.Context, qFlag bool) (project.InfoSet, error) {
293+
func (s *Service) Info(ctx context.Context) (project.InfoSet, error) {
294294
result := project.InfoSet{}
295295
containers, err := s.collectContainers(ctx)
296296
if err != nil {
297297
return nil, err
298298
}
299299

300300
for _, c := range containers {
301-
info, err := c.Info(ctx, qFlag)
301+
info, err := c.Info(ctx)
302302
if err != nil {
303303
return nil, err
304304
}

project/empty.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (e *EmptyService) Scale(ctx context.Context, count int, timeout int) error
7777
}
7878

7979
// Info implements Service.Info but does nothing.
80-
func (e *EmptyService) Info(ctx context.Context, qFlag bool) (InfoSet, error) {
80+
func (e *EmptyService) Info(ctx context.Context) (InfoSet, error) {
8181
return InfoSet{}, nil
8282
}
8383

@@ -133,7 +133,7 @@ func (e *EmptyNetworks) Initialize(ctx context.Context) error {
133133
return nil
134134
}
135135

136-
// Initialize implements Networks.Remove but does nothing.
136+
// Remove implements Networks.Remove but does nothing.
137137
func (e *EmptyNetworks) Remove(ctx context.Context) error {
138138
return nil
139139
}

project/info.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ type InfoSet []Info
1212
// Info holds a list of InfoPart.
1313
type Info map[string]string
1414

15-
func (infos InfoSet) String(titleFlag bool) string {
15+
func (infos InfoSet) String(columns []string, titleFlag bool) string {
1616
//no error checking, none of this should fail
1717
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
1818
tabwriter := tabwriter.NewWriter(buffer, 4, 4, 2, ' ', 0)
1919

2020
first := true
2121
for _, info := range infos {
2222
if first && titleFlag {
23-
writeLine(tabwriter, true, info)
23+
writeLine(tabwriter, columns, true, info)
2424
}
2525
first = false
26-
writeLine(tabwriter, false, info)
26+
writeLine(tabwriter, columns, false, info)
2727
}
2828

2929
tabwriter.Flush()
3030
return buffer.String()
3131
}
3232

33-
func writeLine(writer io.Writer, key bool, info Info) {
33+
func writeLine(writer io.Writer, columns []string, key bool, info Info) {
3434
first := true
35-
for k, v := range info {
35+
for _, column := range columns {
3636
if !first {
3737
writer.Write([]byte{'\t'})
3838
}
3939
first = false
4040
if key {
41-
writer.Write([]byte(k))
41+
writer.Write([]byte(column))
4242
} else {
43-
writer.Write([]byte(v))
43+
writer.Write([]byte(info[column]))
4444
}
4545
}
4646

project/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type APIProject interface {
2121
Kill(ctx context.Context, signal string, services ...string) error
2222
Log(ctx context.Context, follow bool, services ...string) error
2323
Pause(ctx context.Context, services ...string) error
24-
Ps(ctx context.Context, onlyID bool, services ...string) (InfoSet, error)
24+
Ps(ctx context.Context, services ...string) (InfoSet, error)
2525
// FIXME(vdemeester) we could use nat.Port instead ?
2626
Port(ctx context.Context, index int, protocol, serviceName, privatePort string) (string, error)
2727
Pull(ctx context.Context, services ...string) error

project/project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,15 @@ func (p *Project) Port(ctx context.Context, index int, protocol, serviceName, pr
444444
}
445445

446446
// Ps list containers for the specified services.
447-
func (p *Project) Ps(ctx context.Context, onlyID bool, services ...string) (InfoSet, error) {
447+
func (p *Project) Ps(ctx context.Context, services ...string) (InfoSet, error) {
448448
allInfo := InfoSet{}
449449
for _, name := range p.ServiceConfigs.Keys() {
450450
service, err := p.CreateService(name)
451451
if err != nil {
452452
return nil, err
453453
}
454454

455-
info, err := service.Info(ctx, onlyID)
455+
info, err := service.Info(ctx)
456456
if err != nil {
457457
return nil, err
458458
}

project/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Service interface {
1616
Create(ctx context.Context, options options.Create) error
1717
Delete(ctx context.Context, options options.Delete) error
1818
Events(ctx context.Context, messages chan events.ContainerEvent) error
19-
Info(ctx context.Context, qFlag bool) (InfoSet, error)
19+
Info(ctx context.Context) (InfoSet, error)
2020
Log(ctx context.Context, follow bool) error
2121
Kill(ctx context.Context, signal string) error
2222
Pause(ctx context.Context) error

0 commit comments

Comments
 (0)