Skip to content

Commit 555d83a

Browse files
committed
Add support for command aliases
1 parent c656745 commit 555d83a

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/command/Command.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ public AbstractCommand execute(Consumer<CommandContext> commandExecutor) {
153153

154154
ConsumerCommandAdapter command = new ConsumerCommandAdapter(name, description, group, help, hidden,
155155
commandExecutor);
156-
157-
initAliases(command);
156+
command.setAliases(aliases);
158157

159158
return command;
160159
}
@@ -164,18 +163,11 @@ public AbstractCommand execute(Function<CommandContext, String> commandExecutor)
164163

165164
FunctionCommandAdapter command = new FunctionCommandAdapter(name, description, group, help, hidden,
166165
commandExecutor);
167-
168-
initAliases(command);
166+
command.setAliases(aliases);
169167

170168
return command;
171169
}
172170

173-
private void initAliases(AbstractCommand command) {
174-
if (aliases != null) {
175-
command.setAliases(aliases);
176-
}
177-
}
178-
179171
}
180172

181173
}

spring-shell-core/src/main/java/org/springframework/shell/core/command/CommandRegistry.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ public Set<Command> getCommands() {
6262
.filter(command -> !command.isHidden())
6363
.filter(command -> command.getName().equals(name))
6464
.findFirst()
65-
.orElse(null);
65+
.orElseGet(() -> getCommandByAlias(name));
66+
}
67+
68+
@Nullable public Command getCommandByAlias(String name) {
69+
return commands.stream().filter(command -> !command.isHidden()).filter(command -> {
70+
for (String alias : command.getAliases()) {
71+
if (alias.equals(name)) {
72+
return true;
73+
}
74+
}
75+
return false;
76+
}).findFirst().orElse(null);
6677
}
6778

6879
public List<Command> getCommandsByPrefix(String prefix) {
@@ -72,6 +83,7 @@ public List<Command> getCommandsByPrefix(String prefix) {
7283
.toList();
7384
}
7485

86+
// TODO check alias conflicts when registering commands
7587
public void registerCommand(Command command) {
7688
commands.add(command);
7789
}

spring-shell-core/src/main/java/org/springframework/shell/core/command/annotation/Command.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,12 +51,11 @@
5151
String[] name() default {};
5252

5353
/**
54-
* Define alias as an array. Given that alias should be {@code alias1 sub1} it can be
55-
* defined as:
54+
* Define aliases as an array that can be defined as:
5655
*
5756
* <pre class="code">
58-
* alias = { "alias1", "sub1" }
59-
* alias = "alias1 sub1"
57+
* alias = { "alias1", "alias2" }
58+
* alias = "alias1 alias2"
6059
* </pre>
6160
*
6261
* Values are split and trimmed meaning spaces doesn't matter.

spring-shell-core/src/main/java/org/springframework/shell/core/command/annotation/support/CommandFactoryBean.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.shell.core.command.annotation.support;
1717

1818
import java.lang.reflect.Method;
19+
import java.util.Arrays;
1920

2021
import org.apache.commons.logging.Log;
2122
import org.apache.commons.logging.LogFactory;
@@ -59,12 +60,12 @@ public Command getObject() {
5960
org.springframework.shell.core.command.annotation.Command command = MergedAnnotations.from(this.method)
6061
.get(org.springframework.shell.core.command.annotation.Command.class)
6162
.synthesize();
62-
// TODO handle aliases
6363
String name = String.join(" ", command.name());
6464
String description = command.description();
6565
String help = command.help();
6666
String group = command.group();
6767
boolean hidden = command.hidden();
68+
String[] aliases = command.alias();
6869
log.debug("Creating command bean for method '" + this.method + "' with name '" + name + "'");
6970
Class<?> declaringClass = this.method.getDeclaringClass();
7071
Object targetObject;
@@ -87,8 +88,10 @@ Ensure that the declaring class is annotated with a Spring stereotype annotation
8788
catch (BeansException e) {
8889
log.debug("No ConfigurableConversionService bean found, using a default conversion service.");
8990
}
90-
return new MethodInvokerCommandAdapter(name, description, group, help, hidden, this.method, targetObject,
91-
configurableConversionService);
91+
MethodInvokerCommandAdapter methodInvokerCommandAdapter = new MethodInvokerCommandAdapter(name, description,
92+
group, help, hidden, this.method, targetObject, configurableConversionService);
93+
methodInvokerCommandAdapter.setAliases(Arrays.stream(aliases).toList());
94+
return methodInvokerCommandAdapter;
9295
}
9396

9497
@Override

spring-shell-core/src/test/java/org/springframework/shell/core/command/CommandRegistryTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,38 @@ public ExitStatus execute(CommandContext commandContext) {
124124
assertTrue(commands.stream().anyMatch(cmd -> cmd.getName().equals("status")));
125125
}
126126

127+
@Test
128+
void getCommandByAlias() {
129+
// given
130+
commandRegistry.registerCommand(new Command() {
131+
132+
@Override
133+
public String getName() {
134+
return "list";
135+
}
136+
137+
@Override
138+
public java.util.List<String> getAliases() {
139+
return java.util.List.of("ls", "dir");
140+
}
141+
142+
@Override
143+
public ExitStatus execute(CommandContext commandContext) {
144+
return ExitStatus.OK;
145+
}
146+
});
147+
148+
// when
149+
Command cmdLs = commandRegistry.getCommandByAlias("ls");
150+
Command cmdDir = commandRegistry.getCommandByAlias("dir");
151+
Command cmdUnknown = commandRegistry.getCommandByAlias("unknown");
152+
153+
// then
154+
assertNotNull(cmdLs);
155+
assertEquals("list", cmdLs.getName());
156+
assertNotNull(cmdDir);
157+
assertEquals("list", cmdDir.getName());
158+
assertNull(cmdUnknown);
159+
}
160+
127161
}

0 commit comments

Comments
 (0)