Skip to content

Commit 4297010

Browse files
Add --output-format to filter command (json or yaml)
Signed-off-by: Si Beaumont <[email protected]>
1 parent 13446fa commit 4297010

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Sources/swift-openapi-generator/FilterCommand.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ struct _FilterCommand: AsyncParsableCommand {
3636
@Option(help: "Path to a YAML configuration file.")
3737
var config: URL
3838

39+
@Option(help: "Output format, either \(OutputFormat.yaml.rawValue) or \(OutputFormat.json.rawValue).")
40+
var outputFormat: OutputFormat = .yaml
41+
3942
@Argument(help: "Path to the OpenAPI document, either in YAML or JSON.")
4043
var docPath: URL
4144

@@ -54,7 +57,14 @@ struct _FilterCommand: AsyncParsableCommand {
5457
return
5558
}
5659
let filteredDocument = try timing("Filtering document", documentFilter.filter(document))
57-
FileHandle.standardOutput.write(try YAMLEncoder().encode(filteredDocument))
60+
switch outputFormat {
61+
case .yaml:
62+
FileHandle.standardOutput.write(try YAMLEncoder().encode(filteredDocument))
63+
case .json:
64+
let encoder = JSONEncoder()
65+
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
66+
FileHandle.standardOutput.write(try encoder.encode(filteredDocument))
67+
}
5868
}
5969
}
6070

@@ -80,3 +90,8 @@ private let sampleConfig = _UserConfig(
8090
schemas: ["Greeting"]
8191
)
8292
)
93+
94+
enum OutputFormat: String, ExpressibleByArgument {
95+
case json
96+
case yaml
97+
}

0 commit comments

Comments
 (0)