|
| 1 | +[[dynamic-command-availability]] |
| 2 | +=== Dynamic Command Availability |
| 3 | + |
| 4 | +Registered commands do not always make sense, due to the internal state of the application. |
| 5 | +For example, there may be a `download` command, but it only works once the user has used `connect` on a remote |
| 6 | +server. Now, if the user tries to use the `download` command, the shell should gracefully explain that |
| 7 | +the command exist but that it is not available at the time. |
| 8 | +Spring Shell lets you do that, even letting you provide a short explanation of the reason for |
| 9 | +the command not being available. |
| 10 | + |
| 11 | +There are three possible ways for a command to indicate availability. |
| 12 | +They all leverage a no-arg method that returns an instance of `Availability`. |
| 13 | +Consider the following example: |
| 14 | + |
| 15 | +==== |
| 16 | +[source, java] |
| 17 | +---- |
| 18 | +@ShellComponent |
| 19 | +public class MyCommands { |
| 20 | +
|
| 21 | + private boolean connected; |
| 22 | +
|
| 23 | + @ShellMethod("Connect to the server.") |
| 24 | + public void connect(String user, String password) { |
| 25 | + [...] |
| 26 | + connected = true; |
| 27 | + } |
| 28 | +
|
| 29 | + @ShellMethod("Download the nuclear codes.") |
| 30 | + public void download() { |
| 31 | + [...] |
| 32 | + } |
| 33 | +
|
| 34 | + public Availability downloadAvailability() { |
| 35 | + return connected |
| 36 | + ? Availability.available() |
| 37 | + : Availability.unavailable("you are not connected"); |
| 38 | + } |
| 39 | +} |
| 40 | +---- |
| 41 | +==== |
| 42 | + |
| 43 | +The `connect` method is used to connect to the server (details omitted), altering the state |
| 44 | +of the command through the `connected` boolean when done. |
| 45 | +The `download` command as marked as unavailable until the user has connected, thanks to the presence |
| 46 | +of a method named exactly as the `download` command method with the `Availability` suffix in its name. |
| 47 | +The method returns an instance of `Availability`, constructed with one of the two factory methods. |
| 48 | +If the command is not available, an explanation has to be provided. |
| 49 | +Now, if the user tries to invoke the command while not being connected, here is what happens: |
| 50 | + |
| 51 | +==== |
| 52 | +[source] |
| 53 | +---- |
| 54 | +shell:>download |
| 55 | +Command 'download' exists but is not currently available because you are not connected. |
| 56 | +Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace. |
| 57 | +---- |
| 58 | +==== |
| 59 | + |
| 60 | +Information about currently unavailable commands is also used in the integrated help. See <<help-command>>. |
| 61 | + |
| 62 | +[TIP] |
| 63 | +==== |
| 64 | +The reason provided when the command is not available should read nicely if appended after "`Because`". |
| 65 | +
|
| 66 | +You should not start the sentence with a capital or add a final period |
| 67 | +==== |
| 68 | + |
| 69 | +If naming the availability method after the name of the command method does not suit you, you |
| 70 | +can provide an explicit name by using the `@ShellMethodAvailability` annotation: |
| 71 | + |
| 72 | +==== |
| 73 | +[source, java] |
| 74 | +---- |
| 75 | + @ShellMethod("Download the nuclear codes.") |
| 76 | + @ShellMethodAvailability("availabilityCheck") // <1> |
| 77 | + public void download() { |
| 78 | + [...] |
| 79 | + } |
| 80 | +
|
| 81 | + public Availability availabilityCheck() { // <1> |
| 82 | + return connected |
| 83 | + ? Availability.available() |
| 84 | + : Availability.unavailable("you are not connected"); |
| 85 | + } |
| 86 | +---- |
| 87 | +<1> the names have to match |
| 88 | +==== |
| 89 | + |
| 90 | +Lastly, it is often the case that several commands in the same class share the same internal state and, thus, |
| 91 | +should all be available or unavailable as a group. Instead of having to stick the `@ShellMethodAvailability` |
| 92 | +on all command methods, Spring Shell lets you flip things around and put the `@ShellMethodAvailabilty` |
| 93 | +annotation on the availability method, specifying the names of the commands that it controls: |
| 94 | + |
| 95 | +==== |
| 96 | +[source, java] |
| 97 | +---- |
| 98 | + @ShellMethod("Download the nuclear codes.") |
| 99 | + public void download() { |
| 100 | + [...] |
| 101 | + } |
| 102 | +
|
| 103 | + @ShellMethod("Disconnect from the server.") |
| 104 | + public void disconnect() { |
| 105 | + [...] |
| 106 | + } |
| 107 | +
|
| 108 | + @ShellMethodAvailability({"download", "disconnect"}) |
| 109 | + public Availability availabilityCheck() { |
| 110 | + return connected |
| 111 | + ? Availability.available() |
| 112 | + : Availability.unavailable("you are not connected"); |
| 113 | + } |
| 114 | +---- |
| 115 | +==== |
| 116 | + |
| 117 | +[TIP] |
| 118 | +===== |
| 119 | +The default value for the `@ShellMethodAvailability.value()` attribute is `*`. This special |
| 120 | +wildcard matches all command names. This makes it easy to turn all commands of a single class on or off |
| 121 | +with a single availability method: |
| 122 | +
|
| 123 | +==== |
| 124 | +[source,java] |
| 125 | +---- |
| 126 | +@ShellComponent |
| 127 | +public class Toggles { |
| 128 | + @ShellMethodAvailability |
| 129 | + public Availability availabilityOnWeekdays() { |
| 130 | + return Calendar.getInstance().get(DAY_OF_WEEK) == SUNDAY |
| 131 | + ? Availability.available() |
| 132 | + : Availability.unavailable("today is not Sunday"); |
| 133 | + } |
| 134 | +
|
| 135 | + @ShellMethod |
| 136 | + public void foo() {} |
| 137 | +
|
| 138 | + @ShellMethod |
| 139 | + public void bar() {} |
| 140 | +} |
| 141 | +---- |
| 142 | +==== |
| 143 | +===== |
| 144 | + |
| 145 | +TIP: Spring Shell does not impose many constraints on how to write commands and how to organize classes. |
| 146 | +However, it is often good practice to put related commands in the same class, and the availability indicators |
| 147 | +can benefit from that. |
0 commit comments