use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.
the class App method run.
public int run(String[] args) {
CommandLine commandLine = new CommandLine(this);
commandLine.setExecutionExceptionHandler(new ExceptionMessageHandler());
commandLine.setUsageHelpAutoWidth(true);
if (args.length == 0) {
// From https://github.com/remkop/picocli/wiki/JLine-3-Examples and
// https://github.com/remkop/picocli/tree/master/picocli-shell-jline3
AnsiConsole.systemInstall();
// set up JLine built-in commands
Builtins builtins = new Builtins(App::workDir, null, null);
builtins.rename(Builtins.Command.TTOP, "top");
builtins.alias("zle", "widget");
builtins.alias("bindkey", "keymap");
PicocliCommands picocliCommands = new PicocliCommands(commandLine);
init();
Parser parser = new DefaultParser();
try (Terminal terminal = TerminalBuilder.builder().build()) {
SystemRegistry systemRegistry = new SystemRegistryImpl(parser, terminal, App::workDir, null);
systemRegistry.setCommandRegistries(builtins, picocliCommands);
LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(systemRegistry.completer()).parser(parser).variable(LineReader.HISTORY_FILE, Constant.HISTORY).variable(LineReader.HISTORY_FILE_SIZE, 100).variable(LineReader.LIST_MAX, // max tab completion candidates
50).build();
builtins.setLineReader(reader);
TailTipWidgets tailTipWidgets = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TailTipWidgets.TipType.COMPLETER);
tailTipWidgets.enable();
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));
log.info("Tooltips enabled ; press Alt-s to disable");
String prompt = "prompt> ";
// start the shell and process input until the user quits with Ctrl-D
String line;
while (true) {
try {
systemRegistry.cleanUp();
line = reader.readLine(prompt, null, (MaskingCallback) null, null);
systemRegistry.execute(line);
} catch (UserInterruptException e) {
// Ignore
} catch (EndOfFileException e) {
AnsiConsole.systemUninstall();
return 0;
} catch (Exception e) {
systemRegistry.trace(e);
}
}
} catch (IOException e) {
throw new FatalException("Unable to construct terminal console", e);
}
} else {
return commandLine.setExecutionStrategy(parseResult -> executionStrategy(commandLine, parseResult)).execute(args);
}
}
use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.
the class PncClientHelper method setup.
public static Configuration setup(boolean authenticationNeeded) {
Config config = Config.instance();
KeycloakConfig keycloakConfig = config.getActiveProfile().getKeycloak();
String bearerToken = "";
if (authenticationNeeded) {
if (keycloakConfig == null) {
throw new FatalException("Keycloak section is needed in the configuration file!");
}
keycloakConfig.validate();
bearerToken = getBearerToken(keycloakConfig);
if (bearerToken == null || bearerToken.isEmpty()) {
throw new FatalException("Credentials don't seem to be valid");
}
}
config.getActiveProfile().getPnc().validate();
String url = config.getActiveProfile().getPnc().getUrl();
try {
URI uri = new URI(url);
Integer port = null;
if (uri.getPort() != -1) {
port = uri.getPort();
}
Configuration configuration = Configuration.builder().protocol(uri.getScheme()).port(port).host(uri.getHost()).bearerToken(bearerToken).pageSize(50).build();
printBannerIfNecessary(configuration);
return configuration;
} catch (URISyntaxException e) {
throw new FatalException("URI syntax issue", e);
}
}
use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.
the class PncClientHelper method getBearerToken.
/**
* Return null if it couldn't get the authentication token. This generally means that the credentials are not valid
*
* @param keycloakConfig the keycloak config
* @return the token, or null if we couldn't get it
*/
private static String getBearerToken(KeycloakConfig keycloakConfig) {
log.debug("Authenticating to keycloak");
try {
KeycloakClient client = new DirectKeycloakClientImpl();
Credential credential;
if (keycloakConfig.isServiceAccount()) {
credential = client.getCredentialServiceAccount(keycloakConfig.getUrl(), keycloakConfig.getRealm(), keycloakConfig.getUsername(), keycloakConfig.getClientSecret());
} else {
credential = client.getCredential(keycloakConfig.getUrl(), keycloakConfig.getRealm(), keycloakConfig.getClientId(), keycloakConfig.getUsername());
}
return credential.getAccessToken();
} catch (KeycloakClientException e) {
throw new FatalException("Keycloak authentication failed!", e);
}
}
use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.
the class PigFunctionalTest method init.
static String init(Path configDir, boolean clean, Optional<String> releaseStorageUrl, Path targetDirectory) {
String suffix = prepareSuffix();
// todo release storage url mocking
if (configDir == null) {
throw new FatalException("You need to specify the configuration directory!");
}
// validate the PiG config
PigConfig pig = Config.instance().getActiveProfile().getPig();
if (pig == null) {
throw new Validate.ConfigMissingException("Pig configuration missing");
}
pig.validate();
PigContext.init(clean, configDir, targetDirectory.toAbsolutePath().toString(), releaseStorageUrl, Collections.emptyMap());
return suffix;
}
use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.
the class DirectKeycloakClientImpl method askForPassword.
private static String askForPassword() {
Console console = System.console();
if (console == null) {
throw new FatalException("Couldn't get console instance");
}
char[] passwordArray = console.readPassword("Enter your password: ");
return new String(passwordArray);
}
Aggregations