summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Musum <musum@verizonmedia.com>2019-04-03 07:50:31 +0200
committerGitHub <noreply@github.com>2019-04-03 07:50:31 +0200
commitc393a776efe0d100b4c49511b1c9660b45592f60 (patch)
treeff1e59efb27222ca33794d0dd7df5c653b4711e4
parent4a2506315ac3a4f1c2fc89b7f3ffeb9cef3af7c2 (diff)
parent05272b05f1f0934bffd68b3d8055563c88c70d18 (diff)
Merge pull request #8992 from vespa-engine/hakonhall/minimal-template-class
Minimal template class
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java75
-rw-r--r--node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TemplarTest.java21
2 files changed, 96 insertions, 0 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java
new file mode 100644
index 00000000000..113af76972b
--- /dev/null
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java
@@ -0,0 +1,75 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.hosted.node.admin.task.util.file;
+
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A very simple template engine when there's little complexity and lots of Velocity special characters $ and #,
+ * i.e. typically shell script.
+ *
+ * @author hakonhall
+ */
+public class Templar {
+ private final String template;
+
+ private String prefix = "<%=";
+ private String suffix = "%>";
+
+ private final Map<String, String> settings = new HashMap<>();
+
+ public static Templar fromUtf8File(Path path) {
+ return new Templar(new UnixPath(path).readUtf8File());
+ }
+
+ public Templar(String template) {
+ this.template = template;
+ }
+
+ public Templar set(String name, String value) {
+ settings.put(name, value);
+ return this;
+ }
+
+ public String resolve() {
+ StringBuilder text = new StringBuilder(template.length() * 2);
+
+ int start= 0;
+ int end;
+
+ for (; start < template.length(); start = end) {
+ int prefixStart = template.indexOf(prefix, start);
+
+
+ if (prefixStart == -1) {
+ text.append(template, start, template.length());
+ break;
+ } else {
+ text.append(template, start, prefixStart);
+ }
+
+ int suffixStart = template.indexOf(suffix, prefixStart + prefix.length());
+ if (suffixStart == -1) {
+ throw new IllegalArgumentException("Prefix at offset " + prefixStart + " is not terminated");
+ }
+
+ int prefixEnd = prefixStart + prefix.length();
+ String name = template.substring(prefixEnd, suffixStart).trim();
+ String value = settings.get(name);
+ if (value == null) {
+ throw new IllegalArgumentException("No value is set for name '" + name + "' at offset " + prefixEnd);
+ }
+
+ text.append(value);
+
+ end = suffixStart + suffix.length();
+ }
+
+ return text.toString();
+ }
+
+ public FileWriter getFileWriterTo(Path path) {
+ return new FileWriter(path, this::resolve);
+ }
+}
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TemplarTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TemplarTest.java
new file mode 100644
index 00000000000..34b8312d426
--- /dev/null
+++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TemplarTest.java
@@ -0,0 +1,21 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.hosted.node.admin.task.util.file;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author hakonhall
+ */
+public class TemplarTest {
+ @Test
+ public void test() {
+ Templar templar = new Templar("x y <%= foo %>, some other <%=bar%> text");
+ templar.set("foo", "fidelity")
+ .set("bar", "halimov")
+ .set("not", "used");
+
+ assertEquals("x y fidelity, some other halimov text", templar.resolve());
+ }
+} \ No newline at end of file