support run OS commands
This commit is contained in:
@@ -1191,8 +1191,8 @@ public class SimpleParser implements Parser {
|
||||
}
|
||||
|
||||
LOGGER.info(sb.toString());
|
||||
LOGGER.warning("** Type 'hint' (without the quotes) and hit ENTER for step-by-step guidance **"
|
||||
+ StringUtils.LINE_SEPARATOR);
|
||||
// LOGGER.warning("** Type 'hint' (without the quotes) and hit ENTER for step-by-step guidance **"
|
||||
// + StringUtils.LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.springframework.shell.commands;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.springframework.roo.shell.CliCommand;
|
||||
import org.springframework.roo.shell.CliOption;
|
||||
import org.springframework.roo.shell.CommandMarker;
|
||||
import org.springframework.roo.support.logging.HandlerUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Command type to allow execution of native OS commands from the Spring Roo
|
||||
* shell.
|
||||
*
|
||||
* @author Stefan Schmidt
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Component
|
||||
public class OsCommands implements CommandMarker {
|
||||
|
||||
private static final Logger LOGGER = HandlerUtils
|
||||
.getLogger(OsCommands.class);
|
||||
|
||||
private OsOperations osOperations = new OsOperationsImpl();
|
||||
|
||||
@CliCommand(value = "!", help = "Allows execution of operating system (OS) commands.")
|
||||
public void command(
|
||||
@CliOption(key = { "", "command" }, mandatory = false, specifiedDefaultValue = "", unspecifiedDefaultValue = "", help = "The command to execute") final String command) {
|
||||
|
||||
System.out.println("command is:" + command);
|
||||
if (command != null && command.length() > 0) {
|
||||
try {
|
||||
osOperations.executeCommand(command);
|
||||
}
|
||||
catch (final IOException e) {
|
||||
LOGGER.severe("Unable to execute command " + command + " ["
|
||||
+ e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.springframework.shell.commands;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Operations type to allow execution of native OS commands from the Spring Roo
|
||||
* shell.
|
||||
*
|
||||
* @author Stefan Schmidt
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface OsOperations {
|
||||
|
||||
/**
|
||||
* Attempts the execution of a commands and delegates the output to the
|
||||
* standard logger.
|
||||
*
|
||||
* @param command the command to execute
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
void executeCommand(String command) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.springframework.shell.commands;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.roo.support.logging.HandlerUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Implementation of {@link OsOperations} interface.
|
||||
*
|
||||
* @author Stefan Schmidt
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Component
|
||||
public class OsOperationsImpl implements OsOperations {
|
||||
private static final Logger LOGGER = HandlerUtils.getLogger(OsOperationsImpl.class);
|
||||
|
||||
public void executeCommand(final String command) throws IOException {
|
||||
final File root = new File(".");
|
||||
final Process p = Runtime.getRuntime().exec(command, null, root);
|
||||
Reader input = new InputStreamReader(p.getInputStream());
|
||||
Reader errors = new InputStreamReader(p.getErrorStream());
|
||||
|
||||
for (String line : IOUtils.readLines(input)) {
|
||||
if (line.startsWith("[ERROR]")) {
|
||||
LOGGER.severe(line);
|
||||
}
|
||||
else if (line.startsWith("[WARNING]")) {
|
||||
LOGGER.warning(line);
|
||||
}
|
||||
else {
|
||||
LOGGER.info(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (String line : IOUtils.readLines(errors)) {
|
||||
if (line.startsWith("[ERROR]")) {
|
||||
LOGGER.severe(line);
|
||||
}
|
||||
else if (line.startsWith("[WARNING]")) {
|
||||
LOGGER.warning(line);
|
||||
}
|
||||
else {
|
||||
LOGGER.info(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
p.getOutputStream().close();
|
||||
|
||||
|
||||
try {
|
||||
if (p.waitFor() != 0) {
|
||||
LOGGER.warning("The command '" + command + "' did not complete successfully");
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user