1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }