From 91e597dda102a3d1cd58df82a9b68ab6a9037247 Mon Sep 17 00:00:00 2001 From: skedwards88 Date: Wed, 4 Nov 2026 13:14:18 -0700 Subject: [PATCH] edit script to just overwrite table --- .../docgen/table_errors/TableErrorsArgs.java | 2 +- .../docgen/table_errors/TableErrorsCmd.java | 72 +++++++++---------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsArgs.java b/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsArgs.java index 2681de4..80f3d08 100644 --- a/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsArgs.java +++ b/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsArgs.java @@ -18,7 +18,7 @@ public class TableErrorsArgs extends BaseArgs { names = { "-o", "--output" }, description = "Output AsciiDoc file", paramLabel = "OUTPUT_FILE", - defaultValue = "build/docs/table-errors" + defaultValue = "../docs-common/modules/data-api/partials/dataapi-errors.adoc" ) public String $output; diff --git a/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsCmd.java b/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsCmd.java index 7af835c..e18374b 100644 --- a/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsCmd.java +++ b/src/main/java/com/dtsx/docs/commands/docgen/table_errors/TableErrorsCmd.java @@ -29,18 +29,13 @@ public class TableErrorsCmd extends BaseCmd { @Mixin @Getter private TableErrorsArgs $args; - private static final String[] ASCIIDOC_PREFIX = { - "= {data-api} error codes", - "", - "The {data-api} can return the following errors:", - "", - "// Note: The following table is autogenerated from the actual Data API error codes and should not be edited manually.", - "// If there is a typo or an issue with description clarity, the source Data API code should be updated instead.", - "// To regenerate, run 'dh docgen table-errors' from the datastax/astra-client-docs-tests repo. Pass the raw 'src/main/resources/errors.yaml' file from the stargate/data-api repo corresponding to the desired release.", - "" - }; - - private static final String[] ASCIIDOC_SUFFIX = {}; + private static final String TAG_START = "// tag::generated-table[]"; + private static final String TAG_END = "// end::generated-table[]"; + + private static final Pattern TABLE_REGION_PATTERN = Pattern.compile( + Pattern.quote(TAG_START) + ".*?" + Pattern.quote(TAG_END), + Pattern.DOTALL + ); private static final Map ERROR_CODE_SEE_ALSO_MAP = Map.of( "DOCUMENT_REPLACE_DIFFERENT_DOCID", new String[] { @@ -75,39 +70,42 @@ protected int run() { .sorted(Comparator.comparing(n -> n.path("code").asString(""))) .toList(); - if (output.getParent() != null) { - Files.createDirectories(output.getParent()); - } - - try (val writer = Files.newBufferedWriter(output)) { - for (val line : ASCIIDOC_PREFIX) { - writer.write(line + "\n"); - } - - writer.write("[cols=\"1,1,2,3\"]\n|===\n| Error code | Scope | Title | Description\n\n"); + // Build the replacement block (everything between and including the tags) + val sb = new StringBuilder(); + sb.append(TAG_START).append("\n"); + sb.append("[cols=\"1,1,2,3\"]\n|===\n| Error code | Scope | Title | Description\n\n"); - for (val entry : errors) { - val code = entry.path("code").asString(""); - val scope = entry.hasNonNull("scope") ? entry.get("scope").asString() : "n/a"; - val title = escapePipe(entry.path("title").asString("")); - val desc = escapePipe(PLACEHOLDER_PATTERN.matcher(entry.path("body").asString("").split("\r?\n", 2)[0].trim()).replaceAll("**[$1]**")); + for (val entry : errors) { + val code = entry.path("code").asString(""); + val scope = entry.hasNonNull("scope") ? entry.get("scope").asString() : "n/a"; + val title = escapePipe(entry.path("title").asString("")); + val desc = escapePipe(PLACEHOLDER_PATTERN.matcher(entry.path("body").asString("").split("\r?\n", 2)[0].trim()).replaceAll("**[$1]**")); - writer.write(String.format("| `%s`\n| %s\n| %s\na| %s\n", code, scope, title, desc)); + sb.append(String.format("| `%s`\n| %s\n| %s\na| %s\n", code, scope, title, desc)); - if (ERROR_CODE_SEE_ALSO_MAP.containsKey(code)) { - String[] linkInfo = ERROR_CODE_SEE_ALSO_MAP.get(code); - writer.write(String.format("\nSee also %s[%s].\n", linkInfo[0], linkInfo[1])); - } - writer.write("\n"); + if (ERROR_CODE_SEE_ALSO_MAP.containsKey(code)) { + String[] linkInfo = ERROR_CODE_SEE_ALSO_MAP.get(code); + sb.append(String.format("\nSee also %s[%s].\n", linkInfo[0], linkInfo[1])); } + sb.append("\n"); + } - writer.write("|===\n"); + sb.append("|===\n"); + sb.append(TAG_END); - for (val line : ASCIIDOC_SUFFIX) { - writer.write(line + "\n"); - } + // Read the existing file and splice in the new table + val existing = Files.readString(output); + val matcher = TABLE_REGION_PATTERN.matcher(existing); + + if (!matcher.find()) { + throw new IllegalStateException("Could not find generated-table tags in " + output + ". Expected markers: '" + TAG_START + "' and '" + TAG_END + "'"); } + val updated = new StringBuilder(existing); + updated.replace(matcher.start(), matcher.end(), sb.toString()); + + Files.writeString(output, updated.toString()); + CliLogger.println(false, "@|green ✓|@ Written " + errors.size() + " rows to " + output); return 0;