1
1
/*
2
- * Copyright 2023 the original author or authors.
2
+ * Copyright 2023-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import org .springframework .shell .command .CommandRegistration ;
22
22
import org .springframework .shell .command .annotation .Command ;
23
23
import org .springframework .shell .command .annotation .CommandAvailability ;
24
+ import org .springframework .shell .command .annotation .Option ;
24
25
import org .springframework .shell .standard .ShellComponent ;
25
26
import org .springframework .shell .standard .ShellMethod ;
26
27
import org .springframework .shell .standard .ShellMethodAvailability ;
28
+ import org .springframework .shell .standard .ShellOption ;
27
29
import org .springframework .stereotype .Component ;
28
30
29
31
public class AvailabilityCommands {
@@ -65,6 +67,28 @@ public String testAvailability3LegacyAnnotation(
65
67
public Availability testAvailability3LegacyAnnotationAvailability3 () {
66
68
return Availability .unavailable ("not available 3" );
67
69
}
70
+
71
+ private boolean connected = false ;
72
+
73
+ @ ShellMethod (key = LEGACY_ANNO + "availability-set" , group = GROUP )
74
+ public void testAvailabilitySetLegacyAnnotation (
75
+ @ ShellOption (value = "connected" ) boolean connected
76
+ ) {
77
+ this .connected = connected ;
78
+ }
79
+
80
+ @ ShellMethod (key = LEGACY_ANNO + "availability-use" , group = GROUP )
81
+ @ ShellMethodAvailability ("testAvailabilityLegacyAnnotationConnected" )
82
+ public void testAvailabilityUseLegacyAnnotation (
83
+ ) {
84
+ }
85
+
86
+ public Availability testAvailabilityLegacyAnnotationConnected () {
87
+ return connected
88
+ ? Availability .available ()
89
+ : Availability .unavailable ("you are not connected" );
90
+ }
91
+
68
92
}
69
93
70
94
@ Command (command = BaseE2ECommands .ANNO , group = BaseE2ECommands .GROUP )
@@ -81,6 +105,29 @@ public String testAvailability1Annotation(
81
105
public AvailabilityProvider testAvailability1AnnotationAvailability () {
82
106
return () -> Availability .unavailable ("not available" );
83
107
}
108
+
109
+ private boolean connected = false ;
110
+
111
+ @ Command (command = "availability-set" )
112
+ public void testAvailabilitySetAnnotation (
113
+ @ Option (longNames = "connected" ) boolean connected
114
+ ) {
115
+ this .connected = connected ;
116
+ }
117
+
118
+ @ Command (command = "availability-use" )
119
+ @ CommandAvailability (provider = "testAvailabilityAnnotationConnected" )
120
+ public void testAvailabilityUseAnnotation (
121
+ ) {
122
+ }
123
+
124
+ @ Bean
125
+ public AvailabilityProvider testAvailabilityAnnotationConnected () {
126
+ return () -> connected
127
+ ? Availability .available ()
128
+ : Availability .unavailable ("you are not connected" );
129
+ }
130
+
84
131
}
85
132
86
133
@ Component
@@ -119,5 +166,46 @@ public CommandRegistration testAvailability2Registration() {
119
166
AvailabilityProvider testAvailability2AnnotationAvailability () {
120
167
return () -> Availability .unavailable ("not available" );
121
168
}
169
+
170
+ private boolean connected = false ;
171
+
172
+ @ Bean
173
+ public CommandRegistration testAvailabilitySetRegistration () {
174
+ return getBuilder ()
175
+ .command (REG , "availability-set" )
176
+ .group (GROUP )
177
+ .withOption ()
178
+ .longNames ("connected" )
179
+ .required ()
180
+ .type (boolean .class )
181
+ .and ()
182
+ .withTarget ()
183
+ .consumer (ctx -> {
184
+ boolean connected = ctx .getOptionValue ("connected" );
185
+ this .connected = connected ;
186
+ })
187
+ .and ()
188
+ .build ();
189
+ }
190
+
191
+ @ Bean
192
+ public CommandRegistration testAvailabilityUseRegistration () {
193
+ return getBuilder ()
194
+ .command (REG , "availability-use" )
195
+ .group (GROUP )
196
+ .availability (testAvailabilityRegistrationConnected ())
197
+ .withTarget ()
198
+ .consumer (ctx -> {
199
+ })
200
+ .and ()
201
+ .build ();
202
+ }
203
+
204
+ public AvailabilityProvider testAvailabilityRegistrationConnected () {
205
+ return () -> connected
206
+ ? Availability .available ()
207
+ : Availability .unavailable ("you are not connected" );
208
+ }
209
+
122
210
}
123
211
}
0 commit comments