From 9bb63b4cd3f5be51fc8ec3ccf12522b8c919996c Mon Sep 17 00:00:00 2001 From: gongdewei Date: Mon, 8 Jun 2020 15:29:51 +0800 Subject: [PATCH] transform command: cat --- .../core/command/basic1000/CatCommand.java | 47 +++++++++++++----- .../arthas/core/command/model/CatModel.java | 49 +++++++++++++++++++ .../arthas/core/command/view/CatView.java | 17 +++++++ .../core/command/view/ResultViewResolver.java | 1 + 4 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 core/src/main/java/com/taobao/arthas/core/command/model/CatModel.java create mode 100644 core/src/main/java/com/taobao/arthas/core/command/view/CatView.java diff --git a/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java b/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java index d10efaf0c..9cb3c7fcb 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.nio.charset.Charset; import java.util.List; +import com.taobao.arthas.core.command.model.CatModel; import com.alibaba.arthas.deps.org.slf4j.Logger; import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; import com.taobao.arthas.core.shell.cli.Completion; @@ -24,6 +25,8 @@ public class CatCommand extends AnnotatedCommand { private static final Logger logger = LoggerFactory.getLogger(CatCommand.class); private List files; private String encoding; + private Integer sizeLimit = 128 * 1024; + private int maxSizeLimit = 8 * 1024 * 1024; @Argument(argName = "files", index = 0) @Description("files") @@ -37,37 +40,43 @@ public class CatCommand extends AnnotatedCommand { this.encoding = encoding; } + @Option(shortName = "M", longName = "sizeLimit") + @Description("Upper size limit in bytes for the result (128 * 1024 by default, the maximum value is 8 * 1024 * 1024)") + public void setSizeLimit(Integer sizeLimit) { + this.sizeLimit = sizeLimit; + } + @Override public void process(CommandProcess process) { + if (!verifyOptions(process)) { + return; + } + for (String file : files) { File f = new File(file); if (!f.exists()) { - process.write("cat " + file + ": No such file or directory\n"); - process.end(); + process.end(-1, "cat " + file + ": No such file or directory"); return; } if (f.isDirectory()) { - process.write("cat " + file + ": Is a directory\n"); - process.end(); + process.end(-1, "cat " + file + ": Is a directory"); return; } } for (String file : files) { File f = new File(file); - if (f.length() > 1024 * 1024 * 8) { - process.write("cat " + file + ": Is to large, size: " + f.length() + '\n'); - process.end(); + if (f.length() > sizeLimit) { + process.end(-1, "cat " + file + ": Is too large, size: " + f.length()); return; } try { String fileToString = FileUtils.readFileToString(f, - encoding == null ? Charset.defaultCharset() : Charset.forName(encoding)); - process.write(fileToString); + encoding == null ? Charset.defaultCharset() : Charset.forName(encoding)); + process.appendResult(new CatModel(file, fileToString)); } catch (IOException e) { logger.error("cat read file error. name: " + file, e); - process.write("cat read file error: " + e.getMessage() + '\n'); - process.end(1); + process.end(1, "cat read file error: " + e.getMessage()); return; } } @@ -75,6 +84,22 @@ public class CatCommand extends AnnotatedCommand { process.end(); } + private boolean verifyOptions(CommandProcess process) { + if (sizeLimit > maxSizeLimit) { + process.end(-1, "sizeLimit cannot be large than: " + maxSizeLimit); + return false; + } + + //目前不支持过滤,限制http请求执行的文件大小 + int maxSizeLimitOfHttp = 128 * 1024; + boolean isHttpApiRequest = !process.session().isTty(); + if (isHttpApiRequest && sizeLimit > maxSizeLimitOfHttp) { + process.end(-1, "When executing commands with http, sizeLimit cannot be large than: " + maxSizeLimitOfHttp); + return false; + } + return true; + } + @Override public void complete(Completion completion) { if (!CompletionUtils.completeFilePath(completion)) { diff --git a/core/src/main/java/com/taobao/arthas/core/command/model/CatModel.java b/core/src/main/java/com/taobao/arthas/core/command/model/CatModel.java new file mode 100644 index 000000000..aa4e3f2cc --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/command/model/CatModel.java @@ -0,0 +1,49 @@ +package com.taobao.arthas.core.command.model; + +/** + * Result model for CatCommand + * @author gongdewei 2020/5/11 + */ +public class CatModel extends ResultModel implements Countable { + + private String file; + private String content; + + public CatModel() { + } + + public CatModel(String file, String content) { + this.file = file; + this.content = content; + } + + @Override + public String getType() { + return "cat"; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public int size() { + if (content != null) { + //粗略计算行数作为item size + return content.length()/100 + 1; + } + return 0; + } +} diff --git a/core/src/main/java/com/taobao/arthas/core/command/view/CatView.java b/core/src/main/java/com/taobao/arthas/core/command/view/CatView.java new file mode 100644 index 000000000..4d21c4941 --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/command/view/CatView.java @@ -0,0 +1,17 @@ +package com.taobao.arthas.core.command.view; + +import com.taobao.arthas.core.command.model.CatModel; +import com.taobao.arthas.core.shell.command.CommandProcess; + +/** + * Result view for CatCommand + * @author gongdewei 2020/5/11 + */ +public class CatView extends ResultView { + + @Override + public void draw(CommandProcess process, CatModel result) { + process.write(result.getContent()).write("\n"); + } + +} diff --git a/core/src/main/java/com/taobao/arthas/core/command/view/ResultViewResolver.java b/core/src/main/java/com/taobao/arthas/core/command/view/ResultViewResolver.java index d2956fb86..6e46a00a9 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/view/ResultViewResolver.java +++ b/core/src/main/java/com/taobao/arthas/core/command/view/ResultViewResolver.java @@ -38,6 +38,7 @@ public class ResultViewResolver { registerView(new HelpModel(), new HelpView()); //registerView(new HistoryModel(), new HistoryView()); registerView(new EchoModel(), new EchoView()); + registerView(new CatModel(), new CatView()); } catch (Throwable e) { logger.error("register result view failed", e); }