You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2020/08/12 16:38:26 UTC

[sling-org-apache-sling-pipes] branch master updated: SLING-9556 enhancements

This is an automated email from the ASF dual-hosted git repository.

npeltier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git


The following commit(s) were added to refs/heads/master by this push:
     new 710eb51  SLING-9556 enhancements
710eb51 is described below

commit 710eb51f149f95f24bc11ac0581c56a037bf8691
Author: Nicolas Peltier <np...@apache.org>
AuthorDate: Wed Aug 12 18:38:07 2020 +0200

    SLING-9556 enhancements
    
    - moved from / to | separator,
    - allowed re-use of previous pipes in the same command file
---
 .../sling/pipes/internal/CommandExecutorImpl.java  | 10 ++++++++--
 .../pipes/internal/CommandExecutorImplTest.java    | 22 +++++++++++++++++-----
 .../{testcommand.txt => chainedCommand.txt}        |  8 +++++---
 src/test/resources/testcommand.txt                 |  4 ++--
 4 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
index 0cd8bea..ade81a2 100644
--- a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
@@ -75,9 +75,10 @@ public class CommandExecutorImpl extends SlingAllMethodsServlet implements Comma
     public static final String RESOURCE_TYPE = "slingPipes/exec";
     static final String REQ_PARAM_FILE = "pipe_cmdfile";
     static final String REQ_PARAM_CMD = "pipe_cmd";
+    static final String CMD_LINE_PREFIX = "cmd_line_";
     static final String WHITE_SPACE_SEPARATOR = "\\s";
     static final String COMMENT_PREFIX = "#";
-    static final String SEPARATOR = "/";
+    static final String SEPARATOR = "|";
     static final String PARAMS = "@";
     static final String KEY_VALUE_SEP = "=";
     static final String FIRST_TOKEN = "first";
@@ -147,11 +148,16 @@ public class CommandExecutorImpl extends SlingAllMethodsServlet implements Comma
             if (cmds.isEmpty()) {
                 writer.println("No command to execute!");
             }
+            short idx_line = 0;
             for (String command : cmds) {
                 if (StringUtils.isNotBlank(command)) {
+                    JsonWriter pipeWriter = new JsonWriter();
+                    pipeWriter.starts();
                     currentCommand = command;
                     PipeBuilder pipeBuilder = parse(resolver, command.split(WHITE_SPACE_SEPARATOR));
-                    writer.println(pipeBuilder.run(bindings));
+                    Pipe pipe = pipeBuilder.build();
+                    bindings.put(CMD_LINE_PREFIX + idx_line++, pipe.getResource().getPath());
+                    writer.println(plumber.execute(resolver, pipe, bindings, pipeWriter, true));
                 }
             }
             response.setStatus(SC_OK);
diff --git a/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java b/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
index 4464978..346b346 100644
--- a/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
@@ -63,7 +63,7 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
         CommandExecutorImpl.Token token = tokens.get(0);
         assertEquals("pipe key should be 'some'","some", token.pipeKey);
         assertEquals("pipe args should be isolated, items", Arrays.asList("isolated","items"), token.args);
-        String tokenString = "first arg / second firstarg secondarg @ name second / third blah";
+        String tokenString = "first arg | second firstarg secondarg @ name second | third blah";
         tokens = commands.parseTokens(tokenString.split("\\s"));
         assertEquals("there should be 3 tokens", 3, tokens.size());
         assertEquals("keys check", Arrays.asList("first","second", "third"), tokens.stream().map(t -> t.pipeKey).collect(Collectors.toList()));
@@ -87,7 +87,7 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
 
     @Test
     public void testSimpleChainedConf() throws Exception {
-        PipeBuilder builder = commands.parse(context.resourceResolver(),"echo /content/fruits / write some=test key=value".split("\\s"));
+        PipeBuilder builder = commands.parse(context.resourceResolver(),"echo /content/fruits | write some=test key=value".split("\\s"));
         assertNotNull("there should be a resource", builder.run());
         ValueMap props = context.currentResource(PATH_FRUITS).getValueMap();
         assertEquals("there should some=test", "test", props.get("some"));
@@ -128,7 +128,7 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
     @Test
     public void testChainedConfWithInternalOptions() throws Exception {
         PipeBuilder builder = commands.parse(context.resourceResolver(),
-        "echo /content/fruits @ name fruits / write some=${path.fruits} key=value".split("\\s"));
+        "echo /content/fruits @ name fruits | write some=${path.fruits} key=value".split("\\s"));
         assertNotNull("there should be a resource", builder.run());
         ValueMap props = context.currentResource(PATH_FRUITS).getValueMap();
         assertEquals("there should some=/content/fruits", PATH_FRUITS, props.get("some"));
@@ -138,7 +138,7 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
     @Test
     public void adaptToDemoTest() throws Exception {
         String url = "'http://99-bottles-of-beer.net/lyrics.html'";
-        String cmd = "egrep " + url + " @ name bottles @ with 'pattern=(?<number>\\d(\\d)?)' / mkdir '/var/bottles/${bottles.number}'";
+        String cmd = "egrep " + url + " @ name bottles @ with pattern=(?<number>\\d(\\d)?) | mkdir /var/bottles/${bottles.number}";
         PipeBuilder builder = commands.parse(context.resourceResolver(), cmd.split("\\s"));
         ContainerPipe pipe = (ContainerPipe)builder.build();
         ValueMap regexp = pipe.getResource().getChild("conf/bottles").getValueMap();
@@ -168,7 +168,7 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
     @Test
     public void testSimpleCommandServlet() throws IOException, ServletException {
         Map<String, Object> params = new HashMap<>();
-        params.put(CommandExecutorImpl.REQ_PARAM_CMD, "echo /content / mkdir foo / write type=bar");
+        params.put(CommandExecutorImpl.REQ_PARAM_CMD, "echo /content | mkdir foo | write type=bar");
         String response = testServlet(params);
         assertEquals("{\"items\":[\"/content/foo\"],\"size\":1}\n", response);
     }
@@ -184,4 +184,16 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
             + "\"size\":4}\n"
             + "{\"items\":[\"/content/beatles/ringo/jcr:content\"],\"size\":1}\n", response);
     }
+
+    @Test
+    public void testChainedCommand() throws IOException, ServletException {
+        Map<String, Object> params = new HashMap<>();
+        params.put(CommandExecutorImpl.REQ_PARAM_FILE, IOUtils.toString(getClass().getResourceAsStream("/chainedCommand"
+            + ".txt"), "UTF-8"));
+        String response = testServlet(params);
+        assertEquals("{\"items\":[],\"size\":0}\n"
+            + "{\"items\":[\"/content/fruits/banana/isnota/pea\",\"/content/fruits/banana/isnota/carrot\","
+            + "\"/content/fruits/apple/isnota/pea\",\"/content/fruits/apple/isnota/plum\","
+            + "\"/content/fruits/apple/isnota/carrot\"],\"size\":5}\n", response);
+    }
 }
\ No newline at end of file
diff --git a/src/test/resources/testcommand.txt b/src/test/resources/chainedCommand.txt
similarity index 73%
copy from src/test/resources/testcommand.txt
copy to src/test/resources/chainedCommand.txt
index 940c815..865ab09 100644
--- a/src/test/resources/testcommand.txt
+++ b/src/test/resources/chainedCommand.txt
@@ -13,7 +13,9 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-#
-mkdir /content / json ['john','paul','georges','ringo'] @ name beatle / mkdir beatles/${beatle} / write jcr:content/isBeatle=true jcr:content/firstName=${beatle} jcr:content/singer=${'ringo'==beatle?false:true}
 
-echo /content / xpath /jcr:root//element(*,nt:base)[@singer='false']
\ No newline at end of file
+#this pipe returns grand children of the current one (should return blank if context is normal)
+echo isnota | children nt:unstructured
+
+#now we are using above pipe as a reference and we should get stuff
+echo /content | $ /apps/pipes-it/fruit | ref ${cmd_line_0}
\ No newline at end of file
diff --git a/src/test/resources/testcommand.txt b/src/test/resources/testcommand.txt
index 940c815..96c4560 100644
--- a/src/test/resources/testcommand.txt
+++ b/src/test/resources/testcommand.txt
@@ -14,6 +14,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-mkdir /content / json ['john','paul','georges','ringo'] @ name beatle / mkdir beatles/${beatle} / write jcr:content/isBeatle=true jcr:content/firstName=${beatle} jcr:content/singer=${'ringo'==beatle?false:true}
+mkdir /content | json ['john','paul','georges','ringo'] @ name beatle | mkdir beatles/${beatle} | write jcr:content/isBeatle=true jcr:content/firstName=${beatle} jcr:content/singer=${'ringo'==beatle?false:true}
 
-echo /content / xpath /jcr:root//element(*,nt:base)[@singer='false']
\ No newline at end of file
+echo /content | xpath /jcr:root//element(*,nt:base)[@singer='false']
\ No newline at end of file