|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.shell.docs; |
| 17 | + |
| 18 | +import java.util.Calendar; |
| 19 | + |
| 20 | +import org.springframework.context.annotation.Bean; |
| 21 | +import org.springframework.shell.Availability; |
| 22 | +import org.springframework.shell.AvailabilityProvider; |
| 23 | +import org.springframework.shell.command.CommandRegistration; |
| 24 | +import org.springframework.shell.command.annotation.Command; |
| 25 | +import org.springframework.shell.command.annotation.CommandAvailability; |
| 26 | +import org.springframework.shell.standard.ShellComponent; |
| 27 | +import org.springframework.shell.standard.ShellMethod; |
| 28 | +import org.springframework.shell.standard.ShellMethodAvailability; |
| 29 | + |
| 30 | +import static java.util.Calendar.DAY_OF_WEEK; |
| 31 | +import static java.util.Calendar.SUNDAY; |
| 32 | + |
| 33 | +class CommandAvailabilitySnippets { |
| 34 | + |
| 35 | + class Dump1 { |
| 36 | + // tag::availability-method-in-shellcomponent[] |
| 37 | + @ShellComponent |
| 38 | + public class MyCommands { |
| 39 | + |
| 40 | + private boolean connected; |
| 41 | + |
| 42 | + @ShellMethod("Connect to the server.") |
| 43 | + public void connect(String user, String password) { |
| 44 | + // do something |
| 45 | + connected = true; |
| 46 | + } |
| 47 | + |
| 48 | + @ShellMethod("Download the nuclear codes.") |
| 49 | + public void download() { |
| 50 | + // do something |
| 51 | + } |
| 52 | + |
| 53 | + public Availability downloadAvailability() { |
| 54 | + return connected |
| 55 | + ? Availability.available() |
| 56 | + : Availability.unavailable("you are not connected"); |
| 57 | + } |
| 58 | + } |
| 59 | + // end::availability-method-in-shellcomponent[] |
| 60 | + } |
| 61 | + |
| 62 | + class Dump2 { |
| 63 | + boolean connected; |
| 64 | + |
| 65 | + // tag::availability-method-name-in-shellcomponent[] |
| 66 | + @ShellMethod("Download the nuclear codes.") |
| 67 | + @ShellMethodAvailability("availabilityCheck") // <1> |
| 68 | + public void download() { |
| 69 | + } |
| 70 | + |
| 71 | + public Availability availabilityCheck() { // <1> |
| 72 | + return connected |
| 73 | + ? Availability.available() |
| 74 | + : Availability.unavailable("you are not connected"); |
| 75 | + } |
| 76 | + // end::availability-method-name-in-shellcomponent[] |
| 77 | + } |
| 78 | + |
| 79 | + class Dump3 { |
| 80 | + boolean connected; |
| 81 | + // tag::availability-method-name-multi-in-shellcomponent[] |
| 82 | + @ShellMethod("Download the nuclear codes.") |
| 83 | + public void download() { |
| 84 | + } |
| 85 | + |
| 86 | + @ShellMethod("Disconnect from the server.") |
| 87 | + public void disconnect() { |
| 88 | + } |
| 89 | + |
| 90 | + @ShellMethodAvailability({"download", "disconnect"}) |
| 91 | + public Availability availabilityCheck() { |
| 92 | + return connected |
| 93 | + ? Availability.available() |
| 94 | + : Availability.unavailable("you are not connected"); |
| 95 | + } |
| 96 | + |
| 97 | + // end::availability-method-name-multi-in-shellcomponent[] |
| 98 | + } |
| 99 | + |
| 100 | + // tag::availability-method-default-value-in-shellcomponent[] |
| 101 | + @ShellComponent |
| 102 | + public class Toggles { |
| 103 | + |
| 104 | + @ShellMethodAvailability |
| 105 | + public Availability availabilityOnWeekdays() { |
| 106 | + return Calendar.getInstance().get(DAY_OF_WEEK) == SUNDAY |
| 107 | + ? Availability.available() |
| 108 | + : Availability.unavailable("today is not Sunday"); |
| 109 | + } |
| 110 | + |
| 111 | + @ShellMethod |
| 112 | + public void foo() {} |
| 113 | + |
| 114 | + @ShellMethod |
| 115 | + public void bar() {} |
| 116 | + } |
| 117 | + // end::availability-method-default-value-in-shellcomponent[] |
| 118 | + |
| 119 | + class Dump4 { |
| 120 | + // tag::availability-method-annotation[] |
| 121 | + @Command |
| 122 | + class MyCommands { |
| 123 | + |
| 124 | + private boolean connected; |
| 125 | + |
| 126 | + @Command(command = "connect") |
| 127 | + public void connect(String user, String password) { |
| 128 | + connected = true; |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + @Command(command = "download") |
| 133 | + @CommandAvailability(provider = "downloadAvailability") |
| 134 | + public void download( |
| 135 | + ) { |
| 136 | + // do something |
| 137 | + } |
| 138 | + |
| 139 | + @Bean |
| 140 | + public AvailabilityProvider downloadAvailability() { |
| 141 | + return () -> connected |
| 142 | + ? Availability.available() |
| 143 | + : Availability.unavailable("you are not connected"); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // end::availability-method-annotation[] |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + class Dump5 { |
| 152 | + |
| 153 | + // tag::availability-method-programmatic[] |
| 154 | + private boolean connected; |
| 155 | + |
| 156 | + @Bean |
| 157 | + public CommandRegistration connect( |
| 158 | + CommandRegistration.BuilderSupplier builder) { |
| 159 | + return builder.get() |
| 160 | + .command("connect") |
| 161 | + .withOption() |
| 162 | + .longNames("connected") |
| 163 | + .required() |
| 164 | + .type(boolean.class) |
| 165 | + .and() |
| 166 | + .withTarget() |
| 167 | + .consumer(ctx -> { |
| 168 | + boolean connected = ctx.getOptionValue("connected"); |
| 169 | + this.connected = connected; |
| 170 | + }) |
| 171 | + .and() |
| 172 | + .build(); |
| 173 | + } |
| 174 | + |
| 175 | + @Bean |
| 176 | + public CommandRegistration download( |
| 177 | + CommandRegistration.BuilderSupplier builder) { |
| 178 | + return builder.get() |
| 179 | + .command("download") |
| 180 | + .availability(() -> { |
| 181 | + return connected |
| 182 | + ? Availability.available() |
| 183 | + : Availability.unavailable("you are not connected"); |
| 184 | + }) |
| 185 | + .withTarget() |
| 186 | + .consumer(ctx -> { |
| 187 | + // do something |
| 188 | + }) |
| 189 | + .and() |
| 190 | + .build(); |
| 191 | + } |
| 192 | + // end::availability-method-programmatic[] |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments