Search in sources :

Example 1 with TerminalColor

use of org.aesh.readline.terminal.formatting.TerminalColor in project wildfly-core by wildfly.

the class Util method configureColors.

public static void configureColors(CommandContext ctx) {
    ColorConfig colorConfig = ctx.getConfig().getColorConfig();
    if (colorConfig != null) {
        ERROR_COLOR = new TerminalColor((colorConfig.getErrorColor() != null) ? colorConfig.getErrorColor() : Color.RED, Color.DEFAULT, Color.Intensity.BRIGHT);
        if (!isWindows()) {
            WARN_COLOR = new TerminalColor((colorConfig.getWarnColor() != null) ? colorConfig.getWarnColor() : Color.YELLOW, Color.DEFAULT, Color.Intensity.NORMAL);
        } else {
            WARN_COLOR = new TerminalColor((colorConfig.getWarnColor() != null) ? colorConfig.getWarnColor() : Color.YELLOW, Color.DEFAULT, Color.Intensity.BRIGHT);
        }
        SUCCESS_COLOR = new TerminalColor((colorConfig.getSuccessColor() != null) ? colorConfig.getSuccessColor() : Color.DEFAULT, Color.DEFAULT, Color.Intensity.NORMAL);
        REQUIRED_COLOR = new TerminalColor((colorConfig.getRequiredColor() != null) ? colorConfig.getRequiredColor() : Color.CYAN, Color.DEFAULT, Color.Intensity.BRIGHT);
        WORKFLOW_COLOR = new TerminalColor((colorConfig.getWorkflowColor() != null) ? colorConfig.getWorkflowColor() : Color.GREEN, Color.DEFAULT, Color.Intensity.BRIGHT);
        PROMPT_COLOR = new TerminalColor((colorConfig.getWorkflowColor() != null) ? colorConfig.getPromptColor() : Color.BLUE, Color.DEFAULT, Color.Intensity.BRIGHT);
    } else {
        ERROR_COLOR = new TerminalColor(Color.RED, Color.DEFAULT, Color.Intensity.BRIGHT);
        if (!isWindows()) {
            WARN_COLOR = new TerminalColor(Color.YELLOW, Color.DEFAULT, Color.Intensity.NORMAL);
        } else {
            WARN_COLOR = new TerminalColor(Color.YELLOW, Color.DEFAULT, Color.Intensity.BRIGHT);
        }
        SUCCESS_COLOR = new TerminalColor(Color.DEFAULT, Color.DEFAULT, Color.Intensity.NORMAL);
        REQUIRED_COLOR = new TerminalColor(Color.MAGENTA, Color.DEFAULT, Color.Intensity.BRIGHT);
        WORKFLOW_COLOR = new TerminalColor(Color.GREEN, Color.DEFAULT, Color.Intensity.BRIGHT);
        PROMPT_COLOR = new TerminalColor(Color.BLUE, Color.DEFAULT, Color.Intensity.BRIGHT);
    }
}
Also used : TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor)

Example 2 with TerminalColor

use of org.aesh.readline.terminal.formatting.TerminalColor in project infinispan by infinispan.

the class Shell method exec.

@Override
public CommandResult exec(ContextAwareCommandInvocation invocation) {
    KubernetesClient client = KubernetesContext.getClient(invocation);
    namespace = Kube.getNamespaceOrDefault(client, namespace);
    GenericKubernetesResource infinispan = client.genericKubernetesResources(INFINISPAN_CLUSTER_CRD).inNamespace(namespace).withName(name).get();
    if (infinispan == null) {
        throw Messages.MSG.noSuchService(name, namespace);
    }
    String endpointSecretName = Kube.getProperty(infinispan, "spec", "security", "endpointSecretName");
    String certSecretName = Kube.getProperty(infinispan, "spec", "security", "endpointEncryption", "certSecretName");
    Pod pod;
    if (podName == null) {
        pod = client.pods().inNamespace(namespace).withLabel("infinispan_cr", name).list().getItems().stream().filter(p -> "running".equalsIgnoreCase(p.getStatus().getPhase())).findFirst().orElse(null);
    } else {
        pod = client.pods().inNamespace(namespace).withName(podName).get();
    }
    if (pod == null) {
        throw Messages.MSG.noRunningPodsInService(name);
    }
    // Port forwarding mode
    List<ContainerPort> ports = pod.getSpec().getContainers().get(0).getPorts();
    // Find the `infinispan` port
    ContainerPort containerPort = ports.stream().filter(p -> "infinispan".equals(p.getName())).findFirst().get();
    try (LocalPortForward portForward = client.pods().inNamespace(namespace).withName(pod.getMetadata().getName()).portForward(containerPort.getContainerPort())) {
        StringBuilder connection = new StringBuilder();
        List<String> args = new ArrayList<>();
        if (certSecretName != null) {
            connection.append("https://");
            Secret secret = Kube.getSecret(client, namespace, certSecretName);
            final byte[] cert;
            final String suffix;
            if (secret.getData().containsKey("keystore.p12")) {
                cert = Base64.getDecoder().decode(secret.getData().get("keystore.p12"));
                suffix = ".p12";
                String password = new String(Base64.getDecoder().decode(secret.getData().get("password")));
                args.add("-s");
                args.add(password);
            } else {
                cert = new String(Base64.getDecoder().decode(secret.getData().get("tls.crt"))).getBytes(StandardCharsets.UTF_8);
                suffix = ".pem";
            }
            Path certPath = Files.createTempFile("clitrust", suffix, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
            Files.write(certPath, cert);
            args.add("-t");
            args.add(certPath.toString());
            args.add("--hostname-verifier");
            args.add(".*");
        } else {
            connection.append("http://");
        }
        if (endpointSecretName != null) {
            Secret secret = Kube.getSecret(client, namespace, endpointSecretName);
            Map<String, String> credentials = Kube.decodeOpaqueSecrets(secret);
            if (username == null) {
                if (credentials.size() != 1) {
                    throw Messages.MSG.usernameRequired();
                } else {
                    Map.Entry<String, String> entry = credentials.entrySet().iterator().next();
                    connection.append(entry.getKey());
                    connection.append(':');
                    connection.append(entry.getValue());
                    connection.append('@');
                }
            } else {
                connection.append(username);
                if (credentials.containsKey(username)) {
                    connection.append(':');
                    connection.append(credentials.get(username));
                }
                connection.append('@');
            }
        }
        InetAddress localAddress = portForward.getLocalAddress();
        if (localAddress.getAddress().length == 4) {
            connection.append(localAddress.getHostAddress());
        } else {
            connection.append('[').append(localAddress.getHostAddress()).append(']');
        }
        connection.append(':');
        connection.append(portForward.getLocalPort());
        args.add("-c");
        args.add(connection.toString());
        Messages.CLI.debugf("cli %s", args);
        CLI.main(new DefaultShell(), args.toArray(new String[0]), System.getProperties(), false);
        return CommandResult.SUCCESS;
    } catch (Throwable t) {
        TerminalString error = new TerminalString(Util.getRootCause(t).getLocalizedMessage(), new TerminalColor(Color.RED, Color.DEFAULT, Color.Intensity.BRIGHT));
        invocation.getShell().writeln(error.toString());
        return CommandResult.FAILURE;
    }
}
Also used : CommandDefinition(org.aesh.command.CommandDefinition) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) LocalPortForward(io.fabric8.kubernetes.client.LocalPortForward) Argument(org.aesh.command.option.Argument) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) Map(java.util.Map) CommandResult(org.aesh.command.CommandResult) Path(java.nio.file.Path) DEFAULT_CLUSTER_NAME(org.infinispan.cli.commands.kubernetes.Kube.DEFAULT_CLUSTER_NAME) Option(org.aesh.command.option.Option) ContextAwareCommandInvocation(org.infinispan.cli.impl.ContextAwareCommandInvocation) Files(java.nio.file.Files) Messages(org.infinispan.cli.logging.Messages) Util(org.infinispan.commons.util.Util) Color(org.aesh.readline.terminal.formatting.Color) Pod(io.fabric8.kubernetes.api.model.Pod) CliCommand(org.infinispan.cli.commands.CliCommand) StandardCharsets(java.nio.charset.StandardCharsets) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Base64(java.util.Base64) List(java.util.List) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) CLI(org.infinispan.cli.commands.CLI) KubernetesContext(org.infinispan.cli.impl.KubernetesContext) INFINISPAN_CLUSTER_CRD(org.infinispan.cli.commands.kubernetes.Kube.INFINISPAN_CLUSTER_CRD) DefaultShell(org.infinispan.cli.impl.DefaultShell) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Secret(io.fabric8.kubernetes.api.model.Secret) Path(java.nio.file.Path) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) LocalPortForward(io.fabric8.kubernetes.client.LocalPortForward) Pod(io.fabric8.kubernetes.api.model.Pod) ArrayList(java.util.ArrayList) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) Secret(io.fabric8.kubernetes.api.model.Secret) DefaultShell(org.infinispan.cli.impl.DefaultShell) TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Map(java.util.Map) InetAddress(java.net.InetAddress)

Example 3 with TerminalColor

use of org.aesh.readline.terminal.formatting.TerminalColor in project infinispan by infinispan.

the class RestCliCommand method exec.

@Override
protected final CommandResult exec(ContextAwareCommandInvocation invocation) throws CommandException {
    Shell shell = invocation.getShell();
    Context context = invocation.getContext();
    try {
        String response = context.getConnection().execute((c, r) -> {
            try {
                return exec(invocation, c, r);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, getResponseMode());
        if (response != null && !response.isEmpty()) {
            shell.writeln(response);
        }
        invocation.getContext().refreshPrompt();
        return CommandResult.SUCCESS;
    } catch (Exception e) {
        TerminalString error = new TerminalString(Util.getRootCause(e).getLocalizedMessage(), new TerminalColor(Color.RED, Color.DEFAULT, Color.Intensity.BRIGHT));
        shell.writeln(error.toString());
        invocation.getContext().refreshPrompt();
        return CommandResult.FAILURE;
    }
}
Also used : Context(org.infinispan.cli.Context) Shell(org.aesh.command.shell.Shell) TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) CommandException(org.aesh.command.CommandException)

Aggregations

TerminalColor (org.aesh.readline.terminal.formatting.TerminalColor)3 TerminalString (org.aesh.readline.terminal.formatting.TerminalString)2 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)1 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 Secret (io.fabric8.kubernetes.api.model.Secret)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 LocalPortForward (io.fabric8.kubernetes.client.LocalPortForward)1 InetAddress (java.net.InetAddress)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 PosixFilePermissions (java.nio.file.attribute.PosixFilePermissions)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 List (java.util.List)1 Map (java.util.Map)1 CommandDefinition (org.aesh.command.CommandDefinition)1 CommandException (org.aesh.command.CommandException)1 CommandResult (org.aesh.command.CommandResult)1