forked from alibaba/arthas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:alibaba/arthas
- Loading branch information
Showing
6 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/taobao/arthas/core/command/basic1000/TeeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
core/src/main/java/com/taobao/arthas/core/shell/command/internal/TeeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
tee | ||
=== | ||
|
||
> Similar to the traditional `tee` command. | ||
|
||
``` | ||
USAGE: | ||
tee [-a] [-h] [file] | ||
SUMMARY: | ||
tee command for pipes. | ||
EXAMPLES: | ||
sysprop | tee /path/to/logfile | grep java | ||
sysprop | tee -a /path/to/logfile | grep java | ||
WIKI: | ||
https://alibaba.github.io/arthas/tee | ||
OPTIONS: | ||
-a, --append Append to file | ||
-h, --help this help | ||
<file> File path | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
tee | ||
=== | ||
|
||
> 类似传统的`tee`命令。 | ||
|
||
``` | ||
USAGE: | ||
tee [-a] [-h] [file] | ||
SUMMARY: | ||
tee command for pipes. | ||
EXAMPLES: | ||
sysprop | tee /path/to/logfile | grep java | ||
sysprop | tee -a /path/to/logfile | grep java | ||
WIKI: | ||
https://alibaba.github.io/arthas/tee | ||
OPTIONS: | ||
-a, --append Append to file | ||
-h, --help this help | ||
<file> File path | ||
``` |