Skip to content

Commit 09ccc63

Browse files
authored
props: add option to escape unicode (#340)
1 parent 6a2eee0 commit 09ccc63

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

src/main/java/me/itzg/helpers/properties/SetPropertiesCommand.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package me.itzg.helpers.properties;
22

33
import com.fasterxml.jackson.core.type.TypeReference;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
47
import java.io.Reader;
58
import java.io.Writer;
6-
import java.nio.charset.StandardCharsets;
79
import java.nio.file.Files;
810
import java.nio.file.Path;
911
import java.nio.file.StandardOpenOption;
@@ -43,6 +45,9 @@ public class SetPropertiesCommand implements Callable<Integer> {
4345
description = "Key=value pairs of custom properties to set")
4446
Map<String,String> customProperties;
4547

48+
@Option(names = "--escape-unicode")
49+
boolean escapeUnicode;
50+
4651
@Parameters(arity = "1")
4752
Path propertiesFile;
4853

@@ -70,24 +75,52 @@ public Integer call() throws Exception {
7075
}
7176

7277
final Properties properties = new Properties();
73-
if (Files.exists(propertiesFile)) {
74-
try (Reader propsReader = Files.newBufferedReader(propertiesFile, StandardCharsets.UTF_8)) {
75-
properties.load(propsReader);
76-
}
77-
}
78+
loadProperties(properties);
7879

7980
final long changes = processProperties(propertyDefinitions, properties, customProperties);
8081
if (changes > 0) {
8182
log.info("Created/updated {} propert{} in {}", changes, changes != 1 ? "ies":"y", propertiesFile);
8283

83-
try (Writer propsOut = Files.newBufferedWriter(propertiesFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
84-
properties.store(propsOut, String.format("Updated %s by mc-image-helper", Instant.now()));
85-
}
84+
storeProperties(properties);
8685
}
8786

8887
return ExitCode.OK;
8988
}
9089

90+
private void storeProperties(Properties properties) throws IOException {
91+
final String comment = String.format("Updated %s by mc-image-helper", Instant.now());
92+
if (escapeUnicode) {
93+
try (OutputStream out = Files.newOutputStream(propertiesFile,
94+
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE
95+
)) {
96+
properties.store(out, comment);
97+
}
98+
}
99+
else {
100+
try (Writer propsOut = Files.newBufferedWriter(propertiesFile,
101+
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
102+
) {
103+
104+
properties.store(propsOut, comment);
105+
}
106+
}
107+
}
108+
109+
private void loadProperties(Properties properties) throws IOException {
110+
if (Files.exists(propertiesFile)) {
111+
if (escapeUnicode) {
112+
try (InputStream is = Files.newInputStream(propertiesFile)) {
113+
properties.load(is);
114+
}
115+
}
116+
else {
117+
try (Reader propsReader = Files.newBufferedReader(propertiesFile)) {
118+
properties.load(propsReader);
119+
}
120+
}
121+
}
122+
}
123+
91124
/**
92125
* @return count of added/modified properties
93126
*/
@@ -109,13 +142,13 @@ private long processProperties(Map<String, PropertyDefinition> propertyDefinitio
109142
else {
110143
final String envValue = environmentVariablesProvider.get(definition.getEnv());
111144
if (envValue != null) {
112-
final String expectedValue = mapAndValidateValue(definition, envValue);
145+
final String targetValue = mapAndValidateValue(definition, envValue);
113146

114147
final String propValue = properties.getProperty(name);
115148

116-
if (!Objects.equals(expectedValue, propValue)) {
117-
log.debug("Setting property {} to new value '{}'", name, needsValueRedacted(name) ? "***" : expectedValue);
118-
properties.setProperty(name, expectedValue);
149+
if (!Objects.equals(targetValue, propValue)) {
150+
log.debug("Setting property {} to new value '{}'", name, needsValueRedacted(name) ? "***" : targetValue);
151+
properties.setProperty(name, targetValue);
119152
++modifiedViaDefinitions;
120153
}
121154
}

src/test/java/me/itzg/helpers/properties/SetPropertiesCommandTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ void handlesExistingUnicodePropertyValue(String filename) throws IOException {
195195
.containsIgnoringNewLines("key1=value1");
196196
}
197197

198+
@Test
199+
void encodesWithGivenEncoding() {
200+
final Path outputProperties = tempDir.resolve("ascii.properties");
201+
202+
final int exitCode = new CommandLine(new SetPropertiesCommand()
203+
.setEnvironmentVariablesProvider(MappedEnvVarProvider.of(
204+
"MOTD", "§ctest"
205+
))
206+
)
207+
.execute(
208+
"--definitions", definitionsFile.toString(),
209+
"--escape-unicode",
210+
outputProperties.toString()
211+
);
212+
213+
assertThat(exitCode).isEqualTo(ExitCode.OK);
214+
215+
//noinspection UnnecessaryUnicodeEscape
216+
assertThat(outputProperties)
217+
.content()
218+
.containsIgnoringNewLines("motd=\\u00A7ctest");
219+
220+
}
221+
198222
private void assertPropertiesEqualExcept(Properties properties, String... propertiesToIgnore) {
199223
final HashSet<Object> actualKeys = new HashSet<>(properties.keySet());
200224
Arrays.asList(propertiesToIgnore).forEach(actualKeys::remove);

src/test/resources/properties/property-definitions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"server-portv6": {
99
"env": "SERVER_PORT_V6"
1010
},
11+
"motd": {
12+
"env": "MOTD"
13+
},
1114
"gamemode": {
1215
"env": "GAMEMODE",
1316
"allowed": ["survival","creative","adventure"],

0 commit comments

Comments
 (0)