mirror of
https://gitee.com/arthas/arthas.git
synced 2024-11-30 03:07:37 +08:00
transform command: cat
This commit is contained in:
parent
5498891252
commit
9bb63b4cd3
@ -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<String> 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)) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<CatModel> {
|
||||
|
||||
@Override
|
||||
public void draw(CommandProcess process, CatModel result) {
|
||||
process.write(result.getContent()).write("\n");
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user