mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
[bs-135] Simplify Command interface a bit more
This commit is contained in:
parent
fc1012e77d
commit
dcdf2d00b8
@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.bootstrap.cli;
|
package org.springframework.bootstrap.cli;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A single command that can be run from the CLI.
|
* A single command that can be run from the CLI.
|
||||||
@ -40,17 +38,16 @@ public interface Command {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns usage help for the command. This should be a simple one-line string
|
* Returns usage help for the command. This should be a simple one-line string
|
||||||
* describing basic usage. eg. '[options] <file>'. Do not include the name of
|
* describing basic usage. e.g. '[options] <file>'. Do not include the name of
|
||||||
* the command in this string.
|
* the command in this string.
|
||||||
*/
|
*/
|
||||||
String getUsageHelp();
|
String getUsageHelp();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints help for the command.
|
* Gets full help text for the command, e.g. a longer description and one line per
|
||||||
* @param out the output writer to display help
|
* option.
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
void printHelp(PrintStream out) throws IOException;
|
String getHelp();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the command.
|
* Run the command.
|
||||||
|
@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.bootstrap.cli;
|
package org.springframework.bootstrap.cli;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
@ -196,7 +194,9 @@ public class SpringBootstrapCli {
|
|||||||
+ " " + command.getUsageHelp());
|
+ " " + command.getUsageHelp());
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
command.printHelp(System.out);
|
if (command.getHelp() != null) {
|
||||||
|
System.out.println(command.getHelp());
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +219,8 @@ public class SpringBootstrapCli {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void printHelp(PrintStream out) throws IOException {
|
public String getHelp() {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.bootstrap.cli.command;
|
package org.springframework.bootstrap.cli.command;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
|
|
||||||
import org.springframework.bootstrap.cli.Command;
|
import org.springframework.bootstrap.cli.Command;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,7 +56,8 @@ public abstract class AbstractCommand implements Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void printHelp(PrintStream out) throws IOException {
|
public String getHelp() {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.bootstrap.cli.command;
|
package org.springframework.bootstrap.cli.command;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import joptsimple.OptionParser;
|
import joptsimple.OptionParser;
|
||||||
@ -56,8 +57,14 @@ public abstract class OptionHandler {
|
|||||||
|
|
||||||
protected abstract void run(OptionSet options) throws Exception;
|
protected abstract void run(OptionSet options) throws Exception;
|
||||||
|
|
||||||
public void printHelp(PrintStream out) throws IOException {
|
public String getHelp() {
|
||||||
|
OutputStream out = new ByteArrayOutputStream();
|
||||||
|
try {
|
||||||
getParser().printHelpOn(out);
|
getParser().printHelpOn(out);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return "Help not available";
|
||||||
|
}
|
||||||
|
return out.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.bootstrap.cli.command;
|
package org.springframework.bootstrap.cli.command;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
|
|
||||||
import org.springframework.bootstrap.cli.Command;
|
import org.springframework.bootstrap.cli.Command;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,8 +34,8 @@ public abstract class OptionParsingCommand extends AbstractCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void printHelp(PrintStream out) throws IOException {
|
public String getHelp() {
|
||||||
this.handler.printHelp(out);
|
return this.handler.getHelp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -19,7 +19,6 @@ import groovy.lang.Script;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import org.apache.ivy.util.FileUtil;
|
import org.apache.ivy.util.FileUtil;
|
||||||
@ -29,12 +28,12 @@ import org.springframework.bootstrap.cli.compiler.GroovyCompiler;
|
|||||||
import org.springframework.bootstrap.cli.compiler.GroovyCompilerConfiguration;
|
import org.springframework.bootstrap.cli.compiler.GroovyCompilerConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Command} to run a script.
|
* {@link Command} to run a Groovy script.
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ScriptCommand extends AbstractCommand {
|
public class ScriptCommand implements Command {
|
||||||
|
|
||||||
private static String[] DEFAULT_PATHS = new String[] { "${SPRING_HOME}/ext",
|
private static String[] DEFAULT_PATHS = new String[] { "${SPRING_HOME}/ext",
|
||||||
"${SPRING_HOME}/bin" };
|
"${SPRING_HOME}/bin" };
|
||||||
@ -45,20 +44,44 @@ public class ScriptCommand extends AbstractCommand {
|
|||||||
|
|
||||||
private Object main;
|
private Object main;
|
||||||
|
|
||||||
public ScriptCommand(String name, String description) {
|
private String name;
|
||||||
super(name, description);
|
|
||||||
|
public ScriptCommand(String script) {
|
||||||
|
this.name = script;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void printHelp(PrintStream out) throws IOException {
|
public String getName() {
|
||||||
if (getMain() instanceof OptionHandler) {
|
if (getMain() instanceof Command) {
|
||||||
((OptionHandler) getMain()).printHelp(out);
|
return ((Command) getMain()).getName();
|
||||||
}
|
}
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
if (getMain() instanceof Command) {
|
||||||
|
return ((Command) getMain()).getDescription();
|
||||||
|
}
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getHelp() {
|
||||||
|
if (getMain() instanceof OptionHandler) {
|
||||||
|
return ((OptionHandler) getMain()).getHelp();
|
||||||
|
}
|
||||||
|
if (getMain() instanceof Command) {
|
||||||
|
return ((Command) getMain()).getHelp();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(String... args) throws Exception {
|
public void run(String... args) throws Exception {
|
||||||
if (getMain() instanceof OptionHandler) {
|
if (getMain() instanceof Command) {
|
||||||
|
((Command) getMain()).run(args);
|
||||||
|
} else if (getMain() instanceof OptionHandler) {
|
||||||
((OptionHandler) getMain()).run(args);
|
((OptionHandler) getMain()).run(args);
|
||||||
} else if (this.main instanceof Runnable) {
|
} else if (this.main instanceof Runnable) {
|
||||||
((Runnable) this.main).run();
|
((Runnable) this.main).run();
|
||||||
@ -80,6 +103,9 @@ public class ScriptCommand extends AbstractCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUsageHelp() {
|
public String getUsageHelp() {
|
||||||
|
if (getMain() instanceof Command) {
|
||||||
|
return ((Command) getMain()).getDescription();
|
||||||
|
}
|
||||||
return "[options] <args>";
|
return "[options] <args>";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +114,7 @@ public class ScriptCommand extends AbstractCommand {
|
|||||||
try {
|
try {
|
||||||
this.main = getMainClass().newInstance();
|
this.main = getMainClass().newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalStateException("Cannot create main class: " + getName(),
|
throw new IllegalStateException("Cannot create main class: " + this.name,
|
||||||
e);
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,7 +124,7 @@ public class ScriptCommand extends AbstractCommand {
|
|||||||
private void compile() {
|
private void compile() {
|
||||||
GroovyCompiler compiler = new GroovyCompiler(new ScriptConfiguration());
|
GroovyCompiler compiler = new GroovyCompiler(new ScriptConfiguration());
|
||||||
compiler.addCompilationCustomizers(new ScriptCompilationCustomizer());
|
compiler.addCompilationCustomizers(new ScriptCompilationCustomizer());
|
||||||
File source = locateSource(getName());
|
File source = locateSource(this.name);
|
||||||
Class<?>[] classes;
|
Class<?>[] classes;
|
||||||
try {
|
try {
|
||||||
classes = compiler.compile(source);
|
classes = compiler.compile(source);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.springframework.bootstrap.cli;
|
package org.springframework.bootstrap.cli;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -18,7 +17,6 @@ import static org.hamcrest.Matchers.equalTo;
|
|||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.BDDMockito.willThrow;
|
import static org.mockito.BDDMockito.willThrow;
|
||||||
import static org.mockito.Matchers.anyObject;
|
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,7 +139,7 @@ public class SpringBootstrapCliTests {
|
|||||||
@Test
|
@Test
|
||||||
public void help() throws Exception {
|
public void help() throws Exception {
|
||||||
this.cli.run("help", "command");
|
this.cli.run("help", "command");
|
||||||
verify(this.regularCommand).printHelp((PrintStream) anyObject());
|
verify(this.regularCommand).getHelp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -17,9 +17,6 @@ package org.springframework.bootstrap.cli.command;
|
|||||||
|
|
||||||
import groovy.lang.Script;
|
import groovy.lang.Script;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@ -33,21 +30,34 @@ public class ScriptCommandTests {
|
|||||||
|
|
||||||
public static boolean executed = false;
|
public static boolean executed = false;
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void testMissing() throws Exception {
|
||||||
|
ScriptCommand command = new ScriptCommand("missing");
|
||||||
|
command.run("World");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScript() throws Exception {
|
public void testScript() throws Exception {
|
||||||
ScriptCommand command = new ScriptCommand("script", "Run a test command");
|
ScriptCommand command = new ScriptCommand("script");
|
||||||
command.run("World");
|
command.run("World");
|
||||||
assertEquals("World",
|
assertEquals("World",
|
||||||
((String[]) ((Script) command.getMain()).getProperty("args"))[0]);
|
((String[]) ((Script) command.getMain()).getProperty("args"))[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCommand() throws Exception {
|
||||||
|
ScriptCommand command = new ScriptCommand("command");
|
||||||
|
assertEquals("My script command", command.getUsageHelp());
|
||||||
|
command.run("World");
|
||||||
|
assertTrue(executed);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptions() throws Exception {
|
public void testOptions() throws Exception {
|
||||||
ScriptCommand command = new ScriptCommand("test", "Run a test command");
|
ScriptCommand command = new ScriptCommand("test");
|
||||||
command.run("World", "--foo");
|
command.run("World", "--foo");
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
String out = ((OptionHandler) command.getMain()).getHelp();
|
||||||
((OptionHandler) command.getMain()).printHelp(new PrintStream(out));
|
assertTrue("Wrong output: " + out, out.contains("--foo"));
|
||||||
assertTrue("Wrong output: " + out, out.toString().contains("--foo"));
|
|
||||||
assertTrue(executed);
|
assertTrue(executed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user