Search in sources :

Example 1 with Context

use of org.infinispan.cli.Context in project infinispan by infinispan.

the class Config method exec.

@Override
public CommandResult exec(ContextAwareCommandInvocation invocation) {
    Context context = invocation.getContext();
    context.getProperties().forEach((k, v) -> invocation.printf("%s=%s\n", k, v));
    return CommandResult.SUCCESS;
}
Also used : Context(org.infinispan.cli.Context)

Example 2 with Context

use of org.infinispan.cli.Context in project infinispan by infinispan.

the class CdContextCompleter method complete.

@Override
public void complete(CompleterInvocation invocation) {
    Context context = ((ContextAwareCompleterInvocation) invocation).context;
    Resource resource = context.getConnection().getActiveResource();
    String v = invocation.getGivenCompleteValue();
    if (v == null || v.length() == 0) {
        // no completions yet, add all of the local resource children
        invocation.addAllCompleterValues(getChildrenNames(resource));
        invocation.setAppendSpace(resource.isLeaf());
    } else {
        String[] parts = v.split("/");
        if (parts.length == 0) {
            resource = resource.findAncestor(RootResource.class);
            invocation.addAllCompleterValues(getChildrenNames(resource));
            invocation.setAppendSpace(resource.isLeaf());
        } else {
            int offset;
            String last;
            String prefix;
            if (v.endsWith("/")) {
                offset = 0;
                last = "";
                prefix = v;
            } else {
                offset = 1;
                last = parts[parts.length - 1];
                int lastSlash = v.lastIndexOf('/');
                prefix = lastSlash < 0 ? "" : v.substring(0, lastSlash + 1);
            }
            for (int i = 0; i < parts.length - offset; i++) {
                if (parts[i].isEmpty()) {
                    resource = resource.findAncestor(RootResource.class);
                } else {
                    try {
                        resource = resource.getChild(parts[i]);
                    } catch (IOException e) {
                    // Ignore
                    }
                }
            }
            Iterable<String> all = getChildrenNames(resource);
            for (String item : all) {
                if (item.startsWith(last)) {
                    invocation.addCompleterValue(prefix + item);
                }
            }
            invocation.setAppendSpace(resource.isLeaf());
        }
    }
}
Also used : Context(org.infinispan.cli.Context) RootResource(org.infinispan.cli.resources.RootResource) Resource(org.infinispan.cli.resources.Resource) RootResource(org.infinispan.cli.resources.RootResource) IOException(java.io.IOException)

Example 3 with Context

use of org.infinispan.cli.Context in project infinispan by infinispan.

the class SiteCompleter method getAvailableItems.

@Override
protected Collection<String> getAvailableItems(ContextAwareCompleterInvocation invocation) throws IOException {
    Context context = invocation.context;
    Command<?> cmd = invocation.getCommand();
    Connection connection = context.getConnection();
    Optional<String> cacheName = getCacheName(context, cmd);
    return cacheName.map(name -> getAvailableSites(connection, name)).orElseGet(connection::getSitesView);
}
Also used : Context(org.infinispan.cli.Context) CacheCompleter.getCacheName(org.infinispan.cli.completers.CacheCompleter.getCacheName) Context(org.infinispan.cli.Context) Collection(java.util.Collection) Optional(java.util.Optional) IOException(java.io.IOException) Connection(org.infinispan.cli.connection.Connection) Collections(java.util.Collections) Command(org.aesh.command.Command) Connection(org.infinispan.cli.connection.Connection)

Example 4 with Context

use of org.infinispan.cli.Context in project infinispan by infinispan.

the class CLI method initialCommandRuntimeBuilder.

private static AeshCommandRuntimeBuilder initialCommandRuntimeBuilder(Shell shell, Properties properties, boolean kube) throws CommandRegistryException {
    AeshCommandRegistryBuilder registryBuilder = AeshCommandRegistryBuilder.builder();
    Context context;
    if (kube) {
        context = new KubernetesContext(properties);
        registryBuilder.command(Kube.class);
    } else {
        context = new ContextImpl(properties);
        registryBuilder.command(CLI.class);
    }
    AeshCommandRuntimeBuilder runtimeBuilder = AeshCommandRuntimeBuilder.builder();
    runtimeBuilder.commandActivatorProvider(new ContextAwareCommandActivatorProvider(context)).commandInvocationProvider(new ContextAwareCommandInvocationProvider(context)).commandNotFoundHandler(new CliCommandNotFoundHandler()).completerInvocationProvider(new ContextAwareCompleterInvocationProvider(context)).shell(shell).aeshContext(context).commandRegistry(registryBuilder.create());
    return runtimeBuilder;
}
Also used : KubernetesContext(org.infinispan.cli.impl.KubernetesContext) Context(org.infinispan.cli.Context) AeshCommandRegistryBuilder(org.aesh.command.impl.registry.AeshCommandRegistryBuilder) AeshCommandRuntimeBuilder(org.aesh.command.AeshCommandRuntimeBuilder) CliCommandNotFoundHandler(org.infinispan.cli.impl.CliCommandNotFoundHandler) ContextAwareCompleterInvocationProvider(org.infinispan.cli.completers.ContextAwareCompleterInvocationProvider) ContextImpl(org.infinispan.cli.impl.ContextImpl) ContextAwareCommandActivatorProvider(org.infinispan.cli.activators.ContextAwareCommandActivatorProvider) KubernetesContext(org.infinispan.cli.impl.KubernetesContext) ContextAwareCommandInvocationProvider(org.infinispan.cli.impl.ContextAwareCommandInvocationProvider)

Example 5 with Context

use of org.infinispan.cli.Context 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

Context (org.infinispan.cli.Context)5 IOException (java.io.IOException)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 AeshCommandRuntimeBuilder (org.aesh.command.AeshCommandRuntimeBuilder)1 Command (org.aesh.command.Command)1 CommandException (org.aesh.command.CommandException)1 AeshCommandRegistryBuilder (org.aesh.command.impl.registry.AeshCommandRegistryBuilder)1 Shell (org.aesh.command.shell.Shell)1 TerminalColor (org.aesh.readline.terminal.formatting.TerminalColor)1 TerminalString (org.aesh.readline.terminal.formatting.TerminalString)1 ContextAwareCommandActivatorProvider (org.infinispan.cli.activators.ContextAwareCommandActivatorProvider)1 CacheCompleter.getCacheName (org.infinispan.cli.completers.CacheCompleter.getCacheName)1 ContextAwareCompleterInvocationProvider (org.infinispan.cli.completers.ContextAwareCompleterInvocationProvider)1 Connection (org.infinispan.cli.connection.Connection)1 CliCommandNotFoundHandler (org.infinispan.cli.impl.CliCommandNotFoundHandler)1 ContextAwareCommandInvocationProvider (org.infinispan.cli.impl.ContextAwareCommandInvocationProvider)1 ContextImpl (org.infinispan.cli.impl.ContextImpl)1 KubernetesContext (org.infinispan.cli.impl.KubernetesContext)1