11package me .itzg .helpers .properties ;
22
33import com .fasterxml .jackson .core .type .TypeReference ;
4+ import java .io .IOException ;
5+ import java .io .InputStream ;
6+ import java .io .OutputStream ;
47import java .io .Reader ;
58import java .io .Writer ;
6- import java .nio .charset .StandardCharsets ;
79import java .nio .file .Files ;
810import java .nio .file .Path ;
911import 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 }
0 commit comments