Skip to content

Commit 41b8f0f

Browse files
committed
Implement recursive search for Docker Compose files
1 parent ec3ab9f commit 41b8f0f

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

main.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,7 @@ func isDirectory(path string) (bool, error) {
2929
return info.IsDir(), nil
3030
}
3131

32-
func getSubdirectories(basePath string) ([]string, error) {
33-
entries, err := os.ReadDir(basePath)
34-
if err != nil {
35-
return nil, err
36-
}
37-
38-
var subdirs []string
39-
for _, entry := range entries {
40-
if entry.IsDir() {
41-
subdirs = append(subdirs, filepath.Join(basePath, entry.Name()))
42-
}
43-
}
44-
return subdirs, nil
45-
}
46-
47-
func getComposeFileInDir(dir string) (string, error) {
48-
composeFiles := []string{"docker-compose.yml", "docker-compose.yaml", "compose.yml", "compose.yaml"}
49-
for _, file := range composeFiles {
50-
fullPath := filepath.Join(dir, file)
51-
if _, err := os.Stat(fullPath); err == nil {
52-
return fullPath, nil
53-
}
54-
}
55-
return "", fmt.Errorf("no Docker Compose in %s found", dir)
56-
}
57-
58-
func getComposeFiles(basePath string) ([]string, error) {
32+
func getComposeFilesInDir(basePath string) ([]string, error) {
5933
isDir, err := isDirectory(basePath)
6034
if err != nil {
6135
return nil, err
@@ -64,17 +38,31 @@ func getComposeFiles(basePath string) ([]string, error) {
6438
return nil, fmt.Errorf("%s is not a Directory", basePath)
6539
}
6640

67-
subdirs, err := getSubdirectories(basePath)
41+
composeFileNames := []string{"docker-compose.yml", "docker-compose.yaml", "compose.yml", "compose.yaml"}
42+
var composeFiles []string
43+
44+
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
45+
if err != nil {
46+
return err
47+
}
48+
49+
if !info.IsDir() {
50+
for _, fileName := range composeFileNames {
51+
if filepath.Base(path) == fileName {
52+
composeFiles = append(composeFiles, path)
53+
break
54+
}
55+
}
56+
}
57+
return nil
58+
})
59+
6860
if err != nil {
69-
return nil, err
61+
return nil, fmt.Errorf("error while scanning directory %s: %w", basePath, err)
7062
}
7163

72-
var composeFiles []string
73-
for _, dir := range subdirs {
74-
composeFile, err := getComposeFileInDir(dir)
75-
if err == nil {
76-
composeFiles = append(composeFiles, composeFile)
77-
}
64+
if len(composeFiles) == 0 {
65+
return nil, fmt.Errorf("no Docker Compose files found in %s", basePath)
7866
}
7967

8068
return composeFiles, nil
@@ -105,7 +93,7 @@ func getAllComposeFiles() ([]string, string, error) {
10593
paths := getAllComposeSearchPaths()
10694

10795
for _, path := range paths {
108-
currentComposeFilePaths, _ := getComposeFiles(path)
96+
currentComposeFilePaths, _ := getComposeFilesInDir(path)
10997
composeFilePaths = append(composeFilePaths, currentComposeFilePaths...)
11098
}
11199

0 commit comments

Comments
 (0)