Skip to content
Closed
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/compose.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/golinter.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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, "")
}
13 changes: 11 additions & 2 deletions pkg/compose/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func processFile(ctx context.Context, file string, project *types.Project, extFi
}
for name, service := range base.Services {
for i, envFile := range service.EnvFiles {
if !envFile.Required {
continue
}
hash := fmt.Sprintf("%x.env", sha256.Sum256([]byte(envFile.Path)))
envFiles[envFile.Path] = hash
f, err = transform.ReplaceEnvFile(f, name, i, hash)
Expand Down Expand Up @@ -351,8 +354,11 @@ func (s *composeService) checkEnvironmentVariables(project *types.Project, optio
errorList := map[string][]string{}

for _, service := range project.Services {
if len(service.EnvFiles) > 0 {
errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has env_file declared.", service.Name))
for _, envFile := range service.EnvFiles {
if envFile.Required {
errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has env_file declared.", service.Name))
break
}
}
}

Expand Down Expand Up @@ -438,6 +444,9 @@ func (s *composeService) checkForSensitiveData(project *types.Project) ([]secret
for _, service := range project.Services {
// Check env files
for _, envFile := range service.EnvFiles {
if !envFile.Required {
continue
}
findings, err := scan.ScanFile(envFile.Path)
if err != nil {
return nil, fmt.Errorf("failed to scan env file %s: %w", envFile.Path, err)
Expand Down
67 changes: 67 additions & 0 deletions pkg/compose/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package compose

import (
"slices"
"strings"
"testing"

"github.com/compose-spec/compose-go/v2/loader"
Expand Down Expand Up @@ -100,3 +101,69 @@ services:
return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
}, cmp.Ignore()))
}

func Test_createLayers_withRequiredFalse(t *testing.T) {
project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
WorkingDir: "testdata/publish/",
Environment: types.Mapping{},
ConfigFiles: []types.ConfigFile{
{
Filename: "testdata/publish/compose-required-false.yaml",
},
},
})
assert.NilError(t, err)
project.ComposeFiles = []string{"testdata/publish/compose-required-false.yaml"}

service := &composeService{}
layers, err := service.createLayers(t.Context(), project, api.PublishOptions{
WithEnvironment: true,
})
assert.NilError(t, err)

assert.Equal(t, len(layers), 2)

assert.Equal(t, layers[0].Annotations["com.docker.compose.file"], "compose-required-false.yaml")

assert.Equal(t, layers[1].MediaType, "application/vnd.docker.compose.envfile")

envFileHash := layers[1].Annotations["com.docker.compose.envfile"]
assert.Assert(t, len(envFileHash) > 0)
assert.Assert(t, envFileHash != "missing.env")
}

func Test_checkEnvironmentVariables_withRequiredFalse(t *testing.T) {
project := &types.Project{
Services: types.Services{
"test": {
Name: "test",
EnvFiles: []types.EnvFile{
{
Path: "missing.env",
Required: false,
},
{
Path: "existing.env",
Required: true,
},
},
},
"test2": {
Name: "test2",
EnvFiles: []types.EnvFile{
{
Path: "optional.env",
Required: false,
},
},
},
},
}

service := &composeService{}

err := service.checkEnvironmentVariables(project, api.PublishOptions{})
assert.Assert(t, err != nil)
assert.Assert(t, strings.Contains(err.Error(), `service "test" has env_file declared.`))
assert.Assert(t, !strings.Contains(err.Error(), `service "test2"`))
}
9 changes: 9 additions & 0 deletions pkg/compose/testdata/publish/compose-required-false.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: test-required-false
services:
test:
image: test
env_file:
- path: missing.env
required: false
- path: existing.env
required: true
1 change: 1 addition & 0 deletions pkg/compose/testdata/publish/existing.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXISTING_VAR=value