Skip to content

Commit

Permalink
CLI: option to customize the command name and version (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi authored Nov 3, 2023
1 parent 42627d5 commit 0489947
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 24 deletions.
13 changes: 13 additions & 0 deletions langstream-cli/src/main/java/ai/langstream/cli/LangStreamCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@CommandLine.Command(
name = "tenants",
header = "Manage LangStream tenants",
header = "Manage ${ROOT-COMMAND-NAME} tenants",
subcommands = {
PutTenantCmd.class,
DeleteTenantCmd.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@

public class VersionProvider implements CommandLine.IVersionProvider {

private static final List<String> VERSION_INFO = getVersionFromJar();
private static List<String> 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);
Expand All @@ -36,12 +40,19 @@ public String[] getVersion() {
return new String[] {VERSION_INFO.get(0)};
}

private static List<String> getVersionFromJar() {
private static List<String> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 0489947

Please sign in to comment.