Skip to content

Commit a8b774b

Browse files
committed
Fix script command
1 parent 643c635 commit a8b774b

File tree

6 files changed

+57
-14
lines changed

6 files changed

+57
-14
lines changed

spring-shell-core-autoconfigure/src/main/java/org/springframework/shell/core/autoconfigure/StandardCommandsAutoConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.shell.core.command.Clear;
2424
import org.springframework.shell.core.command.Command;
2525
import org.springframework.shell.core.command.Help;
26+
import org.springframework.shell.core.command.Script;
2627
import org.springframework.shell.core.command.Version;
2728

2829
/**
@@ -54,4 +55,10 @@ public Command versionCommand() {
5455
return new Version();
5556
}
5657

58+
@Bean
59+
@ConditionalOnProperty(value = "spring.shell.command.script.enabled", havingValue = "true", matchIfMissing = true)
60+
public Command scriptCommand() {
61+
return new Script();
62+
}
63+
5764
}

spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020

2121
import org.springframework.shell.core.FileInputProvider;
22-
import org.springframework.shell.core.utils.Utils;
22+
import org.springframework.shell.core.NonInteractiveShellRunner;
2323

2424
/**
2525
* A command that can read and execute other commands from a file.
@@ -28,16 +28,29 @@
2828
* @author Janne Valkealahti
2929
* @author Mahmoud Ben Hassine
3030
*/
31-
public class Script extends AbstractCommand {
31+
public class Script implements Command {
3232

33-
public Script(String name, String description) {
34-
super(name, description);
33+
private CommandParser commandParser = new DefaultCommandParser();
34+
35+
@Override
36+
public String getDescription() {
37+
return "Execute commands from a script file";
38+
}
39+
40+
@Override
41+
public String getGroup() {
42+
return "Built-In Commands";
3543
}
3644

3745
@Override
38-
public ExitStatus doExecute(CommandContext commandContext) throws Exception {
46+
public ExitStatus execute(CommandContext commandContext) throws Exception {
3947
List<CommandArgument> arguments = commandContext.parsedInput().arguments();
40-
File file = new File(arguments.get(0).value());
48+
if (arguments.size() != 1) {
49+
throw new IllegalArgumentException(
50+
"Script command expects exactly one argument: the absolute path to the script file to execute.");
51+
}
52+
String scriptFile = arguments.get(0).value();
53+
File file = new File(scriptFile);
4154
try (FileInputProvider inputProvider = new FileInputProvider(file)) {
4255
String input;
4356
while ((input = inputProvider.readInput()) != null) {
@@ -48,14 +61,18 @@ public ExitStatus doExecute(CommandContext commandContext) throws Exception {
4861
}
4962

5063
private void executeCommand(CommandContext commandContext, String input) throws Exception {
51-
Command command = commandContext.commandRegistry().getCommandByName(input);
52-
if (command == null) {
53-
String availableCommands = Utils.formatAvailableCommands(commandContext.commandRegistry());
54-
throw new CommandNotFoundException("No command found for name: " + input + ". " + availableCommands);
55-
}
56-
CommandContext singleCommandContext = new CommandContext(commandContext.parsedInput(),
57-
commandContext.commandRegistry(), commandContext.outputWriter());
58-
command.execute(singleCommandContext);
64+
String[] commandTokens = input.split(" ");
65+
NonInteractiveShellRunner shellRunner = new NonInteractiveShellRunner(this.commandParser,
66+
commandContext.commandRegistry());
67+
shellRunner.run(commandTokens);
68+
}
69+
70+
/**
71+
* Set the command parser to use to parse commands in the script.
72+
* @param commandParser the command parser to set
73+
*/
74+
public void setCommandParser(CommandParser commandParser) {
75+
this.commandParser = commandParser;
5976
}
6077

6178
}

spring-shell-core/src/main/java/org/springframework/shell/core/command/annotation/support/EnableCommandRegistrar.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR
4848
registry.registerBeanDefinition("help", new RootBeanDefinition(Help.class));
4949
registry.registerBeanDefinition("clear", new RootBeanDefinition(Clear.class));
5050
registry.registerBeanDefinition("version", new RootBeanDefinition(Version.class));
51+
registry.registerBeanDefinition("script", new RootBeanDefinition(Script.class));
5152

5253
// register user defined commands
5354
Class<?>[] candidateClasses = shellAnnotation.value();

spring-shell-docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*** xref:commands/builtin/clear.adoc[]
1919
*** xref:commands/builtin/exit.adoc[]
2020
*** xref:commands/builtin/version.adoc[]
21+
*** xref:commands/builtin/script.adoc[]
2122
** xref:commands/writing.adoc[]
2223
* xref:building.adoc[]
2324
* xref:components/index.adoc[]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[[built-in-commands-script]]
2+
= Script
3+
4+
The `script` command allows you to execute a series of commands from a specified script file. This is useful for automating repetitive tasks or setting up an environment quickly.
5+
6+
This command expects a file path as an argument, which points to the script file containing the commands to be executed. For example:
7+
8+
[source,shell]
9+
----
10+
$>script -- /path/to/your/script.txt
11+
----
12+
13+
Since the script command does not define any options, you need to use the `--` separator before specifying the script file path to avoid ambiguity. Please refer to the xref:commands/syntax.adoc[] section fpr more details about command syntax.
14+
15+
The script file should contain one command per line, and the commands will be executed in the order they appear in the file. Comments can be added to the script file by starting a line with a `#` character.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello --name=foo
2+
hello --name=bar

0 commit comments

Comments
 (0)