Skip to content

Commit

Permalink
ENH: add folder path as output for schema generation (#4820)
Browse files Browse the repository at this point in the history
* ENH: add folder path as output

Signed-off-by: George Chen <qchea@amazon.com>
  • Loading branch information
chenqi0805 authored Aug 12, 2024
1 parent fedd070 commit be85abe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
3 changes: 2 additions & 1 deletion data-prepper-plugin-schema-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ This module includes the SDK and CLI for generating schemas for Data Prepper pip
## CLI Usage

```
./gradlew :data-prepper-plugin-schema-cli:run --args='--plugin_type=processor --plugin_names=grok'
./gradlew :data-prepper-plugin-schema-cli:run --args='--plugin_type=processor --plugin_names=grok --output_folder=/path/to/schemas'
```

* plugin_type: A required parameter specifies type of processor. Valid options are `source`, `buffer`, `processor`, `route`, `sink`.
* plugin_names: An optional parameter filters the result by plugin names separated by `,`, e.g. `grok,date`.
* output_folder: An optional parameter to specify the output folder path.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
import org.slf4j.LoggerFactory;
import picocli.CommandLine;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.github.victools.jsonschema.module.jackson.JacksonOption.RESPECT_JSONPROPERTY_REQUIRED;

Expand All @@ -37,6 +42,9 @@ public class DataPrepperPluginSchemaExecute implements Runnable {
@CommandLine.Option(names = {"--site.baseurl"}, defaultValue = "/docs/latest")
private String siteBaseUrl;

@CommandLine.Option(names = {"--output_folder"})
private String folderPath;

public static void main(String[] args) {
final int exitCode = new CommandLine(new DataPrepperPluginSchemaExecute()).execute(args);
System.exit(exitCode);
Expand All @@ -57,18 +65,59 @@ public void run() {
final Class<?> pluginType = pluginConfigsJsonSchemaConverter.pluginTypeNameToPluginType(pluginTypeName);
final Map<String, String> pluginNameToJsonSchemaMap = pluginConfigsJsonSchemaConverter.convertPluginConfigsIntoJsonSchemas(
SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON, pluginType);
Map<String, String> filteredPluginNameToJsonSchemaMap;
if (pluginNames == null) {
pluginNameToJsonSchemaMap.values().forEach(System.out::println);
filteredPluginNameToJsonSchemaMap = pluginNameToJsonSchemaMap;
} else {
final Set<String> pluginNamesSet = Set.of(pluginNames.split(","));
final List<String> result = pluginNamesSet.stream().flatMap(name -> {
if (!pluginNameToJsonSchemaMap.containsKey(name)) {
LOG.error("plugin name: {} not found", name);
return Stream.empty();
}
return Stream.of(pluginNameToJsonSchemaMap.get(name));
}).collect(Collectors.toList());
result.forEach(System.out::println);
filteredPluginNameToJsonSchemaMap = pluginNamesSet.stream()
.filter(name -> {
if (!pluginNameToJsonSchemaMap.containsKey(name)) {
LOG.error("plugin name: {} not found", name);
return false;
}
return true;
})
.collect(Collectors.toMap(
Function.identity(),
pluginNameToJsonSchemaMap::get
));
}

if (folderPath == null) {
writeCollectionToConsole(filteredPluginNameToJsonSchemaMap.values());
} else {
writeMapToFiles(filteredPluginNameToJsonSchemaMap, folderPath);
}
}

private static void writeCollectionToConsole(final Collection<String> values) {
values.forEach(System.out::println);
}

private static void writeMapToFiles(final Map<String, String> map, final String folderPath) {
// Ensure the directory exists
final Path directory = Paths.get(folderPath);
if (!Files.exists(directory)) {
try {
Files.createDirectories(directory);
} catch (IOException e) {
System.err.println("Error creating directory: " + e.getMessage());
return;
}
}

// Iterate through the map and write each entry to a file
for (final Map.Entry<String, String> entry : map.entrySet()) {
final String fileName = entry.getKey() + ".json";
final Path filePath = directory.resolve(fileName);

try {
Files.write(filePath, entry.getValue().getBytes());
System.out.println("Written file: " + filePath);
} catch (IOException e) {
System.err.println("Error writing file " + fileName + ": " + e.getMessage());
}
}
}
}

0 comments on commit be85abe

Please sign in to comment.