Index: shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java =================================================================== --- shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java (revision 0) +++ shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java (revision 0) @@ -0,0 +1,100 @@ +/* + * 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.karaf.shell.commands; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import org.apache.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.felix.gogo.commands.Option; +import org.apache.karaf.shell.console.AbstractAction; + +@Command(scope = "shell", name = "head", description = "Displays the first lines of a file") +public class HeadAction extends AbstractAction { + + private static final int DEFAULT_NUMBER_OF_LINES = 10; + + @Option(name = "-n", aliases = {}, description = "The number of lines to display, starting at 1.", required = false, multiValued = false) + private int numberOfLines; + + @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces (use - for STDIN)", required = true, multiValued = true) + private List 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("Heading STDIN"); + head(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("Heading URL: " + url); + reader = new BufferedReader(new InputStreamReader(url.openStream())); + } + catch (MalformedURLException ignore) { + // They try a file + File file = new File(filename); + log.info("Heading file: " + file); + reader = new BufferedReader(new FileReader(file)); + } + + try { + head(reader); + } + finally { + try { + reader.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + + return null; + } + + private void head(final BufferedReader reader) throws IOException + { + String line; + int lineno = 1; + + if (numberOfLines < 1) { + numberOfLines = DEFAULT_NUMBER_OF_LINES; + } + + while ((line = reader.readLine()) != null && lineno <= numberOfLines) { + System.out.println(line); + lineno++; + } + } +} Index: shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java =================================================================== --- shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java (revision 0) +++ shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java (revision 0) @@ -0,0 +1,107 @@ +/* + * 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.karaf.shell.commands; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.LinkedList; +import java.util.List; + +import org.apache.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.felix.gogo.commands.Option; +import org.apache.karaf.shell.console.AbstractAction; + +@Command(scope = "shell", name = "tail", description = "Displays the last lines of a file") +public class TailAction extends AbstractAction { + + private static final int DEFAULT_NUMBER_OF_LINES = 10; + + @Option(name = "-n", aliases = {}, description = "The number of lines to display, starting at 1.", required = false, multiValued = false) + private int numberOfLines; + + @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces (use - for STDIN)", required = true, multiValued = true) + private List 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("Tailing STDIN"); + tail(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("Tailing URL: " + url); + reader = new BufferedReader(new InputStreamReader(url.openStream())); + } + catch (MalformedURLException ignore) { + // They try a file + File file = new File(filename); + log.info("Tailing file: " + file); + reader = new BufferedReader(new FileReader(file)); + } + + try { + tail(reader); + } + finally { + try { + reader.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + + return null; + } + + private void tail(final BufferedReader reader) throws IOException + { + List lines = new LinkedList(); + String line; + int lineno = 1; + + while ((line = reader.readLine()) != null) { + lines.add(line); + } + + if (numberOfLines < 1) { + numberOfLines = DEFAULT_NUMBER_OF_LINES; + } + + int startLine = lines.size() < numberOfLines ? 0 : lines.size() - numberOfLines - 1; + + for (lineno = startLine; lineno < lines.size(); lineno++) { + System.out.println(lines.get(lineno)); + } + } +} Index: shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands =================================================================== --- shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands (revision 1043675) +++ shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands (working copy) @@ -21,6 +21,7 @@ org.apache.karaf.shell.commands.ExecuteAction org.apache.karaf.shell.commands.GrepAction org.apache.karaf.shell.commands.HistoryAction +org.apache.karaf.shell.commands.HeadAction org.apache.karaf.shell.commands.IfAction org.apache.karaf.shell.commands.JavaAction org.apache.karaf.shell.commands.LogoutAction @@ -30,3 +31,4 @@ org.apache.karaf.shell.commands.SleepAction org.apache.karaf.shell.commands.SortAction org.apache.karaf.shell.commands.TacAction +org.apache.karaf.shell.commands.TailAction Index: shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml =================================================================== --- shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (revision 1043675) +++ shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (working copy) @@ -41,6 +41,9 @@ + + + @@ -78,6 +81,9 @@ + + +