Fix availability feature
- Bring back some missing functionality which got missing during the rework to new command model. - Polish some classes. - Restore origin sample. - Add availability things into help templates and its representation model. - Fixes #423
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2021 the original author or authors.
|
||||
* Copyright 2017-2022 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.
|
||||
@@ -29,29 +29,37 @@ import org.springframework.shell.standard.ShellMethodAvailability;
|
||||
@ShellComponent
|
||||
public class DynamicCommands {
|
||||
|
||||
private boolean connected;
|
||||
private boolean connected;
|
||||
|
||||
public Availability authenticateAvailability() {
|
||||
return connected ? Availability.available() : Availability.unavailable("you are not connected");
|
||||
}
|
||||
private boolean authenticated;
|
||||
|
||||
@ShellMethod(value = "Authenticate with the system", group = "Dynamic Commands")
|
||||
public void authenticate(String credentials) {
|
||||
}
|
||||
public Availability authenticateAvailability() {
|
||||
return connected ? Availability.available() : Availability.unavailable("you are not connected");
|
||||
}
|
||||
|
||||
@ShellMethod(value = "Connect to the system", group = "Dynamic Commands")
|
||||
public void connect() {
|
||||
connected = true;
|
||||
}
|
||||
@ShellMethod(value = "Authenticate with the system", group = "Dynamic Commands")
|
||||
public void authenticate(String credentials) {
|
||||
authenticated = "sesame".equals(credentials);
|
||||
}
|
||||
|
||||
@ShellMethod(value = "Disconnect from the system", group = "Dynamic Commands")
|
||||
public void disconnect() {
|
||||
connected = false;
|
||||
}
|
||||
@ShellMethod(value = "Connect to the system", group = "Dynamic Commands")
|
||||
public void connect() {
|
||||
connected = true;
|
||||
}
|
||||
|
||||
@ShellMethod(value = "Blow Everything up", group = "Dynamic Commands")
|
||||
@ShellMethodAvailability("dangerousAvailability")
|
||||
public String blowUp() {
|
||||
return "Boom!";
|
||||
}
|
||||
@ShellMethod(value = "Disconnect from the system", group = "Dynamic Commands")
|
||||
public void disconnect() {
|
||||
connected = false;
|
||||
}
|
||||
|
||||
@ShellMethod(value = "Blow Everything up", group = "Dynamic Commands")
|
||||
@ShellMethodAvailability("dangerousAvailability")
|
||||
public String blowUp() {
|
||||
return "Boom!";
|
||||
}
|
||||
|
||||
public Availability dangerousAvailability() {
|
||||
return connected && authenticated ? Availability.available()
|
||||
: Availability.unavailable("you failed to authenticate. Try 'sesame'.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user