You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2012/05/31 10:58:21 UTC

svn commit: r1344603 - /karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CatAction.java

Author: cschneider
Date: Thu May 31 08:58:20 2012
New Revision: 1344603

URL: http://svn.apache.org/viewvc?rev=1344603&view=rev
Log:
KARAF-754: Adding - option for printing stdin

Modified:
    karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CatAction.java

Modified: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CatAction.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CatAction.java?rev=1344603&r1=1344602&r2=1344603&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CatAction.java (original)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/CatAction.java Thu May 31 08:58:20 2012
@@ -16,13 +16,16 @@
  */
 package org.apache.karaf.shell.commands.impl;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.File;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.net.URL;
 import java.net.MalformedURLException;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.karaf.shell.commands.Argument;
@@ -32,7 +35,7 @@ import org.apache.karaf.shell.console.Ab
 
 /**
  * Concatenate and print files and/or URLs.
- *
+ * 
  * @version $Rev: 593392 $ $Date: 2007-11-09 03:14:15 +0100 (Fri, 09 Nov 2007) $
  */
 @Command(scope = "shell", name = "cat", description = "Displays the content of a file or URL.")
@@ -41,53 +44,66 @@ public class CatAction extends AbstractA
     @Option(name = "-n", aliases = {}, description = "Number the output lines, starting at 1.", required = false, multiValued = false)
     private boolean displayLineNumbers;
 
-    @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespace (use - for STDIN)", required = true, multiValued = true)
+    @Option(name = "-", description = "Use stdin")
+    private boolean stdin;
+
+    @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespace (use - for STDIN)", required = false, multiValued = true)
     private List<String> paths;
 
     protected Object doExecute() throws Exception {
-        //
-        // Support "-" if length is one, and read from io.in
-        // This will help test command pipelines.
-        //
-        if (paths.size() == 1 && "-".equals(paths.get(0))) {
-            log.info("Printing STDIN");
-            cat(new BufferedReader(new InputStreamReader(System.in)));
-        }
-        else {
-            for (String filename : paths) {
-                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);
-                }
-                finally {
-                    try {
-                        reader.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
+        if (stdin) {
+            paths = Collections.singletonList("-");
+        }
+
+        if (paths == null) {
+            throw new RuntimeException("Need to supply a path");
+        }
+
+        for (String filename : paths) {
+            BufferedReader reader = new BufferedReader(createReader(filename));
+            try {
+                cat(reader);
+            } finally {
+                closeReader(reader);
             }
         }
 
         return null;
     }
 
-    private void cat(final BufferedReader reader) throws IOException
-    {
+    private void closeReader(BufferedReader reader) {
+        try {
+            reader.close();
+        } catch (IOException e) {
+            // Ignore
+        }
+    }
+
+    /**
+     * Create a reader for a url orfor a file
+     * 
+     * @param urlOrfileName
+     * @return
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    private Reader createReader(String urlOrfileName) throws IOException, FileNotFoundException {
+        if ("-".equals(urlOrfileName)) {
+            log.debug("Printing STDIN");
+            return new InputStreamReader(System.in);
+        }
+        try {
+            URL url = new URL(urlOrfileName);
+            log.debug("Printing URL: " + url);
+            return new InputStreamReader(url.openStream());
+        } catch (MalformedURLException ignore) {
+            File file = new File(urlOrfileName);
+            log.debug("Printing file: " + file);
+            return new FileReader(file);
+        }
+    }
+
+    private void cat(final BufferedReader reader) throws IOException {
         String line;
         int lineno = 1;