BAEL-19967: Migrate spring-shell module to the com.baeldung package
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.shell;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.springframework.shell.Bootstrap;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Bootstrap.main(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.shell.simple;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.shell.plugin.support.DefaultBannerProvider;
|
||||
import org.springframework.shell.support.util.OsUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class SimpleBannerProvider extends DefaultBannerProvider {
|
||||
|
||||
public String getBanner() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("=======================================").append(OsUtils.LINE_SEPARATOR);
|
||||
buf.append("* Baeldung Shell *").append(OsUtils.LINE_SEPARATOR);
|
||||
buf.append("=======================================").append(OsUtils.LINE_SEPARATOR);
|
||||
buf.append("Version:").append(this.getVersion());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return "1.0.1";
|
||||
}
|
||||
|
||||
public String getWelcomeMessage() {
|
||||
return "Welcome to Baeldung CLI";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProviderName() {
|
||||
return "Baeldung Banner";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.shell.simple;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.springframework.shell.Bootstrap;
|
||||
import org.springframework.shell.core.CommandMarker;
|
||||
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
|
||||
import org.springframework.shell.core.annotation.CliCommand;
|
||||
import org.springframework.shell.core.annotation.CliOption;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleCLI implements CommandMarker {
|
||||
|
||||
private String getContentsOfUrlAsString(URL url) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
sb.append(inputLine);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
sb.append("ERROR");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@CliCommand(value = { "web-get", "wg" }, help = "Displays the contents of a URL.")
|
||||
public String webGet(@CliOption(key = { "", "url" }, help = "URL whose contents will be displayed.") URL url) {
|
||||
return getContentsOfUrlAsString(url);
|
||||
}
|
||||
|
||||
@CliCommand(value = { "web-save", "ws" }, help = "Saves the contents of a URL.")
|
||||
public String webSave(@CliOption(key = { "", "url" }, help = "URL whose contents will be saved.") URL url, @CliOption(key = { "out", "file" }, mandatory = true, help = "The name of the file.") String file) {
|
||||
String contents = getContentsOfUrlAsString(url);
|
||||
try (PrintWriter out = new PrintWriter(file)) {
|
||||
out.write(contents);
|
||||
} catch (FileNotFoundException ex) {
|
||||
// Ignore
|
||||
}
|
||||
return "Done.";
|
||||
}
|
||||
|
||||
private boolean adminEnableExecuted = false;
|
||||
|
||||
@CliAvailabilityIndicator(value = { "web-save" })
|
||||
public boolean isAdminEnabled() {
|
||||
return adminEnableExecuted;
|
||||
}
|
||||
|
||||
@CliCommand(value = "admin-enable")
|
||||
public String adminEnable() {
|
||||
adminEnableExecuted = true;
|
||||
return "Admin commands enabled.";
|
||||
}
|
||||
|
||||
@CliCommand(value = "admin-disable")
|
||||
public String adminDisable() {
|
||||
adminEnableExecuted = false;
|
||||
return "Admin commands disabled.";
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.shell.simple;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class SimpleHistoryFileNameProvider extends DefaultHistoryFileNameProvider {
|
||||
|
||||
@Override
|
||||
public String getHistoryFileName() {
|
||||
return "baeldung-shell.log";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProviderName() {
|
||||
return "Baeldung History";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.shell.simple;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.shell.plugin.support.DefaultPromptProvider;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class SimplePromptProvider extends DefaultPromptProvider {
|
||||
|
||||
@Override
|
||||
public String getPrompt() {
|
||||
return "baeldung-shell>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProviderName() {
|
||||
return "Baeldung Prompt";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.shell.simple;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import org.springframework.shell.core.Completion;
|
||||
import org.springframework.shell.core.Converter;
|
||||
import org.springframework.shell.core.MethodTarget;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleURLConverter implements Converter<URL> {
|
||||
|
||||
@Override
|
||||
public URL convertFromText(String value, Class<?> requiredType, String optionContext) {
|
||||
try {
|
||||
return new URL(value);
|
||||
} catch (MalformedURLException ex) {
|
||||
// Ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType, String existingData, String optionContext, MethodTarget target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> requiredType, String optionContext) {
|
||||
return URL.class.isAssignableFrom(requiredType);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user