|
1 | 1 | package org.csanchez.jenkins.plugins.kubernetes;
|
2 | 2 |
|
| 3 | +import static hudson.Util.*; |
3 | 4 | import static java.util.stream.Collectors.*;
|
4 | 5 | import static org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate.*;
|
5 | 6 |
|
|
10 | 11 | import java.util.List;
|
11 | 12 | import java.util.Map;
|
12 | 13 | import java.util.Set;
|
13 |
| -import java.util.regex.Matcher; |
14 |
| -import java.util.regex.Pattern; |
15 | 14 |
|
16 | 15 | import javax.annotation.CheckForNull;
|
17 | 16 | import javax.annotation.Nonnull;
|
|
23 | 22 | import com.google.common.base.Preconditions;
|
24 | 23 | import com.google.common.base.Strings;
|
25 | 24 |
|
| 25 | +import hudson.Util; |
26 | 26 | import hudson.model.Label;
|
27 | 27 | import hudson.model.Node;
|
28 | 28 | import hudson.tools.ToolLocationNodeProperty;
|
29 | 29 |
|
30 | 30 | public class PodTemplateUtils {
|
31 | 31 |
|
32 |
| - private static final String PLACEHOLDER_KEY = "key"; |
33 |
| - private static final String PLACEHOLDER_FORMAT = "\\$\\{%s\\}"; |
34 |
| - private static final String PLACEHOLDER_REGEX = String.format(PLACEHOLDER_FORMAT, "(?<" + PLACEHOLDER_KEY + ">[a-zA-Z0-9_]+)"); |
35 |
| - private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile(PLACEHOLDER_REGEX); |
36 |
| - |
37 | 32 | /**
|
38 | 33 | * Combines a {@link ContainerTemplate} with its parent.
|
39 | 34 | * @param parent The parent container template (nullable).
|
@@ -221,52 +216,44 @@ public static PodTemplate getTemplateByName(@CheckForNull String name, Collectio
|
221 | 216 | * @return The substituted value if found, or the input value otherwise.
|
222 | 217 | */
|
223 | 218 | public static String substituteEnv(String s) {
|
224 |
| - return substitute(s, System.getenv()); |
| 219 | + return replaceMacro(s, System.getenv()); |
225 | 220 | }
|
226 | 221 |
|
227 | 222 | /**
|
228 | 223 | * Substitutes a placeholder with a value found in the environment.
|
| 224 | + * @deprecated check if it is null or empty in the caller method, then use {@link #substituteEnv(String)} |
229 | 225 | * @param s The placeholder. Should be use the format: ${placeholder}.
|
230 | 226 | * @param defaultValue The default value to return if no match is found.
|
231 | 227 | * @return The substituted value if found, or the default value otherwise.
|
232 | 228 | */
|
| 229 | + @Deprecated |
233 | 230 | public static String substituteEnv(String s, String defaultValue) {
|
234 | 231 | return substitute(s, System.getenv(), defaultValue);
|
235 | 232 | }
|
236 | 233 |
|
237 | 234 | /**
|
238 | 235 | * Substitutes a placeholder with a value found in the specified map.
|
| 236 | + * @deprecated use {@link Util#replaceMacro(String, Map)} |
239 | 237 | * @param s The placeholder. Should be use the format: ${placeholder}.
|
240 | 238 | * @param properties The map with the key value pairs to use for substitution.
|
241 | 239 | * @return The substituted value if found, or the input value otherwise.
|
242 | 240 | */
|
| 241 | + @Deprecated |
243 | 242 | public static String substitute(String s, Map<String, String> properties) {
|
244 |
| - return substitute(s, properties, null); |
| 243 | + return replaceMacro(s, properties); |
245 | 244 | }
|
246 | 245 |
|
247 | 246 | /**
|
248 | 247 | * Substitutes a placeholder with a value found in the specified map.
|
| 248 | + * @deprecated check if it is null or empty in the caller method, then use {@link #substitute(String,Map)} |
249 | 249 | * @param s The placeholder. Should be use the format: ${placeholder}.
|
250 | 250 | * @param properties The map with the key value pairs to use for substitution.
|
251 | 251 | * @param defaultValue The default value to return if no match is found.
|
252 | 252 | * @return The substituted value if found, or the default value otherwise.
|
253 | 253 | */
|
| 254 | + @Deprecated |
254 | 255 | public static String substitute(String s, Map<String, String> properties, String defaultValue) {
|
255 |
| - if (Strings.isNullOrEmpty(s)) { |
256 |
| - return defaultValue; |
257 |
| - } |
258 |
| - |
259 |
| - Matcher m = PLACEHOLDER_PATTERN.matcher(s); |
260 |
| - while (m.find()) { |
261 |
| - String key = m.group(PLACEHOLDER_KEY); |
262 |
| - String val = properties.get(key); |
263 |
| - if (val != null) { |
264 |
| - s = s.replaceAll(String.format(PLACEHOLDER_FORMAT, key), Matcher.quoteReplacement(val)); |
265 |
| - } else if (defaultValue != null) { |
266 |
| - s = s.replaceAll(String.format(PLACEHOLDER_FORMAT, key), defaultValue); |
267 |
| - } |
268 |
| - } |
269 |
| - return s; |
| 256 | + return Strings.isNullOrEmpty(s) ? defaultValue : replaceMacro(s, properties); |
270 | 257 | }
|
271 | 258 |
|
272 | 259 | private static List<TemplateEnvVar> combineEnvVars(ContainerTemplate parent, ContainerTemplate template) {
|
|
0 commit comments