Skip to content

Add dynamic command registration #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2022
Merged

Conversation

jvalkeal
Copy link
Contributor

  • Extend CommandRegistry for add/remove methods.
  • For rest of shell classes move to use registry
    directly instead of caching commands as registry
    is not immutable anymore.
  • Add new sample
  • Fixes Register commands dynamically #379

- Extend CommandRegistry for add/remove methods.
- For rest of shell classes move to use registry
  directly instead of caching commands as registry
  is not immutable anymore.
- Add new sample
- Fixes spring-projects#379
@jvalkeal jvalkeal merged commit 7b547e5 into spring-projects:main Feb 26, 2022
@@ -58,6 +58,16 @@ else if (mim == InteractionMode.NONINTERACTIVE) {
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
}

@Override
public void addCommand(String name, MethodTarget target) {
commands.put(name, target);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log warning when a command is overrwrritten? Not sure when this would happen, but it may be helpful to know when/if it does.

/**
* Construct a MethodTarget for the unique method named {@literal name} on the given object. Fails with an exception
* in case of overloaded method.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding doc for the params?

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.jline.utils.Signals;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ReflectionUtils;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright needs updating.

@@ -158,6 +143,7 @@ public Object evaluate(Input input) {

List<String> words = input.words();
if (command != null) {
Map<String, MethodTarget> methodTargets = commandRegistry.listCommands();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A slight improvement would be to move this into a methodTargets() method (used in 3 places) and it may also be an easy area of extension to allow getting commands in a slightly different fashion.

@ShellComponent
public class RegisterCommands extends AbstractShellComponent {

private final PojoMethods pojoMethods = new PojoMethods();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] Indention

import org.springframework.shell.standard.ShellOption;

@ShellComponent
public class RegisterCommands extends AbstractShellComponent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does test the dynamic commands, but is not a test.

I was looking for an IT that does some coarse-grained testing that we could add coverage to. We do not have one - may be a good thing to add.

Do the current unit tests cover the new functionality?

@onobc
Copy link
Contributor

onobc commented Feb 26, 2022

@jvalkeal - awesome addition! I added a few comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Register commands dynamically
2 participants