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;
}
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());
}
}
}
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);
}
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;
}
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;
}
}
Aggregations