From 048994776043d9974d839d945162c535da6d2148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Fri, 3 Nov 2023 16:58:28 +0100 Subject: [PATCH] CLI: option to customize the command name and version (#686) --- .../java/ai/langstream/cli/LangStreamCLI.java | 13 +++++++++++ .../langstream/cli/commands/RootAppCmd.java | 2 +- .../cli/commands/RootArchetypeCmd.java | 2 +- .../ai/langstream/cli/commands/RootCmd.java | 2 -- .../cli/commands/RootDockerCmd.java | 2 +- .../cli/commands/RootTenantCmd.java | 2 +- .../cli/commands/VersionProvider.java | 23 ++++++++++++++----- .../AbstractDeployApplicationCmd.java | 4 ++-- .../DownloadApplicationCodeCmd.java | 2 +- .../applications/GetApplicationCmd.java | 2 +- .../applications/GetApplicationLogsCmd.java | 2 +- .../applications/ListApplicationCmd.java | 2 +- .../archetypes/ListArchetypesCmd.java | 2 +- .../cli/commands/configure/ConfigureCmd.java | 2 +- .../docker/LocalRunApplicationCmd.java | 8 +++---- 15 files changed, 46 insertions(+), 24 deletions(-) diff --git a/langstream-cli/src/main/java/ai/langstream/cli/LangStreamCLI.java b/langstream-cli/src/main/java/ai/langstream/cli/LangStreamCLI.java index 61f461196..51ae72b3b 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/LangStreamCLI.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/LangStreamCLI.java @@ -17,6 +17,7 @@ import ai.langstream.admin.client.HttpRequestFailedException; import ai.langstream.cli.commands.RootCmd; +import ai.langstream.cli.commands.VersionProvider; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -43,7 +44,19 @@ public static void main(String... args) { } public static int execute(String[] args) { + String name = System.getenv("LANGSTREAM_CLI_CUSTOMIZE_NAME"); + String description = System.getenv("LANGSTREAM_CLI_CUSTOMIZE_DESCRIPTION"); + if (name == null) { + name = "langstream"; + } + if (description == null) { + description = "LangStream CLI"; + } + String version = System.getenv("LANGSTREAM_CLI_CUSTOMIZE_VERSION"); + VersionProvider.initialize(description, version); final CommandLine cmdLine = new CommandLine(new RootCmd()); + cmdLine.setCommandName(name); + cmdLine.getCommandSpec().usageMessage().header(description); CommandLine gen = cmdLine.getSubcommands().get("generate-completion"); gen.getCommandSpec().usageMessage().hidden(true); return cmdLine.setExecutionExceptionHandler( diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootAppCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootAppCmd.java index 992f5a63d..889d5ba79 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootAppCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootAppCmd.java @@ -27,7 +27,7 @@ @CommandLine.Command( name = "apps", - header = "Manage LangStream applications", + header = "Manage ${ROOT-COMMAND-NAME} applications", subcommands = { AbstractDeployApplicationCmd.DeployApplicationCmd.class, AbstractDeployApplicationCmd.UpdateApplicationCmd.class, diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootArchetypeCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootArchetypeCmd.java index 1bf57a26d..44f3fc653 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootArchetypeCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootArchetypeCmd.java @@ -21,7 +21,7 @@ @CommandLine.Command( name = "archetypes", - header = "Use LangStream Archetypes", + header = "Use ${ROOT-COMMAND-NAME} Archetypes", subcommands = {ListArchetypesCmd.class}) @Getter public class RootArchetypeCmd { diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootCmd.java index 1ca0dd96a..93266d716 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootCmd.java @@ -21,11 +21,9 @@ import picocli.CommandLine; @CommandLine.Command( - name = "langstream", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class, scope = CommandLine.ScopeType.INHERIT, - header = "LangStream CLI", subcommands = { RootArchetypeCmd.class, RootAppCmd.class, diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootDockerCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootDockerCmd.java index fcde84574..d7d98f084 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootDockerCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootDockerCmd.java @@ -21,7 +21,7 @@ @CommandLine.Command( name = "docker", - header = "Run LangStream locally", + header = "Run ${ROOT-COMMAND-NAME} locally", subcommands = {LocalRunApplicationCmd.class}) @Getter public class RootDockerCmd { diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootTenantCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootTenantCmd.java index 989087fc6..44ec1d731 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/RootTenantCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/RootTenantCmd.java @@ -26,7 +26,7 @@ @CommandLine.Command( name = "tenants", - header = "Manage LangStream tenants", + header = "Manage ${ROOT-COMMAND-NAME} tenants", subcommands = { PutTenantCmd.class, DeleteTenantCmd.class, diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/VersionProvider.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/VersionProvider.java index dc7ab7b8c..93b04e818 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/VersionProvider.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/VersionProvider.java @@ -26,7 +26,11 @@ public class VersionProvider implements CommandLine.IVersionProvider { - private static final List VERSION_INFO = getVersionFromJar(); + private static List VERSION_INFO = null; + + public static void initialize(String label, String forceVersion) { + VERSION_INFO = getVersionFromJar(label, forceVersion); + } public static String getMavenVersion() { return VERSION_INFO.get(1); @@ -36,12 +40,19 @@ public String[] getVersion() { return new String[] {VERSION_INFO.get(0)}; } - private static List getVersionFromJar() { + private static List getVersionFromJar(String label, String forceVersion) { try { - Manifest manifest = getManifest(); - final String version = getVersionFromManifest(manifest); - final String gitRevision = getGitRevisionFromManifest(manifest); - return List.of(String.format("LangStream CLI %s (%s)", version, gitRevision), version); + final String version; + final String gitRevision; + if (forceVersion != null) { + version = forceVersion; + gitRevision = ""; + } else { + final Manifest manifest = getManifest(); + version = getVersionFromManifest(manifest); + gitRevision = " (" + getGitRevisionFromManifest(manifest) + ")"; + } + return List.of(String.format("%s %s", label, version + gitRevision), version); } catch (Throwable t) { // never ever let this exception bubble up otherwise any command will fail return List.of(String.format("Error: %s", t.getMessage()), "unknown"); diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/AbstractDeployApplicationCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/AbstractDeployApplicationCmd.java index d6f5b4338..f176f1b3c 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/AbstractDeployApplicationCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/AbstractDeployApplicationCmd.java @@ -29,7 +29,7 @@ public abstract class AbstractDeployApplicationCmd extends BaseApplicationCmd { - @CommandLine.Command(name = "deploy", header = "Deploy a LangStream application") + @CommandLine.Command(name = "deploy", header = "Deploy an application") public static class DeployApplicationCmd extends AbstractDeployApplicationCmd { @CommandLine.Parameters(description = "Name of the application") @@ -101,7 +101,7 @@ Formats format() { } } - @CommandLine.Command(name = "update", header = "Update an existing LangStream application") + @CommandLine.Command(name = "update", header = "Update an existing application") public static class UpdateApplicationCmd extends AbstractDeployApplicationCmd { @CommandLine.Parameters(description = "Name of the application") diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/DownloadApplicationCodeCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/DownloadApplicationCodeCmd.java index 96e703aca..3cc0bd16f 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/DownloadApplicationCodeCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/DownloadApplicationCodeCmd.java @@ -24,7 +24,7 @@ import lombok.SneakyThrows; import picocli.CommandLine; -@CommandLine.Command(name = "download", header = "Get LangStream application code") +@CommandLine.Command(name = "download", header = "Get the application code") public class DownloadApplicationCodeCmd extends BaseApplicationCmd { protected static final Pattern REGEX_PATTERN = diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationCmd.java index b0c5767e0..da3759771 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationCmd.java @@ -18,7 +18,7 @@ import lombok.SneakyThrows; import picocli.CommandLine; -@CommandLine.Command(name = "get", header = "Get LangStream application status") +@CommandLine.Command(name = "get", header = "Get the application status") public class GetApplicationCmd extends BaseApplicationCmd { @CommandLine.Parameters(description = "ID of the application") diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationLogsCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationLogsCmd.java index 92d627bd5..0ac784225 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationLogsCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/GetApplicationLogsCmd.java @@ -24,7 +24,7 @@ import lombok.SneakyThrows; import picocli.CommandLine; -@CommandLine.Command(name = "logs", header = "Get LangStream application logs") +@CommandLine.Command(name = "logs", header = "Get the application logs") public class GetApplicationLogsCmd extends BaseApplicationCmd { @CommandLine.Parameters(description = "ID of the application") diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/ListApplicationCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/ListApplicationCmd.java index 6743cf0b4..31a441008 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/ListApplicationCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/applications/ListApplicationCmd.java @@ -22,7 +22,7 @@ import lombok.SneakyThrows; import picocli.CommandLine; -@CommandLine.Command(name = "list", header = "List all LangStream applications") +@CommandLine.Command(name = "list", header = "List all applications") public class ListApplicationCmd extends BaseApplicationCmd { protected static final String[] COLUMNS_FOR_RAW = { diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/archetypes/ListArchetypesCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/archetypes/ListArchetypesCmd.java index 7494d4eba..056732ef3 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/archetypes/ListArchetypesCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/archetypes/ListArchetypesCmd.java @@ -20,7 +20,7 @@ import lombok.SneakyThrows; import picocli.CommandLine; -@CommandLine.Command(name = "list", header = "List all LangStream archetypes") +@CommandLine.Command(name = "list", header = "List all the available archetypes") public class ListArchetypesCmd extends BaseArchetypeCmd { protected static final String[] COLUMNS_FOR_RAW = {"id", "labels"}; diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/configure/ConfigureCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/configure/ConfigureCmd.java index 712d56e97..5a43cbea2 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/configure/ConfigureCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/configure/ConfigureCmd.java @@ -26,7 +26,7 @@ @CommandLine.Command( name = "configure", header = - "Configure LangStream tenant and authentication. DEPRECATED. Use 'langstream profiles' instead.") + "Configure tenant and authentication. DEPRECATED. Use '${ROOT-COMMAND-NAME} profiles' instead.") @Getter public class ConfigureCmd extends BaseCmd { diff --git a/langstream-cli/src/main/java/ai/langstream/cli/commands/docker/LocalRunApplicationCmd.java b/langstream-cli/src/main/java/ai/langstream/cli/commands/docker/LocalRunApplicationCmd.java index 708831bd7..18dc29ec6 100644 --- a/langstream-cli/src/main/java/ai/langstream/cli/commands/docker/LocalRunApplicationCmd.java +++ b/langstream-cli/src/main/java/ai/langstream/cli/commands/docker/LocalRunApplicationCmd.java @@ -52,7 +52,7 @@ import org.apache.commons.lang3.SystemUtils; import picocli.CommandLine; -@CommandLine.Command(name = "run", header = "Run on a docker container a LangStream application") +@CommandLine.Command(name = "run", header = "Run the application on a docker container") public class LocalRunApplicationCmd extends BaseDockerCmd { protected static final String LOCAL_DOCKER_RUN_PROFILE = "local-docker-run"; @@ -91,7 +91,7 @@ public class LocalRunApplicationCmd extends BaseDockerCmd { @CommandLine.Option( names = {"--start-webservices"}, - description = "Start LangStream webservices") + description = "Start webservices") private boolean startWebservices = true; @CommandLine.Option( @@ -142,13 +142,13 @@ public class LocalRunApplicationCmd extends BaseDockerCmd { @CommandLine.Option( names = {"--langstream-runtime-version"}, - description = "Version of the LangStream runtime to use", + description = "Version of the runtime to use", defaultValue = "${env:LANGSTREAM_RUNTIME_DOCKER_IMAGE_VERSION}") private String dockerImageVersion; @CommandLine.Option( names = {"--langstream-runtime-docker-image"}, - description = "Docker image of the LangStream runtime to use", + description = "Docker image of the runtime to use", defaultValue = "${env:LANGSTREAM_RUNTIME_DOCKER_IMAGE}") private String dockerImageName;