|
21 | 21 | */ |
22 | 22 |
|
23 | 23 | import com.walmartlabs.concord.common.ConfigurationUtils; |
24 | | -import org.ini4j.Ini; |
25 | | -import org.ini4j.Profile; |
26 | 24 | import org.slf4j.Logger; |
27 | 25 | import org.slf4j.LoggerFactory; |
28 | 26 |
|
29 | 27 | import java.io.IOException; |
30 | | -import java.nio.charset.StandardCharsets; |
31 | 28 | import java.nio.file.Files; |
32 | 29 | import java.nio.file.Path; |
33 | 30 | import java.nio.file.StandardOpenOption; |
|
37 | 34 |
|
38 | 35 | import static com.walmartlabs.concord.sdk.MapUtils.getMap; |
39 | 36 | import static com.walmartlabs.concord.sdk.MapUtils.getString; |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
40 | 38 |
|
41 | 39 | public class AnsibleConfig { |
42 | 40 |
|
@@ -87,7 +85,7 @@ public Path write() { |
87 | 85 |
|
88 | 86 | Path cfgPath = getConfigPath(); |
89 | 87 | try { |
90 | | - Files.write(cfgPath, b.toString().getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); |
| 88 | + Files.write(cfgPath, b.toString().getBytes(UTF_8), StandardOpenOption.CREATE); |
91 | 89 | } catch (IOException e) { |
92 | 90 | log.error("Configuration write {} error", CFG_FILE_NAME, e); |
93 | 91 | throw new RuntimeException("Configuration write error: " + e.getMessage()); |
@@ -186,21 +184,43 @@ private static StringBuilder addCfgSection(StringBuilder b, String name, Map<Str |
186 | 184 |
|
187 | 185 | private Map<String, Map<String, Object>> loadFromFile(Path file) { |
188 | 186 | try { |
189 | | - Ini ini = new Ini(); |
190 | | - ini.load(file.toFile()); |
191 | | - |
192 | | - Map<String, Map<String, Object>> result = new HashMap<>(); |
193 | | - for (Map.Entry<String, Profile.Section> e : ini.entrySet()) { |
194 | | - Map<String, Object> section = new HashMap<>(); |
195 | | - for (Map.Entry<String, String> s : e.getValue().entrySet()) { |
196 | | - section.put(s.getKey(), s.getValue()); |
197 | | - } |
198 | | - result.put(e.getKey(), section); |
199 | | - } |
200 | | - return result; |
| 187 | + return parseIniFile(file); |
201 | 188 | } catch (IOException e) { |
202 | 189 | log.error("Configuration parse error: {}", e.getMessage()); |
203 | 190 | throw new RuntimeException("Configuration parse error " + file + ": " + e.getMessage()); |
204 | 191 | } |
205 | 192 | } |
| 193 | + |
| 194 | + private static Map<String, Map<String, Object>> parseIniFile(Path file) throws IOException { |
| 195 | + Map<String, Map<String, Object>> result = new HashMap<>(); |
| 196 | + Map<String, Object> currentSection = null; |
| 197 | + for (String line : Files.readAllLines(file, UTF_8)) { |
| 198 | + line = line.trim(); |
| 199 | + |
| 200 | + if (line.isEmpty() || line.startsWith("#") || line.startsWith(";")) { |
| 201 | + continue; |
| 202 | + } |
| 203 | + |
| 204 | + if (line.startsWith("[") && line.endsWith("]")) { |
| 205 | + String sectionName = line.substring(1, line.length() - 1).trim(); |
| 206 | + currentSection = new HashMap<>(); |
| 207 | + result.put(sectionName, currentSection); |
| 208 | + continue; |
| 209 | + } |
| 210 | + |
| 211 | + if (currentSection != null && line.contains("=")) { |
| 212 | + int equalIndex = line.indexOf("="); |
| 213 | + String key = line.substring(0, equalIndex).trim(); |
| 214 | + String value = line.substring(equalIndex + 1).trim(); |
| 215 | + |
| 216 | + if ((value.startsWith("\"") && value.endsWith("\"")) || |
| 217 | + (value.startsWith("'") && value.endsWith("'"))) { |
| 218 | + value = value.substring(1, value.length() - 1); |
| 219 | + } |
| 220 | + |
| 221 | + currentSection.put(key, value); |
| 222 | + } |
| 223 | + } |
| 224 | + return result; |
| 225 | + } |
206 | 226 | } |
0 commit comments