mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
fix grep command --trim-end support
This commit is contained in:
@@ -10,8 +10,6 @@ import com.taobao.middleware.cli.annotations.Name;
|
||||
import com.taobao.middleware.cli.annotations.Option;
|
||||
import com.taobao.middleware.cli.annotations.Summary;
|
||||
|
||||
import one.profiler.AsyncProfiler;
|
||||
|
||||
/**
|
||||
* @see com.taobao.arthas.core.shell.command.internal.GrepHandler
|
||||
*/
|
||||
@@ -93,9 +91,9 @@ public class GrepCommand extends AnnotatedCommand {
|
||||
this.showLineNumber = showLineNumber;
|
||||
}
|
||||
|
||||
@Option(longName = "trim-end", flag = true)
|
||||
@Option(longName = "trim-end", flag = false)
|
||||
@DefaultValue("true")
|
||||
@Description("Remove whitespaces at the end of the line")
|
||||
@Description("Remove whitespaces at the end of the line, default value true")
|
||||
public void setTrimEnd(boolean trimEnd) {
|
||||
this.trimEnd = trimEnd;
|
||||
}
|
||||
@@ -170,11 +168,7 @@ public class GrepCommand extends AnnotatedCommand {
|
||||
|
||||
@Override
|
||||
public void process(CommandProcess process) {
|
||||
process.write("The grep command only for pipes. See 'grep --help'").write("\n");
|
||||
|
||||
String libPath = "/private/tmp/async-profiler-1.6-macos-x64/build/libasyncProfiler.so";
|
||||
AsyncProfiler instance = AsyncProfiler.getInstance(libPath);
|
||||
process.write(instance.toString());
|
||||
process.write("The grep command only for pipes. See 'grep --help'\n");
|
||||
process.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,12 +46,16 @@ public class GrepHandler extends StdoutHandler {
|
||||
*/
|
||||
private final Integer maxCount;
|
||||
|
||||
private static CLI cli = null;
|
||||
|
||||
public static StdoutHandler inject(List<CliToken> tokens) {
|
||||
List<String> args = StdoutHandler.parseArgs(tokens, NAME);
|
||||
|
||||
GrepCommand grepCommand = new GrepCommand();
|
||||
CLI cli = CLIConfigurator.define(GrepCommand.class);
|
||||
CommandLine commandLine = cli.parse(args);
|
||||
if (cli == null) {
|
||||
cli = CLIConfigurator.define(GrepCommand.class);
|
||||
}
|
||||
CommandLine commandLine = cli.parse(args, true);
|
||||
|
||||
try {
|
||||
CLIConfigurator.inject(commandLine, grepCommand);
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.taobao.arthas.core.command.basic1000;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.taobao.middleware.cli.CLI;
|
||||
import com.taobao.middleware.cli.CommandLine;
|
||||
import com.taobao.middleware.cli.annotations.CLIConfigurator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author hengyunabc 2019-10-31
|
||||
*
|
||||
*/
|
||||
public class GrepCommandTest {
|
||||
|
||||
private static CLI cli = null;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
cli = CLIConfigurator.define(GrepCommand.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
List<String> args = Arrays.asList("-v", "ppp");
|
||||
GrepCommand grepCommand = new GrepCommand();
|
||||
CommandLine commandLine = cli.parse(args, true);
|
||||
|
||||
try {
|
||||
CLIConfigurator.inject(commandLine, grepCommand);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Assert.assertTrue(grepCommand.isInvertMatch());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
List<String> args = Arrays.asList("--before-context=6", "ppp");
|
||||
GrepCommand grepCommand = new GrepCommand();
|
||||
CommandLine commandLine = cli.parse(args, true);
|
||||
|
||||
try {
|
||||
CLIConfigurator.inject(commandLine, grepCommand);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Assert.assertEquals(6, grepCommand.getBeforeLines());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
List<String> args = Arrays.asList("--trim-end=false", "ppp");
|
||||
GrepCommand grepCommand = new GrepCommand();
|
||||
CommandLine commandLine = cli.parse(args, true);
|
||||
|
||||
try {
|
||||
CLIConfigurator.inject(commandLine, grepCommand);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Assert.assertFalse(grepCommand.isTrimEnd());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user