diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 2528fccacfb..e79a8bcef89 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -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") @@ -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(), diff --git a/cmd/compose/ps_format_test.go b/cmd/compose/ps_format_test.go new file mode 100644 index 00000000000..b36c0788953 --- /dev/null +++ b/cmd/compose/ps_format_test.go @@ -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, "") +}