From 9783c7c990100f410ba8e348d2d2740d455f34ed Mon Sep 17 00:00:00 2001 From: Andrey Litvitski Date: Wed, 7 May 2025 22:14:33 +0300 Subject: [PATCH] improve command aliasing in `CommandRegistration.BaseBuilder` Signed-off-by: Andrey Litvitski --- .../shell/command/CommandRegistration.java | 25 ++++++++++++++++++- .../command/CommandRegistrationTests.java | 5 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java index 2f1b0edc1..16c6c7b86 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2023 the original author or authors. + * Copyright 2022-2025 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. @@ -41,6 +41,7 @@ * Interface defining a command registration endpoint. * * @author Janne Valkealahti + * @author Andrey Litvitski */ public interface CommandRegistration { @@ -764,6 +765,14 @@ public interface Builder { */ AliasSpec withAlias(); + /** + * Define an alias what this command should execute + * + * @param commands the commands + * @return builder for chaining + */ + Builder withAlias(String... commands); + /** * Define an exit code what this command should execute * @@ -1421,6 +1430,20 @@ public AliasSpec withAlias() { return spec; } + @Override + public Builder withAlias(String... commands) { + Assert.notNull(commands, "commands must be set"); + DefaultAliasSpec spec = new DefaultAliasSpec(this); + spec.commands = Arrays.asList(commands).stream() + .flatMap(c -> Stream.of(c.split(" "))) + .filter(c -> StringUtils.hasText(c)) + .map(c -> c.trim()) + .collect(Collectors.toList()) + .toArray(new String[0]); + this.aliasSpecs.add(spec); + return this; + } + @Override public ExitCodeSpec withExitCode() { DefaultExitCodeSpec spec = new DefaultExitCodeSpec(this); diff --git a/spring-shell-core/src/test/java/org/springframework/shell/command/CommandRegistrationTests.java b/spring-shell-core/src/test/java/org/springframework/shell/command/CommandRegistrationTests.java index 0df451396..dd60f4bf4 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/command/CommandRegistrationTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/command/CommandRegistrationTests.java @@ -411,15 +411,16 @@ public void testAliases() { .command("alias2") .group("Alias Group") .and() + .withAlias("alias3") .withTarget() .function(function1) .and() .build(); assertThat(registration.getCommand()).isEqualTo("command1"); assertThat(registration.getGroup()).isEqualTo("Test Group"); - assertThat(registration.getAliases()).hasSize(2); + assertThat(registration.getAliases()).hasSize(3); assertThat(registration.getAliases().stream().map(CommandAlias::getCommand)).containsExactlyInAnyOrder("alias1", - "alias2"); + "alias2", "alias3"); assertThat(registration.getAliases().get(0).getGroup()).isEqualTo("Alias Group"); assertThat(registration.getAliases().get(1).getGroup()).isEqualTo("Alias Group"); }