Skip to content

Commit

Permalink
Merge pull request #63 from fonimus/feature/exclude-builtin-commands
Browse files Browse the repository at this point in the history
Add properties to exclude not wanted built-in commands
  • Loading branch information
fonimus authored Aug 13, 2019
2 parents 09ed799 + 40cb6fc commit 29b1105
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 33 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

For more information please visit `spring shell` [website](https://projects.spring.io/spring-shell/)
or [2.0.1 reference documentation](https://docs.spring.io/spring-shell/docs/2.0.1.RELEASE/reference/htmlsingle/)
or [2.0.1 reference documentation](https://docs.spring.io/spring-shell/docs/2.0.1.RELEASE/reference/htmlsingle/).

* [Getting started](#getting-started)
* [Actuator commands](#actuator-commands)
Expand Down Expand Up @@ -73,7 +73,12 @@ ssh:
# for ssh helper 'confirm' method
confirmation-words:
- y
- yes
- yes
# set to false to disable following default built-in commands
default-commands:
jvm: true
postprocessors: true
thread: true
display-banner: true
# to use AnyOsFileValueProvider instead of spring shell FileValueProvider for all File option parameters
# if set to false, it still can be used via '@ShellOption(valueProvider = AnyOsFileValueProvider.class) File file'
Expand All @@ -86,7 +91,7 @@ ssh:
port: 2222
user: user
prompt:
# in enum: com.github.fonimus.ssh.shell.PromptColor
# in enum: com.github.fonimus.ssh.shell.PromptColor (black, red, green, yellow, blue, magenta, cyan, white, bright)
color: white
text: 'shell>'
```
Expand Down
3 changes: 3 additions & 0 deletions samples/complete/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ssh:
authentication: security
auth-provider-bean-name: customAuthManager
any-os-file-provider: false
default-commands:
jvm: false
threads: false

management:
endpoints:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.fonimus.ssh.shell;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import static com.github.fonimus.ssh.shell.SshShellProperties.SSH_SHELL_PREFIX;

/**
Expand Down Expand Up @@ -38,6 +39,8 @@ public class SshShellProperties {

private Prompt prompt = new Prompt();

private DefaultCommands defaultCommands = new DefaultCommands();

private Actuator actuator = new Actuator();

private boolean enable = true;
Expand Down Expand Up @@ -92,4 +95,17 @@ public static class Actuator {
private List<String> excludes = new ArrayList<>();
}

/**
* Default commands configuration
*/
@Data
public static class DefaultCommands {

private boolean jvm = true;

private boolean postprocessors = true;

private boolean threads = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@

import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellMethod;

import com.github.fonimus.ssh.shell.postprocess.PostProcessor;

import static com.github.fonimus.ssh.shell.SshShellProperties.SSH_SHELL_PREFIX;

/**
* Command to list available post processors
*/
@SshShellComponent
@ShellCommandGroup("Built-In Commands")
@ConditionalOnProperty(
value = {
SSH_SHELL_PREFIX + ".default-commands.postprocessors",
SSH_SHELL_PREFIX + ".defaultCommands.postprocessors"
}, havingValue = "true", matchIfMissing = true
)
public class Postprocessors {

private List<PostProcessor> postProcessors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.fonimus.ssh.shell.commands;

import com.github.fonimus.ssh.shell.postprocess.TypePostProcessorResultHandler;
import org.jline.terminal.Terminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.commands.Stacktrace;

import com.github.fonimus.ssh.shell.postprocess.TypePostProcessorResultHandler;

/**
* Override stacktrace command to get error per thread
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package com.github.fonimus.ssh.shell.commands;

import com.github.fonimus.ssh.shell.SshShellHelper;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.table.*;

import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.table.ArrayTableModel;
import org.springframework.shell.table.BorderStyle;
import org.springframework.shell.table.SimpleHorizontalAligner;
import org.springframework.shell.table.SimpleVerticalAligner;
import org.springframework.shell.table.SizeConstraints;
import org.springframework.shell.table.Table;
import org.springframework.shell.table.TableBuilder;
import org.springframework.shell.table.TableModel;

import com.github.fonimus.ssh.shell.SshShellHelper;

import static com.github.fonimus.ssh.shell.SshShellHelper.at;
import static com.github.fonimus.ssh.shell.SshShellProperties.SSH_SHELL_PREFIX;

/**
* System command
*/
@SshShellComponent
@ShellCommandGroup("Built-In Commands")
@ConditionalOnProperty(
value = {
SSH_SHELL_PREFIX + ".default-commands.jvm",
SSH_SHELL_PREFIX + ".defaultCommands.jvm"
}, havingValue = "true", matchIfMissing = true
)
public class SystemCommand {

public static final String SPLIT_REGEX = "[:;]";
Expand All @@ -39,7 +55,7 @@ private static SizeConstraints.Extent extent(String[] raw, String regex) {
return new SizeConstraints.Extent(min, max);
}

@ShellMethod(key = "jvm-env", value = "List system env.")
@ShellMethod(key = "jvm-env", value = "List system environment.")
public Object jvmEnv(boolean simpleView) {
if (simpleView) {
return buildSimple(System.getenv());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.github.fonimus.ssh.shell.commands;

import com.github.fonimus.ssh.shell.PromptColor;
import com.github.fonimus.ssh.shell.SshShellHelper;
import com.github.fonimus.ssh.shell.interactive.Interactive;
import com.github.fonimus.ssh.shell.interactive.KeyBinding;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
Expand All @@ -15,17 +20,27 @@
import org.springframework.shell.table.SimpleHorizontalAligner;
import org.springframework.shell.table.TableBuilder;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import com.github.fonimus.ssh.shell.PromptColor;
import com.github.fonimus.ssh.shell.SshShellHelper;
import com.github.fonimus.ssh.shell.interactive.Interactive;
import com.github.fonimus.ssh.shell.interactive.KeyBinding;

import static com.github.fonimus.ssh.shell.SshShellHelper.*;
import static com.github.fonimus.ssh.shell.SshShellHelper.INTERACTIVE_LONG_MESSAGE;
import static com.github.fonimus.ssh.shell.SshShellHelper.INTERACTIVE_SHORT_MESSAGE;
import static com.github.fonimus.ssh.shell.SshShellHelper.at;
import static com.github.fonimus.ssh.shell.SshShellProperties.SSH_SHELL_PREFIX;

/**
* Thread command
*/
@SshShellComponent
@ShellCommandGroup("Built-In Commands")
@ConditionalOnProperty(
value = {
SSH_SHELL_PREFIX + ".default-commands.threads",
SSH_SHELL_PREFIX + ".defaultCommands.threads"
}, havingValue = "true", matchIfMissing = true
)
public class ThreadCommand {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd:MM:yyyy HH:mm:ss");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.fonimus.ssh.shell.postprocess;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fonimus.ssh.shell.postprocess.provided.JsonPointerPostProcessor;
import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;

import java.util.Arrays;
import java.util.Collections;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fonimus.ssh.shell.postprocess.provided.JsonPointerPostProcessor;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -23,12 +24,12 @@ static void init() {

@Test
void process() throws Exception {
String test = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(
Health.down()
.withDetail("test", "value")
.withDetail("map", Collections.singletonMap("key", "map-value"))
.withDetail("list", Collections.singletonList("item"))
.build());
Health health = Health.down()
.withDetail("test", "value")
.withDetail("map", Collections.singletonMap("key", "map-value"))
.withDetail("list", Collections.singletonList("item"))
.build();
String test = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(health);

assertAll("json pointer",
() -> assertEquals("not-a-json", processor.process("not-a-json", Collections.singletonList("/test"))),
Expand All @@ -39,19 +40,27 @@ void process() throws Exception {
processor.process(test, Collections.singletonList("test"))),
() -> assertEquals("No node found with json path expression: /not-existing",
processor.process(test, Collections.singletonList("/not-existing"))),
() -> assertEquals("{\n \"test\" : \"value\",\n \"map\" : {\n \"key\" : \"map-value\"\n },\n \"list\" : [ \"item\" ]\n}",
() -> assertEqualsNoLineSeparator("{ \"test\" : \"value\", \"map\" : { \"key\" : \"map-value\" }, \"list\" : [ \"item\" ]}",
processor.process(test, Collections.singletonList("/details"))),
() -> assertEquals("value", processor.process(test, Collections.singletonList("/details/test"))),
() -> assertEquals("[ \"item\" ]", processor.process(test, Collections.singletonList("/details/list"))),
() -> assertEquals("item", processor.process(test, Collections.singletonList("/details/list/0"))),
() -> assertEquals("No node found with json path expression: /details/list/1",
processor.process(test, Collections.singletonList("/details/list/1"))),
() -> assertEquals("{\n \"key\" : \"map-value\"\n}", processor.process(test, Collections.singletonList("/details/map"))),
() -> assertEqualsNoLineSeparator("{ \"key\" : \"map-value\"}", processor.process(test, Collections.singletonList("/details/map"))),
() -> assertEquals("map-value", processor.process(test, Collections.singletonList("/details/map/key"))),
() -> assertEquals("map-value", processor.process(test, Arrays.asList("/details/map/key", "dont-care"))),
() -> assertEquals("No node found with json path expression: /details/map/not-a-key",
processor.process(test, Collections.singletonList("/details/map/not-a-key")))
);

}
}

private static void assertEqualsNoLineSeparator(String expected, String actual) {
assertEquals(clean(expected), clean(actual));
}

private static String clean(String toClean) {
return toClean == null ? null : toClean.replaceAll("[\\r\\n]+", "");
}
}

0 comments on commit 29b1105

Please sign in to comment.