Skip to content

Commit

Permalink
log: bannerResource can supply replacement map
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Nov 17, 2017
1 parent f808f36 commit 277d1b9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.nio.file.Files;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -221,13 +223,33 @@ public ToolBelt banner(String text) {
* @return this
*/
public ToolBelt bannerResource(String resource) {
return bannerResource(resource, null);
}

/**
* Display banner for top level help
*
* @param resource resource path
* @param replacements a map of Regex->replacement, to replace values in the loaded resource
*
* @return this
*/
public ToolBelt bannerResource(String resource, Map<String, String> replacements) {
this.commands.banner = () -> {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(resource);
if (null != resourceAsStream) {
try {
String result;
try (BufferedReader is = new BufferedReader(new InputStreamReader(resourceAsStream))) {
return is.lines().collect(Collectors.joining("\n"));
result = is.lines().collect(Collectors.joining("\n"));
}
if (replacements != null && !replacements.isEmpty()) {
for (String s : replacements.keySet()) {
String val = replacements.get(s);
result = result.replaceAll(s, Matcher.quoteReplacement(val));
}
}
return result;
} catch (IOException e) {

}
Expand Down

0 comments on commit 277d1b9

Please sign in to comment.