Add dynamic command availability

Introduce availability concept on MethodTarget (with reason if not available)
Add bridge to @CliAvailabilityIndicator to Legacy registrar

Fixes #138

Add help for unavailable commands

Add standard API for availability
This commit is contained in:
Eric Bottard
2017-08-22 18:22:51 +02:00
parent 6c231a072c
commit 1eea04ad2f
16 changed files with 658 additions and 45 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.shell.samples.legacy;
import java.lang.reflect.Method;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -35,6 +36,13 @@ public class LegacyCommands implements CommandMarker {
public static final Method REGISTER_METHOD = ReflectionUtils.findMethod(LegacyCommands.class, "register", String.class, ArtifactType.class, String.class, boolean.class);
public static final Method SUM_METHOD = ReflectionUtils.findMethod(LegacyCommands.class, "sum", int.class, int.class);
private boolean available = true;
@CliAvailabilityIndicator("register module")
public boolean registerAvailable() {
return available;
}
@CliCommand(value = "register module", help = "Register a new module")
public String register(
@CliOption(mandatory = true,
@@ -57,10 +65,11 @@ public class LegacyCommands implements CommandMarker {
return String.format(("Successfully registered module '%s:%s'"), type, name);
}
@CliCommand(value = "sum", help = "adds two numbers")
@CliCommand(value = "sum", help = "adds two numbers. Will also toggle the 'register module' command availability")
public int sum(
@CliOption(key = "v1", unspecifiedDefaultValue = "38") int a,
@CliOption(key = "v2", specifiedDefaultValue = "42") int b) {
available = !available;
return a + b;
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.standard;
import org.springframework.shell.Availability;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellMethodAvailability;
/**
* Showcases dynamic command availability.
*
* @author Eric Bottard
*/
@ShellComponent
public class DynamicCommands {
private boolean connected;
private boolean authenticated;
public Availability authenticateAvailability() {
return connected ? Availability.available() : Availability.unavailable("you are not connected");
}
@ShellMethod("Authenticate with the system")
public void authenticate(String credentials) {
authenticated = "sesame".equals(credentials);
}
@ShellMethod("Connect to the system")
public void connect() {
connected = true;
}
@ShellMethod("Disconnect from the system")
public void disconnect() {
connected = false;
}
@ShellMethod("Blow Everything up")
@ShellMethodAvailability("dangerousAvailability")
public String blowUp() {
return "Boom!";
}
private Availability dangerousAvailability() {
return connected && authenticated ? Availability.available() : Availability.unavailable("you failed to authenticate. Try 'sesame'.");
}
}