This is an automated email from the ASF dual-hosted git repository. joerghoh pushed a commit to branch SLING-13348 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git
commit 8fd3d9f812faaec83acd9dfbe0d89b5efa657005 Author: Joerg Hoh <[email protected]> AuthorDate: Tue Oct 6 20:35:12 2026 +0200 SLING-13348 add possibility to display the version --- README.md | 7 +++++++ .../org/apache/sling/cli/impl/CommandProcessor.java | 11 ++++++++++- .../apache/sling/cli/impl/CommandProcessorTest.java | 21 +++++++++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 90c97d8..724a590 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,13 @@ After building, run the image with: This invocation produces a list of available commands. +To print the CLI version and exit, use `--version` (or `-V`): + + docker run --rm apache/sling-committer-cli --version + +The version comes from the running OSGi bundle and uses OSGi notation, for example +`Apache Sling Committer CLI 1.0.0.SNAPSHOT`. + ## Commands The commands can be executed in 3 different modes: diff --git a/src/main/java/org/apache/sling/cli/impl/CommandProcessor.java b/src/main/java/org/apache/sling/cli/impl/CommandProcessor.java index dc5d2a0..9cce104 100644 --- a/src/main/java/org/apache/sling/cli/impl/CommandProcessor.java +++ b/src/main/java/org/apache/sling/cli/impl/CommandProcessor.java @@ -63,6 +63,12 @@ public class CommandProcessor { private BundleContext ctx; private Config cfg; + @CommandLine.Option( + names = {"-V", "--version"}, + versionHelp = true, + description = "Print version information and exit.") + private boolean versionRequested; + private static final Map<String, Class> CLI_GROUPS; static { @@ -73,7 +79,7 @@ public class CommandProcessor { private Map<String, TreeSet<CommandWithProps>> commands = new ConcurrentHashMap<>(); @Activate - private void activate(BundleContext ctx, Config cfg) { + void activate(BundleContext ctx, Config cfg) { this.ctx = ctx; this.cfg = cfg; } @@ -99,6 +105,9 @@ public class CommandProcessor { void runCommand() { System.setProperty("picocli.usage.width", "140"); CommandLine commandLine = new CommandLine(this); + commandLine + .getCommandSpec() + .version("Apache Sling Committer CLI " + ctx.getBundle().getVersion()); commandLine.addSubcommand(CommandLine.HelpCommand.class); for (Map.Entry<String, TreeSet<CommandWithProps>> entry : commands.entrySet()) { String group = entry.getKey(); diff --git a/src/test/java/org/apache/sling/cli/impl/CommandProcessorTest.java b/src/test/java/org/apache/sling/cli/impl/CommandProcessorTest.java index 4e07f4c..affe58f 100644 --- a/src/test/java/org/apache/sling/cli/impl/CommandProcessorTest.java +++ b/src/test/java/org/apache/sling/cli/impl/CommandProcessorTest.java @@ -20,13 +20,17 @@ package org.apache.sling.cli.impl; import java.util.Map; -import org.apache.sling.testing.mock.osgi.junit.OsgiContext; -import org.junit.Rule; +import org.junit.Before; import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Version; import picocli.CommandLine; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class CommandProcessorTest { @@ -58,8 +62,15 @@ public class CommandProcessorTest { } } - @Rule - public final OsgiContext context = new OsgiContext(); + private BundleContext bundleContext; + + @Before + public void setUp() { + Bundle bundle = mock(Bundle.class); + when(bundle.getVersion()).thenReturn(new Version("1.2.3.SNAPSHOT")); + bundleContext = mock(BundleContext.class); + when(bundleContext.getBundle()).thenReturn(bundle); + } /** * Validates that a variety of arguments are parsed correctly @@ -93,6 +104,7 @@ public class CommandProcessorTest { Command.PROPERTY_NAME_COMMAND_NAME, MockCommand.COMMAND_NAME)); + commandProcessor.activate(bundleContext, mock(CommandProcessor.Config.class)); commandProcessor.runCommand(); assertThat(cmd.invocationCount, equalTo(1)); @@ -132,6 +144,7 @@ public class CommandProcessorTest { Command.PROPERTY_NAME_COMMAND_NAME, MockCommand.COMMAND_NAME)); + commandProcessor.activate(bundleContext, mock(CommandProcessor.Config.class)); commandProcessor.runCommand(); assertThat(cmd.invocationCount, equalTo(1));
