Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
import static org.apache.ignite.internal.cli.commands.Options.Constants.CLUSTER_URL_OPTION;
import static org.apache.ignite.internal.cli.commands.Options.Constants.CLUSTER_URL_OPTION_DESC;

import jakarta.inject.Inject;
import java.net.URL;
import org.apache.ignite.internal.cli.commands.ProfileMixin;
import org.apache.ignite.internal.cli.config.CliConfigKeys;
import org.apache.ignite.internal.cli.config.ConfigManager;
import org.apache.ignite.internal.cli.config.ConfigManagerProvider;
import org.apache.ignite.internal.cli.core.converters.RestEndpointUrlConverter;
import org.jetbrains.annotations.Nullable;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;

/**
* Mixin class for cluster URL option.
* Mixin class for cluster URL and profile options in REPL mode.
*/
public class ClusterUrlMixin {
/** Cluster endpoint URL option. */
Expand All @@ -38,7 +45,29 @@ public class ClusterUrlMixin {
)
private URL clusterUrl;

/** Profile to get default values from. */
@Mixin
private ProfileMixin profile;

@Inject
private ConfigManagerProvider configManagerProvider;

/**
* Gets cluster URL from either the command line or from the specified profile in the config.
*
* @return cluster URL
*/
@Nullable
public String getClusterUrl() {
return clusterUrl != null ? clusterUrl.toString() : null;
if (clusterUrl != null) {
return clusterUrl.toString();
} else {
String profileName = profile.getProfileName();
if (profileName != null) {
ConfigManager configManager = configManagerProvider.get();
return configManager.getProperty(CliConfigKeys.CLUSTER_URL.value(), profileName);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,34 @@
package org.apache.ignite.internal.cli.commands.cluster;

import jakarta.inject.Inject;
import org.apache.ignite.internal.cli.commands.ProfileMixin;
import org.apache.ignite.internal.cli.config.CliConfigKeys;
import org.apache.ignite.internal.cli.config.ConfigManager;
import org.apache.ignite.internal.cli.config.ConfigManagerProvider;
import picocli.CommandLine.Mixin;

/**
* Mixin class to combine cluster URL and profile options.
* Mixin class for cluster URL and profile options.
*/
public class ClusterUrlProfileMixin {
/** Cluster endpoint URL option. */
@Mixin
private ClusterUrlMixin clusterUrl;

/** Profile to get default values from. */
@Mixin
private ProfileMixin profileName;

@Inject
private ConfigManagerProvider configManagerProvider;

/**
* Gets cluster URL from either the command line or from the config with specified or default profile.
* Gets cluster URL from either the command line or from the default profile in the config.
*
* @return cluster URL
*/
public String getClusterUrl() {
if (clusterUrl.getClusterUrl() != null) {
return clusterUrl.getClusterUrl();
String clusterUrlFromOptions = clusterUrl.getClusterUrl();
if (clusterUrlFromOptions != null) {
return clusterUrlFromOptions;
} else {
ConfigManager configManager = configManagerProvider.get();
return configManager.getProperty(CliConfigKeys.CLUSTER_URL.value(), profileName.getProfileName());
return configManager.getCurrentProperty(CliConfigKeys.CLUSTER_URL.value());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@

import jakarta.inject.Inject;
import java.net.URL;
import org.apache.ignite.internal.cli.commands.ProfileMixin;
import org.apache.ignite.internal.cli.config.CliConfigKeys;
import org.apache.ignite.internal.cli.config.ConfigManager;
import org.apache.ignite.internal.cli.config.ConfigManagerProvider;
import org.apache.ignite.internal.cli.core.converters.RestEndpointUrlConverter;
import org.apache.ignite.internal.cli.core.exception.IgniteCliException;
import org.apache.ignite.internal.cli.core.repl.registry.NodeNameRegistry;
import org.jetbrains.annotations.Nullable;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;

/**
Expand All @@ -40,6 +45,13 @@ public class NodeUrlMixin {
@ArgGroup
private Options options;

/** Profile to get default values from. */
@Mixin
private ProfileMixin profile;

@Inject
private ConfigManagerProvider configManagerProvider;

@Inject
private NodeNameRegistry nodeNameRegistry;

Expand Down Expand Up @@ -84,6 +96,11 @@ public String getNodeUrl() {
+ " not found. Provide a valid name or use a URL"));
}
}
String profileName = profile.getProfileName();
if (profileName != null) {
ConfigManager configManager = configManagerProvider.get();
return configManager.getProperty(CliConfigKeys.CLUSTER_URL.value(), profileName);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
import java.util.Optional;
import java.util.Set;
import org.apache.ignite.internal.cli.commands.cluster.ClusterUrlMixin;
import org.apache.ignite.internal.cli.commands.cluster.ClusterUrlProfileMixin;
import org.apache.ignite.internal.cli.commands.cluster.init.ClusterInitOptions;
import org.apache.ignite.internal.cli.commands.connect.ConnectOptions;
import org.apache.ignite.internal.cli.commands.node.NodeUrlMixin;
import org.apache.ignite.internal.cli.commands.node.NodeUrlProfileMixin;
import org.apache.ignite.internal.cli.commands.recovery.cluster.reset.ResetClusterMixin;
import org.apache.ignite.internal.cli.commands.recovery.partitions.states.PartitionStatesMixin;
import org.apache.ignite.internal.cli.core.repl.registry.NodeNameRegistry;
Expand All @@ -64,7 +66,7 @@ class MixinTest {

@Test
void doubleInvocationNodeName() {
NodeCommand command = new NodeCommand();
NodeReplCommand command = new NodeReplCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

String nodeName = "test";
Expand All @@ -78,7 +80,7 @@ void doubleInvocationNodeName() {

@Test
void doubleInvocationNodeUrl() {
NodeCommand command = new NodeCommand();
NodeReplCommand command = new NodeReplCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

String nodeUrl = "http://test";
Expand All @@ -92,7 +94,7 @@ void doubleInvocationNodeUrl() {

@Test
void doubleInvocationClusterUrl() {
ClusterCommand command = new ClusterCommand();
ClusterReplCommand command = new ClusterReplCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

String clusterUrl = "http://test";
Expand All @@ -104,6 +106,94 @@ void doubleInvocationClusterUrl() {
assertThat(command.clusterUrl.getClusterUrl(), is(nullValue()));
}

@Test
void clusterUrlDefaultValue() {
ClusterCommand command = new ClusterCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

// Default value is taken from the config
commandLine.parseArgs();
assertThat(command.clusterUrl.getClusterUrl(), is("http://localhost:10300"));

// Value is taken from the option
commandLine.parseArgs("--url=http://test");
assertThat(command.clusterUrl.getClusterUrl(), is("http://test"));

// Value is taken from the profile
commandLine.parseArgs("--profile=test");
assertThat(command.clusterUrl.getClusterUrl(), is("http://localhost:10301"));

// Option overrides profiles
commandLine.parseArgs("--url=http://test", "--profile=test");
assertThat(command.clusterUrl.getClusterUrl(), is("http://test"));
}

@Test
void clusterUrlDefaultValueRepl() {
ClusterReplCommand command = new ClusterReplCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

// Default value is null
commandLine.parseArgs();
assertThat(command.clusterUrl.getClusterUrl(), is(nullValue()));

// Value is taken from the option
commandLine.parseArgs("--url=http://test");
assertThat(command.clusterUrl.getClusterUrl(), is("http://test"));

// Value is taken from the profile
commandLine.parseArgs("--profile=test");
assertThat(command.clusterUrl.getClusterUrl(), is("http://localhost:10301"));

// Option overrides profiles
commandLine.parseArgs("--url=http://test", "--profile=test");
assertThat(command.clusterUrl.getClusterUrl(), is("http://test"));
}

@Test
void nodeUrlDefaultValue() {
NodeCommand command = new NodeCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

// Default value is taken from the config
commandLine.parseArgs();
assertThat(command.nodeUrl.getNodeUrl(), is("http://localhost:10300"));

// Value is taken from the option
commandLine.parseArgs("--url=http://test");
assertThat(command.nodeUrl.getNodeUrl(), is("http://test"));

// Value is taken from the profile
commandLine.parseArgs("--profile=test");
assertThat(command.nodeUrl.getNodeUrl(), is("http://localhost:10301"));

// Option overrides profiles
commandLine.parseArgs("--url=http://test", "--profile=test");
assertThat(command.nodeUrl.getNodeUrl(), is("http://test"));
}

@Test
void nodeUrlDefaultValueRepl() {
NodeReplCommand command = new NodeReplCommand();
CommandLine commandLine = new CommandLine(command, new MicronautFactory(context));

// Default value is null
commandLine.parseArgs();
assertThat(command.nodeUrl.getNodeUrl(), is(nullValue()));

// Value is taken from the option
commandLine.parseArgs("--url=http://test");
assertThat(command.nodeUrl.getNodeUrl(), is("http://test"));

// Value is taken from the profile
commandLine.parseArgs("--profile=test");
assertThat(command.nodeUrl.getNodeUrl(), is("http://localhost:10301"));

// Option overrides profiles
commandLine.parseArgs("--url=http://test", "--profile=test");
assertThat(command.nodeUrl.getNodeUrl(), is("http://test"));
}

@Test
void doubleInvocationUnitList() {
UnitCommand command = new UnitCommand();
Expand Down Expand Up @@ -183,12 +273,24 @@ void doubleInvocationPartitionStates() {

@Command
private static class NodeCommand {
@Mixin
private NodeUrlProfileMixin nodeUrl;
}

@Command
private static class NodeReplCommand {
@Mixin
private NodeUrlMixin nodeUrl;
}

@Command
private static class ClusterCommand {
@Mixin
private ClusterUrlProfileMixin clusterUrl;
}

@Command
private static class ClusterReplCommand {
@Mixin
private ClusterUrlMixin clusterUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

import io.micronaut.configuration.picocli.MicronautFactory;
import io.micronaut.context.ApplicationContext;
Expand All @@ -32,7 +31,8 @@
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;
import picocli.CommandLine.Model.OptionSpec;

Expand All @@ -49,18 +49,13 @@ static void setDumbTerminal() {
System.setProperty("org.jline.terminal.dumb", "true");
}

@Test
void nonReplCommands() {
CommandLine cmd = new CommandLine(TopLevelCliCommand.class, new MicronautFactory(context));
@ParameterizedTest
@ValueSource(classes = {TopLevelCliCommand.class, TopLevelCliReplCommand.class})
void everyCommandWithUrlOptionHasProfileOption(Class<?> cmdClass) {
CommandLine cmd = new CommandLine(cmdClass, new MicronautFactory(context));
assertThat(subCommands(cmd), everyItem(profileOption(notNullValue(OptionSpec.class))));
}

@Test
void replCommands() {
CommandLine cmd = new CommandLine(TopLevelCliReplCommand.class, new MicronautFactory(context));
assertThat(subCommands(cmd), everyItem(profileOption(nullValue(OptionSpec.class))));
}

private static Matcher<CommandLine> profileOption(Matcher<OptionSpec> optionMatcher) {
return new TypeSafeMatcher<>() {
@Override
Expand All @@ -76,7 +71,7 @@ protected boolean matchesSafely(CommandLine item) {

@Override
protected void describeMismatchSafely(CommandLine item, Description mismatchDescription) {
mismatchDescription.appendText(item.getCommandSpec().qualifiedName())
mismatchDescription.appendValue(item.getCommandSpec().qualifiedName())
.appendText(" has --profile option ").appendValue(item.getCommandSpec().findOption("--profile"));
}
};
Expand Down