View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.util.shell.commands;
18  
19  import java.io.BufferedReader;
20  import java.io.FileNotFoundException;
21  import java.io.FileReader;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  
26  import org.apache.accumulo.core.util.shell.Shell;
27  import org.apache.accumulo.core.util.shell.Shell.Command;
28  import org.apache.commons.cli.CommandLine;
29  import org.apache.commons.cli.Option;
30  import org.apache.commons.cli.Options;
31  
32  public class HistoryCommand extends Command {
33    
34    private Option clearHist;
35    
36    @Override
37    public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
38      
39      final String histDir = System.getenv("HOME") + "/.accumulo";
40      int counter = 0;
41      
42      if (cl.hasOption(clearHist.getOpt())) {
43        
44        try {
45          
46          final FileWriter outFile = new FileWriter(histDir + "/shell_history.txt");
47          final PrintWriter out = new PrintWriter(outFile);
48          out.close();
49          
50        } catch (IOException e) {
51          
52          e.printStackTrace();
53        }
54      }
55      
56      else {
57        try {
58          final BufferedReader in = new BufferedReader(new FileReader(histDir + "/shell_history.txt"));
59          String Line;
60          try {
61            Line = in.readLine();
62            while (Line != null) {
63              shellState.getReader().printString(counter + " " + Line);
64              shellState.getReader().printNewline();
65              counter++;
66              Line = in.readLine();
67            }
68          } catch (IOException e) {
69            
70            e.printStackTrace();
71          }
72        } catch (FileNotFoundException e) {
73          
74          e.printStackTrace();
75        }
76      }
77      
78      return 0;
79    }
80    
81    @Override
82    public String description() {
83      return ("generates a list of commands previously executed");
84    }
85    
86    @Override
87    public int numArgs() {
88      return 0;
89    }
90    
91    @Override
92    public Options getOptions() {
93      final Options o = new Options();
94      
95      clearHist = new Option("c", "clear", false, "clear history file");
96      clearHist.setRequired(false);
97      
98      o.addOption(clearHist);
99      
100     return o;
101   }
102 }