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 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.
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell;
|
||||
|
||||
/**
|
||||
@@ -23,20 +22,21 @@ package org.springframework.shell;
|
||||
*/
|
||||
public class CommandNotCurrentlyAvailable extends RuntimeException {
|
||||
|
||||
private final String command;
|
||||
private final Availability availability;
|
||||
private final String command;
|
||||
private final Availability availability;
|
||||
|
||||
public CommandNotCurrentlyAvailable(String command, Availability availability) {
|
||||
super(String.format("Command '%s' exists but is not currently available because %s", command, availability.getReason()));
|
||||
this.command = command;
|
||||
this.availability = availability;
|
||||
}
|
||||
public CommandNotCurrentlyAvailable(String command, Availability availability) {
|
||||
super(String.format("Command '%s' exists but is not currently available because %s", command,
|
||||
availability.getReason()));
|
||||
this.command = command;
|
||||
this.availability = availability;
|
||||
}
|
||||
|
||||
public String getCommand() {
|
||||
return command;
|
||||
}
|
||||
public String getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
public Availability getAvailability() {
|
||||
return availability;
|
||||
}
|
||||
public Availability getAvailability() {
|
||||
return availability;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.CommandNotCurrentlyAvailable;
|
||||
import org.springframework.shell.command.CommandParser.CommandParserException;
|
||||
import org.springframework.shell.command.CommandParser.CommandParserResults;
|
||||
import org.springframework.shell.command.CommandRegistration.TargetInfo;
|
||||
@@ -95,6 +97,11 @@ public interface CommandExecution {
|
||||
}
|
||||
|
||||
public Object evaluate(CommandRegistration registration, String[] args) {
|
||||
// fast fail with availability before doing anything else
|
||||
Availability availability = registration.getAvailability();
|
||||
if (availability != null && !availability.isAvailable()) {
|
||||
return new CommandNotCurrentlyAvailable(registration.getCommand(), availability);
|
||||
}
|
||||
|
||||
List<CommandOption> options = registration.getOptions();
|
||||
CommandParser parser = CommandParser.of(conversionService);
|
||||
|
||||
@@ -26,6 +26,8 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.CommandNotCurrentlyAvailable;
|
||||
import org.springframework.shell.command.CommandExecution.CommandParserExceptionsException;
|
||||
import org.springframework.shell.command.CommandRegistration.OptionArity;
|
||||
|
||||
@@ -459,4 +461,22 @@ public class CommandExecutionTests extends AbstractCommandTests {
|
||||
execution.evaluate(r1, new String[]{});
|
||||
}).isInstanceOf(CommandParserExceptionsException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandNotAvailable() {
|
||||
CommandRegistration r1 = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.description("help")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.availability(() -> Availability.unavailable("fake reason"))
|
||||
.withTarget()
|
||||
.function(function1)
|
||||
.and()
|
||||
.build();
|
||||
Object result = execution.evaluate(r1, new String[]{"--arg1", "myarg1value"});
|
||||
assertThat(result).isInstanceOf(CommandNotCurrentlyAvailable.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.shell.command;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.command.CommandRegistration.OptionArity;
|
||||
import org.springframework.shell.command.CommandRegistration.TargetInfo.TargetType;
|
||||
import org.springframework.shell.context.InteractionMode;
|
||||
@@ -410,4 +411,27 @@ public class CommandRegistrationTests extends AbstractCommandTests {
|
||||
assertThat(registration.getExitCode()).isNotNull();
|
||||
assertThat(registration.getExitCode().getMappingFunctions()).hasSize(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAvailability() {
|
||||
CommandRegistration registration;
|
||||
registration = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.withTarget()
|
||||
.function(function1)
|
||||
.and()
|
||||
.build();
|
||||
assertThat(registration.getAvailability()).isNotNull();
|
||||
assertThat(registration.getAvailability().isAvailable()).isTrue();
|
||||
|
||||
registration = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.availability(() -> Availability.unavailable("fake"))
|
||||
.withTarget()
|
||||
.function(function1)
|
||||
.and()
|
||||
.build();
|
||||
assertThat(registration.getAvailability()).isNotNull();
|
||||
assertThat(registration.getAvailability().isAvailable()).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user