Skip to content

Commit cedf6f3

Browse files
committed
Cleanup
Signed-off-by: Piotr Olaszewski <[email protected]>
1 parent a14657f commit cedf6f3

File tree

8 files changed

+146
-182
lines changed

8 files changed

+146
-182
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
3+
[*.{java,xml,gradle}]
4+
indent_style = tab
5+
indent_size = 4

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.util.Collections;
1919
import java.util.List;
20+
import java.util.function.Consumer;
21+
22+
import org.springframework.shell.core.commands.AbstractCommand;
2023

2124
/**
2225
* @author Eric Bottard
@@ -73,14 +76,70 @@ default List<String> getAliases() {
7376
*/
7477
ExitStatus execute(CommandContext commandContext) throws Exception;
7578

79+
/**
80+
* Creates and returns a new instance of a {@code Builder} for defining and
81+
* constructing commands.
82+
* <p>
83+
* The builder allows customization of command properties such as name, description,
84+
* group, help text, aliases, and execution logic.
85+
* @return a new {@code Builder} instance for configuring and creating commands
86+
*/
87+
static Builder builder() {
88+
return new DefaultCommandBuilder();
89+
}
90+
91+
/**
92+
* Builder for creating command.
93+
*/
7694
interface Builder {
77-
Builder name(String name);
78-
Builder description(String description);
79-
Builder help(String help);
80-
Builder group(String group);
81-
Builder options(List<CommandOption> options);
82-
Builder aliases(List<CommandAlias> aliases);
83-
Builder action(Command action);
84-
Command build();
85-
}
95+
96+
/**
97+
* Set the name of the command.
98+
* @return this builder
99+
*/
100+
Builder name(String name);
101+
102+
/**
103+
* Set the description of the command.
104+
* @return this builder
105+
*/
106+
Builder description(String description);
107+
108+
/**
109+
* Set the help of the command.
110+
* @return this builder
111+
*/
112+
Builder help(String help);
113+
114+
/**
115+
* Set the group of the command.
116+
* @return this builder
117+
*/
118+
Builder group(String group);
119+
120+
/**
121+
* Set the aliases of the command.
122+
* @return this builder
123+
*/
124+
Builder aliases(String... aliases);
125+
126+
/**
127+
* Set the aliases of the command.
128+
* @return this builder
129+
*/
130+
Builder aliases(List<String> aliases);
131+
132+
/**
133+
* Set command execution logic.
134+
* @return this builder
135+
*/
136+
Builder execute(Consumer<CommandContext> commandExecutor);
137+
138+
/**
139+
* Build the {@link AbstractCommand}.
140+
*/
141+
AbstractCommand build();
142+
143+
}
144+
86145
}

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

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 39 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
11
package org.springframework.shell.core.command;
22

3-
import org.jspecify.annotations.Nullable;
4-
import org.springframework.shell.core.command.Command.AliasSpec;
5-
import org.springframework.shell.core.command.Command.ExitCodeSpec;
6-
import org.springframework.shell.core.command.Command.TargetSpec;
7-
import org.springframework.shell.core.command.DefaultCommand.*;
8-
import org.springframework.shell.core.command.availability.Availability;
9-
import org.springframework.shell.core.command.support.CommandUtils;
10-
import org.springframework.shell.core.context.InteractionMode;
11-
import org.springframework.util.Assert;
12-
13-
import java.util.ArrayList;
3+
import java.util.Arrays;
144
import java.util.List;
155
import java.util.function.Consumer;
16-
import java.util.function.Function;
17-
import java.util.function.Supplier;
186

19-
import static org.springframework.shell.core.command.Command.Builder;
20-
import static org.springframework.shell.core.command.Command.OptionSpec;
7+
import org.jspecify.annotations.Nullable;
218

9+
import org.springframework.shell.core.commands.AbstractCommand;
10+
import org.springframework.shell.core.commands.adapter.ConsumerCommandAdapter;
11+
import org.springframework.util.Assert;
12+
import static org.springframework.shell.core.command.Command.*;
13+
14+
/**
15+
* Default implementation of {@link Builder}.
16+
*
17+
* @author Piotr Olaszewski
18+
*/
2219
class DefaultCommandBuilder implements Builder {
2320

24-
private @Nullable String command;
21+
private @Nullable String name;
2522

2623
private @Nullable String description;
2724

28-
private @Nullable String group;
29-
30-
private @Nullable InteractionMode interactionMode;
31-
32-
private @Nullable Supplier<Availability> availability;
33-
34-
private boolean hidden;
35-
36-
private @Nullable List<DefaultOptionSpec> optionSpecs;
37-
38-
private @Nullable DefaultHelpOptionsSpec helpOptionsSpec;
39-
40-
private @Nullable Function<String, String> defaultOptionNameModifier;
41-
42-
private @Nullable List<DefaultAliasSpec> aliasSpecs;
25+
private String group = "";
4326

44-
private @Nullable DefaultTargetSpec targetSpec;
27+
private String help = "";
4528

46-
private @Nullable DefaultExitCodeSpec exitCodeSpec;
29+
private @Nullable List<String> aliases;
4730

48-
private @Nullable DefaultErrorHandlingSpec errorHandling;
31+
private @Nullable Consumer<CommandContext> commandContextConsumer;
4932

5033
@Override
51-
public Builder command(String... commands) {
52-
this.command = CommandUtils.toCommand(commands);
34+
public Builder name(String name) {
35+
this.name = name;
5336
return this;
5437
}
5538

@@ -60,127 +43,52 @@ public Builder description(String description) {
6043
}
6144

6245
@Override
63-
public Builder group(String group) {
64-
this.group = group;
65-
return this;
66-
}
67-
68-
@Override
69-
public Builder interactionMode(@Nullable InteractionMode interactionMode) {
70-
this.interactionMode = interactionMode;
71-
return this;
72-
}
73-
74-
@Override
75-
public Builder isInteractive() {
76-
interactionMode = InteractionMode.INTERACTIVE;
77-
return this;
78-
}
79-
80-
@Override
81-
public Builder isNonInteractive() {
82-
interactionMode = InteractionMode.NONINTERACTIVE;
46+
public Builder help(String help) {
47+
this.help = help;
8348
return this;
8449
}
8550

8651
@Override
87-
public Builder availability(Supplier<Availability> availability) {
88-
this.availability = availability;
89-
return this;
90-
}
91-
92-
@Override
93-
public Builder hidden() {
94-
return hidden(true);
95-
}
96-
97-
@Override
98-
public Builder hidden(boolean hidden) {
99-
this.hidden = hidden;
52+
public Builder group(String group) {
53+
this.group = group;
10054
return this;
10155
}
10256

10357
@Override
104-
public Builder withOption(Consumer<OptionSpec> optionConfigurer) {
105-
DefaultOptionSpec optionSpec = new DefaultOptionSpec();
106-
optionConfigurer.accept(optionSpec);
107-
initOptionSpecs().add(optionSpec);
58+
public Builder aliases(String... aliases) {
59+
this.aliases = Arrays.asList(aliases);
10860
return this;
10961
}
11062

11163
@Override
112-
public Builder withHelpOptions(Consumer<Command.HelpOptionsSpec> helpOptionsConfigurer) {
113-
DefaultHelpOptionsSpec defaultHelpOptionsSpec = new DefaultHelpOptionsSpec();
114-
helpOptionsConfigurer.accept(defaultHelpOptionsSpec);
115-
this.helpOptionsSpec = defaultHelpOptionsSpec;
64+
public Builder aliases(List<String> aliases) {
65+
this.aliases = aliases;
11666
return this;
11767
}
11868

119-
private List<DefaultOptionSpec> initOptionSpecs() {
120-
if (optionSpecs == null) {
121-
optionSpecs = new ArrayList<>();
122-
}
123-
return optionSpecs;
124-
}
125-
12669
@Override
127-
public Builder defaultOptionNameModifier(Function<String, String> defaultOptionNameModifier) {
128-
this.defaultOptionNameModifier = defaultOptionNameModifier;
70+
public Builder execute(Consumer<CommandContext> commandExecutor) {
71+
this.commandContextConsumer = commandExecutor;
12972
return this;
13073
}
13174

13275
@Override
133-
public Builder withAlias(Consumer<AliasSpec> aliasConfigurer) {
134-
DefaultAliasSpec aliasSpec = new DefaultAliasSpec();
135-
aliasConfigurer.accept(aliasSpec);
136-
initAliasSpecs().add(aliasSpec);
137-
return this;
138-
}
76+
public AbstractCommand build() {
77+
ConsumerCommandAdapter abstractCommand = initCommand();
13978

140-
private List<DefaultAliasSpec> initAliasSpecs() {
141-
if (aliasSpecs == null) {
142-
aliasSpecs = new ArrayList<>();
79+
if (aliases != null) {
80+
abstractCommand.setAliases(aliases);
14381
}
144-
return aliasSpecs;
145-
}
14682

147-
@Override
148-
public Builder withTarget(Consumer<TargetSpec> targetConfigurer) {
149-
DefaultTargetSpec defaultTargetSpec = new DefaultTargetSpec();
150-
targetConfigurer.accept(defaultTargetSpec);
151-
this.targetSpec = defaultTargetSpec;
152-
return this;
83+
return abstractCommand;
15384
}
15485

155-
@Override
156-
public Builder withExitCode(Consumer<ExitCodeSpec> exitCodeConfigurer) {
157-
DefaultExitCodeSpec defaultExitCodeSpec = new DefaultExitCodeSpec();
158-
exitCodeConfigurer.accept(defaultExitCodeSpec);
159-
this.exitCodeSpec = defaultExitCodeSpec;
160-
return this;
161-
}
162-
163-
@Override
164-
public Builder withErrorHandling(Consumer<Command.ErrorHandlingSpec> errorHandlingConfigurer) {
165-
DefaultErrorHandlingSpec defaultErrorHandlingSpec = new DefaultErrorHandlingSpec();
166-
errorHandlingConfigurer.accept(defaultErrorHandlingSpec);
167-
this.errorHandling = defaultErrorHandlingSpec;
168-
return this;
169-
}
86+
private ConsumerCommandAdapter initCommand() {
87+
Assert.hasText(name, "'name' must be specified");
88+
Assert.hasText(description, "description");
89+
Assert.notNull(commandContextConsumer, "'commandExecutor' must not be null");
17090

171-
@Override
172-
public Command build() {
173-
Assert.hasText(command, "command cannot be empty");
174-
Assert.notNull(targetSpec, "target cannot be null");
175-
176-
InteractionMode interactionMode = this.interactionMode == null ? InteractionMode.ALL : this.interactionMode;
177-
Supplier<Availability> availability = this.availability == null ? Availability::available : this.availability;
178-
List<DefaultOptionSpec> defaultOptionSpecs = initOptionSpecs();
179-
List<DefaultAliasSpec> defaultAliasSpecs = initAliasSpecs();
180-
181-
return new DefaultCommand(command, interactionMode, group, hidden, description, availability,
182-
defaultOptionSpecs, targetSpec, defaultAliasSpecs, exitCodeSpec, errorHandling, helpOptionsSpec,
183-
defaultOptionNameModifier);
91+
return new ConsumerCommandAdapter(name, description, group, help, commandContextConsumer);
18492
}
18593

18694
}

spring-shell-core/src/main/java/org/springframework/shell/core/commands/AbstractCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ public ExitStatus execute(CommandContext commandContext) throws Exception {
9797
return doExecute(commandContext);
9898
}
9999

100+
public abstract ExitStatus doExecute(CommandContext commandContext) throws Exception;
101+
100102
private static boolean isHelp(CommandOption option) {
101103
return option.longName().equalsIgnoreCase("help") || option.shortName() == 'h';
102104
}
103105

104-
public abstract ExitStatus doExecute(CommandContext commandContext) throws Exception;
105-
106106
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package org.springframework.shell.core.commands.adapter;
3+
4+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)