Skip to content

Commit 371452e

Browse files
committed
Added support for multiple --file invocations for compose overrides
1 parent b7b4a8a commit 371452e

File tree

15 files changed

+481
-21
lines changed

15 files changed

+481
-21
lines changed

Sources/CLI/Compose/ComposeCommand.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ extension Application {
4242
// MARK: - Shared Options
4343

4444
struct ComposeOptions: ParsableArguments {
45-
@Option(name: [.customLong("file"), .customShort("f")], help: "Specify an alternate compose file")
46-
var file: String = "docker-compose.yaml"
45+
@Option(name: [.customLong("file"), .customShort("f")], help: "Specify compose file(s) (can be used multiple times)")
46+
var file: [String] = []
4747

4848
@Option(name: [.customLong("project"), .customShort("p")], help: "Specify an alternate project name")
4949
var project: String?
@@ -64,13 +64,40 @@ struct ComposeOptions: ParsableArguments {
6464
return url.lastPathComponent.lowercased().replacingOccurrences(of: " ", with: "")
6565
}
6666

67-
func getComposeFileURL() -> URL {
68-
if file.hasPrefix("/") {
69-
return URL(fileURLWithPath: file)
70-
} else {
71-
let currentPath = FileManager.default.currentDirectoryPath
72-
return URL(fileURLWithPath: currentPath).appendingPathComponent(file)
67+
func getComposeFileURLs() -> [URL] {
68+
// If no files specified, use default
69+
let files = file.isEmpty ? ["docker-compose.yaml", "docker-compose.yml", "compose.yaml", "compose.yml"] : file
70+
71+
var urls: [URL] = []
72+
let currentPath = FileManager.default.currentDirectoryPath
73+
74+
for fileName in files {
75+
let url: URL
76+
if fileName.hasPrefix("/") {
77+
url = URL(fileURLWithPath: fileName)
78+
} else {
79+
url = URL(fileURLWithPath: currentPath).appendingPathComponent(fileName)
80+
}
81+
82+
// For default files, only add if they exist
83+
if file.isEmpty {
84+
if FileManager.default.fileExists(atPath: url.path) {
85+
urls.append(url)
86+
break // Use first found default file
87+
}
88+
} else {
89+
// For explicitly specified files, add them all (parser will check existence)
90+
urls.append(url)
91+
}
92+
}
93+
94+
// If no files found from defaults, return the first default for error message
95+
if urls.isEmpty && file.isEmpty {
96+
let defaultFile = URL(fileURLWithPath: currentPath).appendingPathComponent("docker-compose.yaml")
97+
urls.append(defaultFile)
7398
}
99+
100+
return urls
74101
}
75102

76103
func setEnvironmentVariables() {

Sources/CLI/Compose/ComposeDown.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension Application {
4646

4747
// Parse compose file
4848
let parser = ComposeParser(log: log)
49-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
49+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
5050

5151
// Convert to project
5252
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeExec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension Application {
6363

6464
// Parse compose file
6565
let parser = ComposeParser(log: log)
66-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
66+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
6767

6868
// Convert to project
6969
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeHealth.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Application {
4949

5050
// Parse compose file
5151
let parser = ComposeParser(log: log)
52-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
52+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
5353

5454
// Convert to project
5555
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeLogs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension Application {
5151

5252
// Parse compose file
5353
let parser = ComposeParser(log: log)
54-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
54+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
5555

5656
// Convert to project
5757
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposePS.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension Application {
4545

4646
// Parse compose file
4747
let parser = ComposeParser(log: log)
48-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
48+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
4949

5050
// Convert to project
5151
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeRestart.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension Application {
4545

4646
// Parse compose file
4747
let parser = ComposeParser(log: log)
48-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
48+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
4949

5050
// Convert to project
5151
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeStart.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension Application {
4343

4444
// Parse compose file
4545
let parser = ComposeParser(log: log)
46-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
46+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
4747

4848
// Convert to project
4949
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeStop.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension Application {
4646

4747
// Parse compose file
4848
let parser = ComposeParser(log: log)
49-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
49+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
5050

5151
// Convert to project
5252
let converter = ProjectConverter(log: log)

Sources/CLI/Compose/ComposeUp.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ extension Application {
5656
// Set environment variables
5757
composeOptions.setEnvironmentVariables()
5858

59-
// Parse compose file
59+
// Parse compose files
6060
let parser = ComposeParser(log: log)
61-
let composeFile = try parser.parse(from: composeOptions.getComposeFileURL())
61+
let composeFile = try parser.parse(from: composeOptions.getComposeFileURLs())
6262

6363
// Convert to project
6464
let converter = ProjectConverter(log: log)

0 commit comments

Comments
 (0)