com.xlrit.gears.runner.driver.ExportDriver Maven / Gradle / Ivy
The newest version!
package com.xlrit.gears.runner.driver;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.xlrit.gears.runner.graphql.GraphQLClient;
import com.xlrit.gears.runner.graphql.Operation;
import com.xlrit.gears.runner.utils.MiscUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExportDriver extends AbstractDriver {
private static final Logger LOG = LoggerFactory.getLogger(ExportDriver.class);
@Override
public int run(ExportConfig config) {
GraphQLClient client = getClient(config);
File specDir = new File(config.specDir);
List specFiles = MiscUtils.listFilteredFiles(specDir, config.filePattern, "txt");
int failures = 0;
if (config.dryRun) {
for (var dataFile : specFiles) {
LOG.info(" - Exporting data from {}", dataFile.getPath());
}
} else {
LOG.info(" - Exporting data from {}", config.filePattern);
File resultDir = new File(config.resultDir);
if (config.writeResult && !resultDir.isDirectory() && !resultDir.mkdirs()) {
LOG.error("Failed to create directory {}", config.resultDir);
return -1;
}
LOG.info("Exporting data from {} files:", specFiles.size());
for (File specFile : specFiles) {
LOG.info(" - {}", specFile.getPath());
try {
String spec = Files.readString(specFile.toPath());
JsonNode jsonResponse = client.invoke(Operation.exportData(config.exportTarget, spec));
if (!config.quiet) LOG.info("{}", jsonResponse.get("export"));
if (config.writeResult) {
File resultFile = new File(resultDir, MiscUtils.getNameNoExtension(specFile) + "_result." + config.exportTarget);
Files.writeString(resultFile.toPath(), jsonResponse.get("export").toPrettyString());
}
}
catch (Exception e) {
failures--;
LOG.error("Error while exporting from {}: {}", specFile.getName(), e.getMessage());
if (config.debug) e.printStackTrace();
}
}
}
return failures;
}
}