Skip to content

Commit 4bd8c62

Browse files
committed
patch: allow option to allow comments in json files
1 parent 663205f commit 4bd8c62

File tree

7 files changed

+72
-30
lines changed

7 files changed

+72
-30
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,8 @@ Provides simple operations to list network interface names and check existence
935935
### patch
936936

937937
```
938-
Usage: mc-image-helper patch [-h] [--patch-env-prefix=<envPrefix>] FILE_OR_DIR
938+
Usage: mc-image-helper patch [-h] [--json-allow-comments]
939+
[--patch-env-prefix=<envPrefix>] FILE_OR_DIR
939940
Patches one or more existing files using JSON path based operations
940941
Supports the file formats:
941942
- JSON
@@ -945,6 +946,9 @@ Supports the file formats:
945946
FILE_OR_DIR Path to a PatchSet json file or directory containing
946947
PatchDefinition json files
947948
-h, --help Show this usage and exit
949+
--json-allow-comments
950+
Whether to allow comments in JSON files. Env: PATCH_JSON_ALLOW_COMMENTS
951+
Default: true
948952
--patch-env-prefix=<envPrefix>
949953
Only placeholder variables with this prefix will be
950954
processed

src/main/java/me/itzg/helpers/curseforge/CurseForgeInstaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void install(String slug, InstallationEntryPoint entryPoint) {
230230
);
231231

232232
} catch (IOException e) {
233-
throw new GenericException("Failed to setup API caching", e);
233+
throw new GenericException("File system issue during installation", e);
234234
}
235235
}
236236

src/main/java/me/itzg/helpers/patch/JsonFileFormat.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.itzg.helpers.patch;
22

3+
import com.fasterxml.jackson.core.JsonParser.Feature;
34
import com.fasterxml.jackson.core.type.TypeReference;
45
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
56
import com.fasterxml.jackson.core.util.Separators;
@@ -19,8 +20,9 @@ public class JsonFileFormat implements FileFormat {
1920
private final ObjectMapper objectMapper;
2021
private final ObjectWriter objectWriter;
2122

22-
public JsonFileFormat() {
23-
objectMapper = new ObjectMapper();
23+
public JsonFileFormat(boolean allowComments) {
24+
objectMapper = new ObjectMapper()
25+
.configure(Feature.ALLOW_COMMENTS, allowComments);
2426
objectWriter = objectMapper.writer(
2527
new DefaultPrettyPrinter()
2628
.withSeparators(

src/main/java/me/itzg/helpers/patch/PatchCommand.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,39 @@
1313
import me.itzg.helpers.patch.model.PatchDefinition;
1414
import me.itzg.helpers.patch.model.PatchSet;
1515
import picocli.CommandLine;
16+
import picocli.CommandLine.Command;
17+
import picocli.CommandLine.Option;
18+
import picocli.CommandLine.Parameters;
1619

17-
@CommandLine.Command(name = "patch",
20+
@Command(name = "patch",
1821
description = "Patches one or more existing files using JSON path based operations%n"
1922
+ "Supports the file formats:%n"
2023
+ "- JSON%n"
2124
+ "- JSON5%n"
2225
+ "- Yaml%n"
23-
+ "- TOML, but processed output is not pretty"
26+
+ "- TOML, but processed output is not pretty",
27+
showDefaultValues = true
2428
)
2529
@Slf4j
2630
public class PatchCommand implements Callable<Integer> {
27-
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Show this usage and exit")
31+
32+
private static final String ENV_JSON_ALLOW_COMMENTS = "PATCH_JSON_ALLOW_COMMENTS";
33+
@Option(names = {"-h", "--help"}, usageHelp = true, description = "Show this usage and exit")
2834
boolean showHelp;
2935

30-
@CommandLine.Option(names = "--patch-env-prefix",
36+
@Option(names = "--patch-env-prefix",
3137
defaultValue = "CFG_",
32-
showDefaultValue = CommandLine.Help.Visibility.ALWAYS,
3338
description = "Only placeholder variables with this prefix will be processed"
3439
)
3540
String envPrefix;
3641

37-
@CommandLine.Parameters(description = "Path to a PatchSet json file or directory containing PatchDefinition json files",
42+
@Option(names = "--json-allow-comments", defaultValue = "${env:" + ENV_JSON_ALLOW_COMMENTS + ":-true}",
43+
description = "Whether to allow comments in JSON files. Env: " + ENV_JSON_ALLOW_COMMENTS,
44+
showDefaultValue = CommandLine.Help.Visibility.ALWAYS
45+
)
46+
boolean jsonAllowComments;
47+
48+
@Parameters(description = "Path to a PatchSet json file or directory containing PatchDefinition json files",
3849
paramLabel = "FILE_OR_DIR"
3950
)
4051
Path patches;

src/main/java/me/itzg/helpers/patch/PatchSetProcessor.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ public class PatchSetProcessor {
2929
private final Interpolator interpolator;
3030

3131
public PatchSetProcessor(Interpolator interpolator) {
32+
this(interpolator, false);
33+
}
34+
35+
public PatchSetProcessor(Interpolator interpolator, boolean jsonAllowComments) {
3236
this.interpolator = interpolator;
37+
fileFormats = new FileFormat[]{
38+
new JsonFileFormat(jsonAllowComments),
39+
new Json5FileFormat(),
40+
new YamlFileFormat(),
41+
new TomlFileFormat()
42+
};
3343
}
3444

35-
private final FileFormat[] fileFormats = new FileFormat[]{
36-
new JsonFileFormat(),
37-
new Json5FileFormat(),
38-
new YamlFileFormat(),
39-
new TomlFileFormat()
40-
};
45+
private final FileFormat[] fileFormats;
4146

4247
public void process(PatchSet patchSet) {
4348
log.debug("patchSet={}", patchSet);

src/test/java/me/itzg/helpers/patch/PatchSetProcessorTest.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.file.Path;
1717
import java.nio.file.Paths;
1818
import java.util.Arrays;
19+
import java.util.stream.Stream;
1920
import me.itzg.helpers.env.EnvironmentVariablesProvider;
2021
import me.itzg.helpers.env.Interpolator;
2122
import me.itzg.helpers.patch.model.PatchDefinition;
@@ -25,6 +26,9 @@
2526
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.api.extension.ExtendWith;
2728
import org.junit.jupiter.api.io.TempDir;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.Arguments;
31+
import org.junit.jupiter.params.provider.MethodSource;
2832
import org.mockito.Mock;
2933
import org.mockito.junit.jupiter.MockitoExtension;
3034

@@ -34,29 +38,39 @@ class PatchSetProcessorTest {
3438
@Mock
3539
EnvironmentVariablesProvider environmentVariablesProvider;
3640

37-
@Test
38-
void setInJson(@TempDir Path tempDir) throws IOException {
41+
public static Stream<Arguments> setInJson_args() {
42+
return Stream.of(
43+
Arguments.arguments("testing.json", false),
44+
Arguments.arguments("testing.json", true),
45+
Arguments.arguments("testing-with-comment.json", true)
46+
);
47+
}
48+
49+
@ParameterizedTest
50+
@MethodSource("setInJson_args")
51+
void setInJson(String file, boolean allowComments, @TempDir Path tempDir) throws IOException {
3952
final Path src = tempDir.resolve("testing.json");
40-
Files.copy(Paths.get("src/test/resources/patch/testing.json"), src);
53+
Files.copy(Paths.get("src/test/resources/patch/" + file), src);
4154

4255
final PatchSetProcessor processor = new PatchSetProcessor(
43-
new Interpolator(environmentVariablesProvider, "CFG_")
56+
new Interpolator(environmentVariablesProvider, "CFG_"),
57+
allowComments
4458
);
4559

4660
processor.process(new PatchSet()
47-
.setPatches(singletonList(
48-
new PatchDefinition()
49-
.setFile(src.toString())
50-
.setOps(singletonList(
51-
new PatchSetOperation()
52-
.setPath("$.outer.field1")
53-
.setValue(new TextNode("new value"))
54-
))
55-
))
61+
.setPatches(singletonList(
62+
new PatchDefinition()
63+
.setFile(src.toString())
64+
.setOps(singletonList(
65+
new PatchSetOperation()
66+
.setPath("$.outer.field1")
67+
.setValue(new TextNode("new value"))
68+
))
69+
))
5670
);
5771

5872
assertThat(src).hasSameTextualContentAs(
59-
Paths.get("src/test/resources/patch/expected-setInJson.json")
73+
Paths.get("src/test/resources/patch/expected-setInJson.json")
6074
);
6175
}
6276

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"outer": {
3+
// some comment
4+
"field1": "old"
5+
}
6+
}

0 commit comments

Comments
 (0)