Show an error message when file does not exist for the config-file parameter (#27547)

Closes #26443

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
This commit is contained in:
Martin Bartoš 2024-03-05 16:39:30 +01:00 committed by GitHub
parent d12711e858
commit ee64fb5203
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View file

@ -28,6 +28,9 @@ import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ScopeType;
import java.nio.file.Files;
import java.nio.file.Path;
@Command(name = "keycloak",
header = {
"Keycloak - Open Source Identity and Access Management",
@ -106,6 +109,10 @@ public final class Main {
description = "Set the path to a configuration file. By default, configuration properties are read from the \"keycloak.conf\" file in the \"conf\" directory.",
paramLabel = "file")
public void setConfigFile(String path) {
if (Files.notExists(Path.of(path))) {
throw new CommandLine.ParameterException(spec.commandLine(),
String.format("File specified via '%s' or '%s' option does not exist.", CONFIG_FILE_LONG_NAME, CONFIG_FILE_SHORT_NAME));
}
System.setProperty(KeycloakPropertiesConfigSource.KEYCLOAK_CONFIG_FILE_PROP, path);
}
}

View file

@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.OPTIMIZED_BUILD_OPTION_LONG;
import static org.keycloak.quarkus.runtime.cli.command.Main.CONFIG_FILE_LONG_NAME;
import org.junit.jupiter.api.Test;
import org.keycloak.it.junit5.extension.CLIResult;
@ -152,4 +153,12 @@ public class StartCommandDistTest {
cliResult = dist.run("start", "--db=dev-mem", "--cache=local", "--hostname=localhost", "--http-enabled=true");
cliResult.assertNoMessage("The previous optimized build will be overridden with the following build options:"); // no message, same values provided during auto-build
}
@Test
@Launch({CONFIG_FILE_LONG_NAME + "=src/test/resources/non-existing.conf", "start"})
void testInvalidConfigFileOption(LaunchResult result) {
CLIResult cliResult = (CLIResult) result;
cliResult.assertError("File specified via '--config-file' or '-cf' option does not exist.");
cliResult.assertError(String.format("Try '%s --help' for more information on the available options.", KeycloakDistribution.SCRIPT_CMD));
}
}