Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/compose/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func psCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backend
ValidArgsFunction: completeServiceNames(dockerCli, p),
}
flags := psCmd.Flags()
flags.StringVar(&opts.Format, "format", "table", cliflags.FormatHelp)
flags.StringVar(&opts.Format, "format", "", cliflags.FormatHelp)
flags.StringVar(&opts.Filter, "filter", "", "Filter services by a property (supported filters: status)")
flags.StringArrayVar(&opts.Status, "status", []string{}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]")
flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
Expand Down Expand Up @@ -152,9 +152,14 @@ func runPs(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOp
return nil
}

if opts.Format == "" {
if len(opts.Format) == 0 {
opts.Format = dockerCli.ConfigFile().PsFormat
}
if len(opts.Format) == 0 {
opts.Format = "table"
} else if opts.Quiet {
_, _ = dockerCli.Err().Write([]byte("WARNING: Ignoring custom format, because both --format and --quiet are set.\n"))
}

containerCtx := cliformatter.Context{
Output: dockerCli.Out(),
Expand Down
47 changes: 47 additions & 0 deletions cmd/compose/ps_format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2020 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package compose

import (
"testing"

"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/streams"
"go.uber.org/mock/gomock"
"gotest.tools/v3/assert"

"github.com/docker/compose/v5/pkg/mocks"
)

func TestPsCommandDefaultFormat(t *testing.T) {
// Test that the format flag has empty string as default
projectOpts := &ProjectOptions{}
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cli := mocks.NewMockCli(mockCtrl)
cli.EXPECT().ConfigFile().Return(configfile.New("test")).AnyTimes()
cli.EXPECT().Out().Return(&streams.Out{}).AnyTimes()
cli.EXPECT().Err().Return(&streams.Out{}).AnyTimes()

backendOptions := &BackendOptions{}
cmd := psCommand(projectOpts, cli, backendOptions)

// Check default value of format flag
formatFlag := cmd.Flags().Lookup("format")
assert.Equal(t, formatFlag.DefValue, "")
}