fix(cli): 修复没有转义配置文件中的$符号

This commit is contained in:
2024-02-28 14:30:09 +08:00
parent de78898a60
commit 55c4be2479
2 changed files with 21 additions and 2 deletions

View File

@@ -21,8 +21,8 @@ public class ServiceInfoWrapper {
public ServiceInfoWrapper(String name, ServiceInfo serviceInfo) {
this.name = name;
this.serviceInfo = serviceInfo;
this.environments = SpringPropertiesEscapeHelper.escapeMapKey(serviceInfo.getEnvironments());
this.arguments = SpringPropertiesEscapeHelper.escapeMapKey(serviceInfo.getArguments());
this.environments = SpringPropertiesEscapeHelper.escapeMap(serviceInfo.getEnvironments());
this.arguments = SpringPropertiesEscapeHelper.escapeMap(serviceInfo.getArguments());
}
public Boolean getEnabled() {

View File

@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.service.cli.runner;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* spring boot 配置文件转义
@@ -10,6 +11,9 @@ import java.util.Map;
* @date 2024-02-25
*/
public class SpringPropertiesEscapeHelper {
public static <T> Map<String, T> escapeMap(Map<String, T> map) {
return escapeMapValue(escapeMapKey(map));
}
/**
* 使用 _ 代替 map key 的 .
*/
@@ -20,4 +24,19 @@ public class SpringPropertiesEscapeHelper {
}
return result;
}
public static <T> Map<String, T> escapeMapValue(Map<String, T> map) {
// https://github.com/spring-projects/spring-framework/issues/9628
// spring boot 的配置文件没有办法转义 $,使用 $\{ -> ${ 来绕过一下
return map.entrySet()
.stream()
.peek(entry -> {
if (entry.getValue() instanceof String) {
String value = (String) entry.getValue();
//noinspection unchecked
entry.setValue((T) value.replace("$\\{", "${"));
}
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}