mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
Merge branch 'master' of github.com:alibaba/arthas
This commit is contained in:
@@ -13,6 +13,7 @@ import com.taobao.arthas.core.command.basic1000.ShutdownCommand;
|
||||
import com.taobao.arthas.core.command.basic1000.StopCommand;
|
||||
import com.taobao.arthas.core.command.basic1000.SystemEnvCommand;
|
||||
import com.taobao.arthas.core.command.basic1000.SystemPropertyCommand;
|
||||
import com.taobao.arthas.core.command.basic1000.TeeCommand;
|
||||
import com.taobao.arthas.core.command.basic1000.VMOptionCommand;
|
||||
import com.taobao.arthas.core.command.basic1000.VersionCommand;
|
||||
import com.taobao.arthas.core.command.hidden.JulyCommand;
|
||||
@@ -102,6 +103,7 @@ public class BuiltinCommandPack implements CommandResolver {
|
||||
commands.add(Command.create(PwdCommand.class));
|
||||
commands.add(Command.create(MBeanCommand.class));
|
||||
commands.add(Command.create(GrepCommand.class));
|
||||
commands.add(Command.create(TeeCommand.class));
|
||||
commands.add(Command.create(ProfilerCommand.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.taobao.arthas.core.command.basic1000;
|
||||
|
||||
|
||||
import com.taobao.arthas.core.command.Constants;
|
||||
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
|
||||
import com.taobao.arthas.core.shell.command.CommandProcess;
|
||||
import com.taobao.middleware.cli.annotations.*;
|
||||
|
||||
/**
|
||||
* @author min.yang
|
||||
*/
|
||||
@Name("tee")
|
||||
@Summary("tee command for pipes." )
|
||||
@Description(Constants.EXAMPLE +
|
||||
" sysprop | tee /path/to/logfile | grep java \n" +
|
||||
" sysprop | tee -a /path/to/logfile | grep java \n"
|
||||
+ Constants.WIKI + Constants.WIKI_HOME + "tee")
|
||||
public class TeeCommand extends AnnotatedCommand {
|
||||
|
||||
private String filePath;
|
||||
private boolean append;
|
||||
|
||||
@Argument(index = 0, argName = "file", required = false)
|
||||
@Description("File path")
|
||||
public void setFilePath(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
@Option(shortName = "a", longName = "append", flag = true)
|
||||
@Description("Append to file")
|
||||
public void setRegEx(boolean append) {
|
||||
this.append = append;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(CommandProcess process) {
|
||||
process.write("The tee command only for pipes. See 'tee --help'\n");
|
||||
process.end();
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public boolean isAppend() {
|
||||
return append;
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,9 @@ public abstract class StdoutHandler implements Function<String, String> {
|
||||
return PlainTextHandler.inject(tokens);
|
||||
} else if (firstTextToken.value().equals(WordCountHandler.NAME)) {
|
||||
return WordCountHandler.inject(tokens);
|
||||
} else {
|
||||
} else if (firstTextToken.value().equals(TeeHandler.NAME)){
|
||||
return TeeHandler.inject(tokens);
|
||||
} else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.taobao.arthas.core.shell.command.internal;
|
||||
|
||||
import com.taobao.arthas.core.command.basic1000.TeeCommand;
|
||||
import com.taobao.arthas.core.shell.cli.CliToken;
|
||||
import com.taobao.arthas.core.util.StringUtils;
|
||||
import com.taobao.middleware.cli.CLI;
|
||||
import com.taobao.middleware.cli.CommandLine;
|
||||
import com.taobao.middleware.cli.annotations.CLIConfigurator;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author min.yang
|
||||
*/
|
||||
public class TeeHandler extends StdoutHandler implements CloseFunction {
|
||||
public static final String NAME = "tee";
|
||||
private PrintWriter out;
|
||||
private static CLI cli = null;
|
||||
|
||||
public TeeHandler(String filePath, boolean append) throws IOException {
|
||||
if (StringUtils.isEmpty(filePath)) {
|
||||
return;
|
||||
}
|
||||
File file = new File(filePath);
|
||||
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException(filePath + ": Is a directory");
|
||||
}
|
||||
|
||||
if (!file.exists()) {
|
||||
File parentFile = file.getParentFile();
|
||||
if (parentFile != null) {
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
}
|
||||
out = new PrintWriter(new BufferedWriter(new FileWriter(file, append)));
|
||||
}
|
||||
|
||||
public static StdoutHandler inject(List<CliToken> tokens) {
|
||||
List<String> args = StdoutHandler.parseArgs(tokens, NAME);
|
||||
|
||||
TeeCommand teeCommand = new TeeCommand();
|
||||
if (cli == null) {
|
||||
cli = CLIConfigurator.define(TeeCommand.class);
|
||||
}
|
||||
CommandLine commandLine = cli.parse(args, true);
|
||||
|
||||
try {
|
||||
CLIConfigurator.inject(commandLine, teeCommand);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String filePath = teeCommand.getFilePath();
|
||||
boolean append = teeCommand.isAppend();
|
||||
try {
|
||||
return new TeeHandler(filePath, append);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(String data) {
|
||||
data = super.apply(data);
|
||||
if (out != null) {
|
||||
out.write(data);
|
||||
out.flush();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user