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:
Janne Valkealahti
2022-06-14 09:56:17 +01:00
parent b2f96e679a
commit 39c01fe00b
10 changed files with 208 additions and 41 deletions

View File

@@ -0,0 +1,51 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://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.standard.commands;
/**
* Model encapsulating info about {@code command availability}.
*
* @author Janne Valkealahti
*/
class CommandAvailabilityInfoModel {
private boolean available;
private String reason;
CommandAvailabilityInfoModel(boolean available, String reason) {
this.available = available;
this.reason = reason;
}
/**
* Builds {@link CommandAvailabilityInfoModel}.
*
* @param available the available flag
* @param reason the reason
* @return a command parameter availability model
*/
static CommandAvailabilityInfoModel of(boolean available, String reason) {
return new CommandAvailabilityInfoModel(available, reason);
}
public boolean getAvailable() {
return available;
}
public String getReason() {
return reason;
}
}

View File

@@ -19,6 +19,7 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.shell.Availability;
import org.springframework.shell.command.CommandOption;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.util.ClassUtils;
@@ -34,11 +35,14 @@ class CommandInfoModel {
private String name;
private String description;
private List<CommandParameterInfoModel> parameters;
private CommandAvailabilityInfoModel availability;
CommandInfoModel(String name, String description, List<CommandParameterInfoModel> parameters) {
CommandInfoModel(String name, String description, List<CommandParameterInfoModel> parameters,
CommandAvailabilityInfoModel availability) {
this.name = name;
this.description = description;
this.parameters = parameters;
this.availability = availability;
}
/**
@@ -65,7 +69,15 @@ class CommandInfoModel {
.collect(Collectors.toList());
String description = registration.getDescription();
return new CommandInfoModel(name, description, parameters);
boolean available = true;
String availReason = "";
if (registration.getAvailability() != null) {
Availability a = registration.getAvailability();
available = a.isAvailable();
availReason = a.getReason();
}
CommandAvailabilityInfoModel availModel = CommandAvailabilityInfoModel.of(available, availReason);
return new CommandInfoModel(name, description, parameters, availModel);
}
private static String commandOptionType(CommandOption o) {
@@ -88,4 +100,8 @@ class CommandInfoModel {
public List<CommandParameterInfoModel> getParameters() {
return parameters;
}
public CommandAvailabilityInfoModel getAvailability() {
return availability;
}
}

View File

@@ -36,11 +36,14 @@ class GroupsInfoModel {
private boolean showGroups = true;
private final List<GroupCommandInfoModel> groups;
private final List<CommandInfoModel> commands;
private boolean hasUnavailableCommands = false;
GroupsInfoModel(boolean showGroups, List<GroupCommandInfoModel> groups, List<CommandInfoModel> commands) {
GroupsInfoModel(boolean showGroups, List<GroupCommandInfoModel> groups, List<CommandInfoModel> commands,
boolean hasUnavailableCommands) {
this.showGroups = showGroups;
this.groups = groups;
this.commands = commands;
this.hasUnavailableCommands = hasUnavailableCommands;
}
/**
@@ -71,7 +74,17 @@ class GroupsInfoModel {
List<CommandInfoModel> commands = gcims.stream()
.flatMap(gcim -> gcim.getCommands().stream())
.collect(Collectors.toList());
return new GroupsInfoModel(showGroups, gcims, commands);
boolean hasUnavailableCommands = commands.stream()
.map(c -> {
if (c.getAvailability() != null) {
return c.getAvailability().getAvailable();
}
return true;
})
.filter(a -> !a)
.findFirst()
.isPresent();
return new GroupsInfoModel(showGroups, gcims, commands, hasUnavailableCommands);
}
public boolean getShowGroups() {
@@ -85,4 +98,8 @@ class GroupsInfoModel {
public List<CommandInfoModel> getCommands() {
return commands;
}
public boolean getHasUnavailableCommands() {
return hasUnavailableCommands;
}
}

View File

@@ -57,6 +57,15 @@ options(options) ::= <<
<options:{ o | <option(o)>}>
>>
// AVAILABILITY
availability(availability) ::= <<
<if(!availability.available)>
<("CURRENTLY UNAVAILABLE"); format="style-highlight">
<availability.reason>
<endif>
>>
// main
main(model) ::= <<
<name(model.name, model.description)>
@@ -64,4 +73,5 @@ main(model) ::= <<
<synopsis(model.name, model.parameters)>
<options(model.parameters)>
<availability(model.availability)>
>>

View File

@@ -3,8 +3,21 @@ name() ::= <<
>>
availability(availability) ::= <%
<if(!availability.available)>
<("* "); format="style-highlight">
<endif>
%>
availabilityDesc(hasUnavailableCommands) ::= <<
<if(hasUnavailableCommands)>
<("Commands marked with (*) are currently unavailable.")>
<("Type `help <command>` to learn more.")>
<endif>
>>
command(command) ::= <<
<(command.name); format="style-highlight"><(":"); format="style-highlight"> <command.description>
<availability(command.availability)><(command.name); format="style-highlight"><(":"); format="style-highlight"> <command.description>
>>
@@ -29,4 +42,5 @@ main(model) ::= <<
<else>
<flat(model.commands)>
<endif>
<availabilityDesc(model.hasUnavailableCommands)>
>>