Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Accept environments as parameters when executing PicocliRunner #362

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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 @@ -20,14 +20,23 @@
import io.micronaut.context.env.CommandLinePropertySource;
import io.micronaut.context.env.Environment;
import io.micronaut.core.annotation.TypeHint;
import io.micronaut.core.util.CollectionUtils;
import picocli.CommandLine;
import picocli.CommandLine.*;
import picocli.CommandLine.Command;
import picocli.CommandLine.ExecutionException;
import picocli.CommandLine.InitializationException;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.lang.reflect.Executable;
import java.lang.reflect.Parameter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.Callable;

/**
Expand Down Expand Up @@ -180,6 +189,21 @@ public static int execute(Class<?> clazz, String... args) {
}
}

/**
* Same than {@link #execute(Class, String...)}, but with a list of customEnvironments to be applied.
* @param clazz the Runnable or Callable command class
* @param environments List of environments to be applied when building {@link ApplicationContext}
* @param args the command line arguments
* @return the exit code returned by {@link CommandLine#execute(String...)}
* @see #execute(Class, String...)
*/
public static int execute(final Class<?> clazz, final Collection<String> environments, final String... args) {
final ApplicationContextBuilder builder = buildApplicationContext(clazz, environments, args);
try (ApplicationContext context = builder.start()) {
return execute(clazz, context, args);
}
}

/**
* Obtains an instance of the specified {@code Callable} or {@code Runnable} command class
* from the specified context, injecting any beans from the specified context as required,
Expand All @@ -203,11 +227,21 @@ private static int execute(Class<?> clazz, ApplicationContext context, String...
}

private static ApplicationContextBuilder buildApplicationContext(Class<?> cls, String[] args) {
io.micronaut.core.cli.CommandLine commandLine = io.micronaut.core.cli.CommandLine.parse(args);
CommandLinePropertySource commandLinePropertySource = new CommandLinePropertySource(commandLine);
return buildApplicationContext(cls, Collections.singletonList(Environment.CLI), args);
}

private static ApplicationContextBuilder buildApplicationContext(final Class<?> cls, final Collection<String> customEnvironments, final String[] args) {
final io.micronaut.core.cli.CommandLine commandLine = io.micronaut.core.cli.CommandLine.parse(args);
final CommandLinePropertySource commandLinePropertySource = new CommandLinePropertySource(commandLine);

final Set<String> environments = new HashSet<>();
environments.add(Environment.CLI);
if (CollectionUtils.isNotEmpty(customEnvironments)) {
environments.addAll(customEnvironments);
}
return ApplicationContext
.builder(cls, Environment.CLI)
.propertySources(commandLinePropertySource);
.builder(cls, environments.toArray(String[]::new))
.propertySources(commandLinePropertySource);
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package io.micronaut.configuration.picocli;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.concurrent.Callable;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Value;
import org.junit.Before;
import org.junit.Test;

import io.micronaut.context.annotation.Property;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.concurrent.Callable;

import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.containsString;

public class PicocliRunnerTest {

private static final int EXIT_CODE_INVALID_INPUT = 222;
Expand Down Expand Up @@ -144,6 +148,12 @@ public void testExecuteNormal() {
assertEquals(0, exitCode);
}

@Test
public void testExecuteNormal_withProvidedEnvironments() {
int exitCode = PicocliRunner.execute(MyRunnableCmd.class, List.of("jdbc"), "-v");
assertEquals(0, exitCode);
}

@Test
public void testExecuteNonZeroExitCode() {
int exitCode = PicocliRunner.execute(MyCallableCmd.class, "-x=11", "-y", "3");
Expand Down
Loading