11package 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 ;
144import java .util .List ;
155import 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+ */
2219class 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}
0 commit comments