mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
transform command: history
This commit is contained in:
@@ -4,8 +4,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.taobao.arthas.core.command.Constants;
|
||||
import com.taobao.arthas.core.command.model.HistoryModel;
|
||||
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
|
||||
import com.taobao.arthas.core.shell.command.CommandProcess;
|
||||
import com.taobao.arthas.core.shell.history.HistoryManager;
|
||||
import com.taobao.arthas.core.shell.history.impl.HistoryManagerImpl;
|
||||
import com.taobao.arthas.core.shell.session.Session;
|
||||
import com.taobao.arthas.core.shell.term.impl.TermImpl;
|
||||
import com.taobao.middleware.cli.annotations.Argument;
|
||||
@@ -69,6 +72,14 @@ public class HistoryCommand extends AnnotatedCommand {
|
||||
|
||||
process.write(sb.toString());
|
||||
}
|
||||
} else {
|
||||
if (clear) {
|
||||
HistoryManagerImpl.getInstance().clearHistory();
|
||||
} else {
|
||||
//http api
|
||||
List<String> history = HistoryManagerImpl.getInstance().getHistory();
|
||||
process.appendResult(new HistoryModel(new ArrayList<String>(history)));
|
||||
}
|
||||
}
|
||||
|
||||
process.end();
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.taobao.arthas.core.command.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author gongdewei 2020/4/8
|
||||
*/
|
||||
public class HistoryModel extends ResultModel {
|
||||
|
||||
private List<String> history;
|
||||
|
||||
public HistoryModel() {
|
||||
}
|
||||
|
||||
public HistoryModel(List<String> history) {
|
||||
this.history = history;
|
||||
}
|
||||
|
||||
public List<String> getHistory() {
|
||||
return history;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "history";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.taobao.arthas.core.shell.history;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author gongdewei 2020/4/8
|
||||
*/
|
||||
public interface HistoryManager {
|
||||
|
||||
void addHistory(String commandLine);
|
||||
|
||||
List<String> getHistory();
|
||||
|
||||
void setHistory(List<String> history);
|
||||
|
||||
void saveHistory();
|
||||
|
||||
void loadHistory();
|
||||
|
||||
void clearHistory();
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.taobao.arthas.core.shell.history.impl;
|
||||
|
||||
import com.taobao.arthas.core.shell.history.HistoryManager;
|
||||
import com.taobao.arthas.core.util.Constants;
|
||||
import com.taobao.arthas.core.util.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author gongdewei 2020/4/8
|
||||
*/
|
||||
public class HistoryManagerImpl implements HistoryManager {
|
||||
/**
|
||||
* The max number of history item that will be saved in memory.
|
||||
*/
|
||||
private static final int MAX_HISTORY_SIZE = 500;
|
||||
|
||||
private List<String> history = new ArrayList<String>();
|
||||
|
||||
private static HistoryManager instance;
|
||||
|
||||
public static HistoryManager getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (HistoryManagerImpl.class) {
|
||||
instance = new HistoryManagerImpl();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private HistoryManagerImpl() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveHistory() {
|
||||
FileUtils.saveCommandHistoryString(history, new File(Constants.CMD_HISTORY_FILE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadHistory() {
|
||||
history = FileUtils.loadCommandHistoryString(new File(Constants.CMD_HISTORY_FILE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearHistory() {
|
||||
this.history.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addHistory(String commandLine) {
|
||||
while (history.size() >= MAX_HISTORY_SIZE) {
|
||||
history.remove(0);
|
||||
}
|
||||
history.add(commandLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getHistory() {
|
||||
return history;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHistory(List<String> history) {
|
||||
this.history = history;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -148,6 +148,55 @@ public class FileUtils {
|
||||
return history;
|
||||
}
|
||||
|
||||
/**
|
||||
* save the command history to the given file, data will be overridden.
|
||||
* @param history the command history
|
||||
* @param file the file to save the history
|
||||
*/
|
||||
public static void saveCommandHistoryString(List<String> history, File file) {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = new BufferedOutputStream(openOutputStream(file, false));
|
||||
for (String command: history) {
|
||||
out.write(command.getBytes("utf-8"));
|
||||
out.write('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
} finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> loadCommandHistoryString(File file) {
|
||||
BufferedReader br = null;
|
||||
List<String> history = new ArrayList<String>();
|
||||
try {
|
||||
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
history.add(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
} finally {
|
||||
try {
|
||||
if (br != null) {
|
||||
br.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
public static String readFileToString(File file, Charset encoding) throws IOException {
|
||||
FileInputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user