Skip to content

Commit 4b9aa54

Browse files
juarezjaramillopivovarit
authored andcommitted
BAEL-431 A CLI with Spring Shell (eugenp#1562)
* A Simple CLI with Spring Shell Code for article * Correct file name
1 parent 0b17547 commit 4b9aa54

9 files changed

Lines changed: 308 additions & 0 deletions

File tree

‎spring-all/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<artifactId>spring-retry</artifactId>
4444
<version>${springretry.version}</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.shell</groupId>
48+
<artifactId>spring-shell</artifactId>
49+
<version>${org.springframework.shell.version}</version>
50+
</dependency>
51+
4652

4753
<!-- aspectj -->
4854

@@ -280,6 +286,7 @@
280286
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
281287
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
282288
<springretry.version>1.1.5.RELEASE</springretry.version>
289+
<org.springframework.shell.version>1.2.0.RELEASE</org.springframework.shell.version>
283290

284291
<!-- persistence -->
285292
<hibernate.version>5.2.5.Final</hibernate.version>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.shell;
2+
3+
import java.io.IOException;
4+
import org.springframework.shell.Bootstrap;
5+
6+
public class Main {
7+
public static void main(String[] args) throws IOException {
8+
Bootstrap.main(args);
9+
}
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.baeldung.shell.simple;
2+
3+
import org.springframework.core.Ordered;
4+
import org.springframework.core.annotation.Order;
5+
import org.springframework.shell.plugin.support.DefaultBannerProvider;
6+
import org.springframework.shell.support.util.OsUtils;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Order(Ordered.HIGHEST_PRECEDENCE)
11+
public class SimpleBannerProvider extends DefaultBannerProvider {
12+
13+
public String getBanner() {
14+
StringBuffer buf = new StringBuffer();
15+
buf.append("=======================================").append(OsUtils.LINE_SEPARATOR);
16+
buf.append("* Baeldung Shell *").append(OsUtils.LINE_SEPARATOR);
17+
buf.append("=======================================").append(OsUtils.LINE_SEPARATOR);
18+
buf.append("Version:").append(this.getVersion());
19+
return buf.toString();
20+
}
21+
22+
public String getVersion() {
23+
return "1.0.1";
24+
}
25+
26+
public String getWelcomeMessage() {
27+
return "Welcome to Baeldung CLI";
28+
}
29+
30+
@Override
31+
public String getProviderName() {
32+
return "Baeldung Banner";
33+
}
34+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.baeldung.shell.simple;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.io.PrintWriter;
8+
import java.net.URL;
9+
import java.util.logging.Level;
10+
import java.util.logging.Logger;
11+
import org.springframework.shell.Bootstrap;
12+
import org.springframework.shell.core.CommandMarker;
13+
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
14+
import org.springframework.shell.core.annotation.CliCommand;
15+
import org.springframework.shell.core.annotation.CliOption;
16+
import org.springframework.stereotype.Component;
17+
18+
@Component
19+
public class SimpleCLI implements CommandMarker {
20+
21+
private String getContentsOfUrlAsString(URL url) {
22+
StringBuilder sb = new StringBuilder();
23+
try {
24+
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
25+
String inputLine;
26+
while ((inputLine = in.readLine()) != null) {
27+
sb.append(inputLine);
28+
}
29+
}
30+
} catch (IOException ex) {
31+
sb.append("ERROR");
32+
}
33+
return sb.toString();
34+
}
35+
36+
@CliCommand(
37+
value = {"web-get", "wg"},
38+
help = "Displays the contents of a URL."
39+
)
40+
public String webGet(
41+
@CliOption(
42+
key = {"", "url"},
43+
help = "URL whose contents will be displayed."
44+
) URL url) {
45+
return getContentsOfUrlAsString(url);
46+
}
47+
48+
@CliCommand(
49+
value = {"web-save", "ws"},
50+
help = "Saves the contents of a URL.")
51+
public String webSave(
52+
@CliOption(key = {"", "url"}, help = "URL whose contents will be saved.") URL url,
53+
@CliOption(key = {"out", "file"}, mandatory = true, help = "The name of the file.") String file) {
54+
String contents = getContentsOfUrlAsString(url);
55+
try (PrintWriter out = new PrintWriter(file)) {
56+
out.write(contents);
57+
} catch (FileNotFoundException ex) {
58+
//Ignore
59+
}
60+
return "Done.";
61+
}
62+
63+
private boolean adminEnableExecuted = false;
64+
65+
@CliAvailabilityIndicator(value = {"web-save"})
66+
public boolean isAdminEnabled() {
67+
return adminEnableExecuted;
68+
}
69+
70+
@CliCommand(value = "admin-enable")
71+
public String adminEnable() {
72+
adminEnableExecuted = true;
73+
return "Admin commands enabled.";
74+
}
75+
76+
@CliCommand(value = "admin-disable")
77+
public String adminDisable() {
78+
adminEnableExecuted = false;
79+
return "Admin commands disabled.";
80+
}
81+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.shell.simple;
2+
3+
import org.springframework.core.Ordered;
4+
import org.springframework.core.annotation.Order;
5+
import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@Order(Ordered.HIGHEST_PRECEDENCE)
10+
public class SimpleHistoryFileNameProvider extends DefaultHistoryFileNameProvider {
11+
12+
@Override
13+
public String getHistoryFileName() {
14+
return "baeldung-shell.log";
15+
}
16+
17+
@Override
18+
public String getProviderName() {
19+
return "Baeldung History";
20+
}
21+
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.shell.simple;
2+
3+
import org.springframework.core.Ordered;
4+
import org.springframework.core.annotation.Order;
5+
import org.springframework.shell.plugin.support.DefaultPromptProvider;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@Order(Ordered.HIGHEST_PRECEDENCE)
10+
public class SimplePromptProvider extends DefaultPromptProvider {
11+
12+
@Override
13+
public String getPrompt() {
14+
return "baeldung-shell>";
15+
}
16+
17+
@Override
18+
public String getProviderName() {
19+
return "Baeldung Prompt";
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.baeldung.shell.simple;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.util.List;
6+
import org.springframework.shell.core.Completion;
7+
import org.springframework.shell.core.Converter;
8+
import org.springframework.shell.core.MethodTarget;
9+
import org.springframework.stereotype.Component;
10+
11+
@Component
12+
public class SimpleURLConverter implements Converter<URL> {
13+
14+
@Override
15+
public URL convertFromText(String value, Class<?> requiredType, String optionContext) {
16+
try {
17+
return new URL(value);
18+
} catch (MalformedURLException ex) {
19+
//Ignore
20+
}
21+
return null;
22+
}
23+
24+
@Override
25+
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType,
26+
String existingData, String optionContext, MethodTarget target) {
27+
return false;
28+
}
29+
30+
@Override
31+
public boolean supports(Class<?> requiredType, String optionContext) {
32+
return URL.class.isAssignableFrom(requiredType);
33+
}
34+
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:util="http://www.springframework.org/schema/util"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
7+
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
8+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
9+
10+
<context:component-scan base-package="org.baeldung.shell.simple" />
11+
12+
</beans>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.baeldung.shell.simple;
2+
3+
import java.io.File;
4+
import org.junit.AfterClass;
5+
import org.junit.Assert;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import org.springframework.shell.Bootstrap;
9+
import org.springframework.shell.core.CommandResult;
10+
import org.springframework.shell.core.JLineShellComponent;
11+
12+
public class SimpleCLIUnitTest {
13+
14+
static JLineShellComponent shell;
15+
16+
@BeforeClass
17+
public static void startUp() throws InterruptedException {
18+
Bootstrap bootstrap = new Bootstrap();
19+
shell = bootstrap.getJLineShellComponent();
20+
}
21+
22+
@AfterClass
23+
public static void shutdown() {
24+
shell.stop();
25+
}
26+
27+
public static JLineShellComponent getShell() {
28+
return shell;
29+
}
30+
31+
@Test
32+
public void givenCommandConfig_whenExecutingWebGetCommand_thenCorrectResult() {
33+
34+
CommandResult resultWebSave = shell.executeCommand("web-get --url https://www.google.com");
35+
36+
Assert.assertTrue(resultWebSave.isSuccess());
37+
}
38+
39+
@Test
40+
public void givenCommandConfig_whenExecutingWebSaveCommand_thenCorrectResult() {
41+
42+
shell.executeCommand("admin-enable");
43+
CommandResult result = shell.executeCommand("web-save --url https://www.google.com --out contents.txt");
44+
45+
Assert.assertArrayEquals(
46+
new boolean[]{
47+
result.isSuccess(),
48+
new File("contents.txt").exists()},
49+
new boolean[]{true, true});
50+
}
51+
52+
@Test
53+
public void givenCommandConfig_whenAdminEnableCommandExecuted_thenCorrectAvailability() {
54+
55+
CommandResult resultAdminDisable = shell.executeCommand("admin-disable");
56+
CommandResult resultWebSaveUnavailable = shell.executeCommand("web-save --url https://www.google.com --out contents.txt");
57+
CommandResult resultAdminEnable = shell.executeCommand("admin-enable");
58+
CommandResult resultWebSaveAvailable = shell.executeCommand("web-save --url https://www.google.com --out contents.txt");
59+
60+
Assert.assertArrayEquals(
61+
new boolean[]{
62+
resultAdminDisable.isSuccess(),
63+
resultWebSaveUnavailable.isSuccess(),
64+
resultAdminEnable.isSuccess(),
65+
resultWebSaveAvailable.isSuccess()},
66+
new boolean[]{true, false, true, true});
67+
}
68+
69+
@Test
70+
public void givenCommandConfig_whenWebSaveCommandExecutedNoOutArgument_thenError() {
71+
72+
shell.executeCommand("admin-enable");
73+
CommandResult resultWebSave = shell.executeCommand("web-save --url https://www.google.com");
74+
75+
Assert.assertEquals(resultWebSave.isSuccess(), false);
76+
}
77+
78+
@Test
79+
public void givenCommandConfig_whenExecutingWebGetCommandWithDefaultArgument_thenCorrectResult() {
80+
81+
CommandResult result = shell.executeCommand("web-get https://www.google.com");
82+
83+
Assert.assertEquals(result.isSuccess(), true);
84+
}
85+
86+
}

0 commit comments

Comments
 (0)