Skip to content

Add support for wildcards selecting compose files #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
45 changes: 34 additions & 11 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -403,36 +404,58 @@ func (o *ProjectOptions) GetWorkingDir() (string, error) {
}

// ReadConfigFiles reads ConfigFiles and populates the content field
func (o *ProjectOptions) ReadConfigFiles(ctx context.Context, workingDir string, options *ProjectOptions) (*types.ConfigDetails, error) {
config, err := loader.LoadConfigFiles(ctx, options.ConfigPaths, workingDir, options.loadOptions...)
func (o *ProjectOptions) ReadConfigFiles(ctx context.Context, workingDir string) (*types.ConfigDetails, error) {
config, err := loader.LoadConfigFiles(ctx, o.ConfigPaths, workingDir, o.loadOptions...)
if err != nil {
return nil, err
}
configs := make([][]byte, len(config.ConfigFiles))

for i, c := range config.ConfigFiles {
configs := make([]types.ConfigFile, 0, len(config.ConfigFiles))
for _, c := range config.ConfigFiles {
var err error
var b []byte
if c.IsStdin() {
b, err = io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}

configs = append(configs, types.ConfigFile{
Filename: "-",
Content: b,
Config: nil,
})
} else {
f, err := filepath.Abs(c.Filename)
if err != nil {
return nil, err
}
b, err = os.ReadFile(f)

if stat, err := os.Stat(f); err == nil && stat.IsDir() {
f = filepath.Join(f, "*.yaml")
}

matches, err := filepath.Glob(f)
if err != nil {
return nil, err
}

slices.Sort(matches)

for _, match := range matches {
b, err = os.ReadFile(match)
if err != nil {
return nil, err
}

configs = append(configs, types.ConfigFile{
Filename: match,
Content: b,
Config: nil,
})
}
}
configs[i] = b
}
for i, c := range configs {
config.ConfigFiles[i].Content = c
}
config.ConfigFiles = configs
return config, nil
}

Expand Down Expand Up @@ -476,7 +499,7 @@ func (o *ProjectOptions) prepare(ctx context.Context) (*types.ConfigDetails, err
return &types.ConfigDetails{}, err
}

configDetails, err := o.ReadConfigFiles(ctx, defaultDir, o)
configDetails, err := o.ReadConfigFiles(ctx, defaultDir)
if err != nil {
return configDetails, err
}
Expand Down
46 changes: 46 additions & 0 deletions cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,49 @@ func TestEnvVariablePrecedence(t *testing.T) {
})
}
}

func TestWildcards(t *testing.T) {
options, err := NewProjectOptions([]string{"testdata/wildcards/compose-*.yaml"})
assert.NilError(t, err)
files, err := options.ReadConfigFiles(context.TODO(), ".")
assert.NilError(t, err)

abs, err := filepath.Abs("testdata/wildcards")
assert.NilError(t, err)
assert.DeepEqual(t, files, &types.ConfigDetails{
WorkingDir: ".",
ConfigFiles: []types.ConfigFile{
{
Filename: filepath.Join(abs, "compose-A.yaml"),
Content: []byte{},
},
{
Filename: filepath.Join(abs, "compose-B.yaml"),
Content: []byte{},
},
},
})
}

func TestFolder(t *testing.T) {
options, err := NewProjectOptions([]string{"testdata/wildcards/"})
assert.NilError(t, err)
files, err := options.ReadConfigFiles(context.TODO(), ".")
assert.NilError(t, err)

abs, err := filepath.Abs("testdata/wildcards")
assert.NilError(t, err)
assert.DeepEqual(t, files, &types.ConfigDetails{
WorkingDir: ".",
ConfigFiles: []types.ConfigFile{
{
Filename: filepath.Join(abs, "compose-A.yaml"),
Content: []byte{},
},
{
Filename: filepath.Join(abs, "compose-B.yaml"),
Content: []byte{},
},
},
})
}
Empty file.
Empty file.