You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by gn...@apache.org on 2008/10/17 10:23:56 UTC

svn commit: r705508 - in /geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main: java/org/apache/geronimo/gshell/commands/optional/ resources/META-INF/spring/ resources/org/apache/geronimo/gshell/commands/optional/

Author: gnodet
Date: Fri Oct 17 01:23:56 2008
New Revision: 705508

URL: http://svn.apache.org/viewvc?rev=705508&view=rev
Log:
Add two commands: cat and grep (cat could be further integrated with vfs as it uses URLs atm)

Added:
    geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/CatAction.java
    geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/GrepAction.java
    geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/CatAction.properties
    geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/GrepAction.properties
Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/META-INF/spring/components.xml

Added: geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/CatAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/CatAction.java?rev=705508&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/CatAction.java (added)
+++ geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/CatAction.java Fri Oct 17 01:23:56 2008
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, 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.
+ */
+
+package org.apache.geronimo.gshell.commands.optional;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.List;
+import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.InputStreamReader;
+import java.io.File;
+import java.net.URL;
+import java.net.MalformedURLException;
+
+import org.apache.geronimo.gshell.command.CommandAction;
+import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.io.IO;
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.IOUtil;
+
+/**
+ * Display the content of a file or URL.
+ *
+ * @version $Rev: 700354 $ $Date: 2008-09-30 08:03:46 +0200 (Tue, 30 Sep 2008) $
+ */
+public class CatAction
+    implements CommandAction
+{
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Option(name="-n", description="Number the output lines, starting at 1")
+    private boolean displayLineNumbers;
+
+    @Argument(description="File or URL", required=true)
+    private List<String> args;
+
+    public Object execute(final CommandContext context) throws Exception {
+        assert context != null;
+        IO io = context.getIo();
+
+        //
+        // Support "-" if length is one, and read from io.in
+        // This will help test command pipelines.
+        //
+        if (args.size() == 1 && "-".equals(args.get(0))) {
+            log.info("Printing STDIN");
+            cat(new BufferedReader(io.in), io);
+        }
+        else {
+            for (String filename : args) {
+                BufferedReader reader;
+
+                // First try a URL
+                try {
+                    URL url = new URL(filename);
+                    log.info("Printing URL: " + url);
+                    reader = new BufferedReader(new InputStreamReader(url.openStream()));
+                }
+                catch (MalformedURLException ignore) {
+                    // They try a file
+                    File file = new File(filename);
+                    log.info("Printing file: " + file);
+                    reader = new BufferedReader(new FileReader(file));
+                }
+
+                try {
+                    cat(reader, io);
+                }
+                finally {
+                    IOUtil.close(reader);
+                }
+            }
+        }
+
+        return Result.SUCCESS;
+    }
+
+    private void cat(final BufferedReader reader, final IO io) throws IOException {
+        String line;
+        int lineno = 1;
+
+        while ((line = reader.readLine()) != null) {
+            if (displayLineNumbers) {
+                String gutter = StringUtils.leftPad(String.valueOf(lineno++), 6);
+                io.out.print(gutter);
+                io.out.print("  ");
+            }
+            io.out.println(line);
+        }
+    }
+
+}

Added: geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/GrepAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/GrepAction.java?rev=705508&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/GrepAction.java (added)
+++ geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/java/org/apache/geronimo/gshell/commands/optional/GrepAction.java Fri Oct 17 01:23:56 2008
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, 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.
+ */
+
+package org.apache.geronimo.gshell.commands.optional;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.regex.Pattern;
+import java.io.Reader;
+import java.io.IOException;
+
+import org.apache.geronimo.gshell.command.CommandAction;
+import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.io.IO;
+import org.apache.geronimo.gshell.clp.Option;
+import org.apache.geronimo.gshell.clp.Argument;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * Filter the input stream given some regular expression.
+ *
+ * @version $Rev: 700354 $ $Date: 2008-09-30 08:03:46 +0200 (Tue, 30 Sep 2008) $
+ */
+public class GrepAction
+    implements CommandAction
+{
+
+    @Argument(required=true, description="Regular expression")
+    private String regex;
+
+    @Option(name="-n", aliases = { "--line-number" }, description="Prefix each line of output with the line number within its input file.")
+    private boolean lineNumber;
+
+    @Option(name = "-v", aliases = { "--invert-match" }, description = "Invert the sense of matching, to select non-matching lines.")
+    private boolean invertMatch;
+
+    @Option(name = "-w", aliases = { "--word-regexp" }, description = "Select only those lines containing matches that form whole " +
+                                                                      "words.  The test is that the matching substring must either be " +
+                                                                      "at  the beginning of the line, or preceded by a non-word constituent " +
+                                                                      "character.  Similarly, it must be either at the end of " +
+                                                                      "the line or followed by a non-word constituent character.  " +
+                                                                      "Word-constituent characters are letters, digits, and the underscore.")
+    private boolean wordRegexp;
+
+    @Option(name = "-x", aliases = { "--line-regexp" }, description = "Select only those matches that exactly match the whole line.")
+    private boolean lineRegexp;
+
+    public Object execute(final CommandContext context) throws Exception {
+        assert context != null;
+        IO io = context.getIo();
+
+        String regexp = regex;
+        if (wordRegexp) {
+            regexp = "\\b" + regexp + "\\b";
+        }
+        if (lineRegexp) {
+            regexp = "^" + regexp + "$";
+        } else {
+            regexp = ".*" + regexp + ".*";
+        }
+        Pattern p = Pattern.compile(regexp);
+        try {
+            int lineno = 1;
+            String line;
+            while ((line = readLine(io.in)) != null) {
+                if (p.matcher(line).matches() ^ invertMatch) {
+                    if (lineNumber) {
+                        String gutter = StringUtils.leftPad(String.valueOf(lineno), 6);
+                        io.out.print(gutter);
+                        io.out.print("  ");
+                    }
+                    io.out.println(line);
+                    lineno++;
+                }
+            }
+        } catch (IOException e) {
+        }
+        return null;
+    }
+
+    private String readLine(Reader in) throws IOException {
+        StringBuffer buf = new StringBuffer();
+        while (true) {
+            int i = in.read();
+            if (i == -1 && buf.length() == 0) {
+                throw new IOException("break");
+            }
+            if (i == -1 || i == '\n' || i == '\r') {
+                return buf.toString();
+            }
+            buf.append((char) i);
+        }
+    }
+
+}

Modified: geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/META-INF/spring/components.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/META-INF/spring/components.xml?rev=705508&r1=705507&r2=705508&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/META-INF/spring/components.xml (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/META-INF/spring/components.xml Fri Oct 17 01:23:56 2008
@@ -48,6 +48,14 @@
             <gshell:command name="optional/hostname">
                 <gshell:action class="org.apache.geronimo.gshell.commands.optional.HostnameAction"/>
             </gshell:command>
+
+            <gshell:command name="optional/cat">
+                <gshell:action class="org.apache.geronimo.gshell.commands.optional.CatAction"/>
+            </gshell:command>
+
+            <gshell:command name="optional/grep">
+                <gshell:action class="org.apache.geronimo.gshell.commands.optional.GrepAction"/>
+            </gshell:command>
         </gshell:command-bundle>
     </gshell:plugin>
 

Added: geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/CatAction.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/CatAction.properties?rev=705508&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/CatAction.properties (added)
+++ geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/CatAction.properties Fri Oct 17 01:23:56 2008
@@ -0,0 +1,27 @@
+##
+## Licensed to the Apache Software Foundation (ASF) under one
+## or more contributor license agreements.  See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership.  The ASF licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License.  You may obtain a copy of the License at
+##
+##  http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing,
+## software distributed under the License is distributed on an
+## "AS IS" BASIS, 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.
+##
+
+##
+## $Rev: 703511 $ $Date: 2008-10-10 18:07:36 +0200 (Fri, 10 Oct 2008) $
+##
+
+command.description=Display the content of a file or URL.
+
+command.manual=\
+  TODO: date manual
\ No newline at end of file

Added: geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/GrepAction.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/GrepAction.properties?rev=705508&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/GrepAction.properties (added)
+++ geronimo/gshell/trunk/gshell-commands/gshell-optional/src/main/resources/org/apache/geronimo/gshell/commands/optional/GrepAction.properties Fri Oct 17 01:23:56 2008
@@ -0,0 +1,27 @@
+##
+## Licensed to the Apache Software Foundation (ASF) under one
+## or more contributor license agreements.  See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership.  The ASF licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License.  You may obtain a copy of the License at
+##
+##  http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing,
+## software distributed under the License is distributed on an
+## "AS IS" BASIS, 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.
+##
+
+##
+## $Rev: 703511 $ $Date: 2008-10-10 18:07:36 +0200 (Fri, 10 Oct 2008) $
+##
+
+command.description=Filter the input stream given some regular expression.
+
+command.manual=\
+  TODO: date manual
\ No newline at end of file