use of org.jboss.fuse.credential.store.karaf.util.ProtectionType in project fuse-karaf by jboss-fuse.
the class CredentialStoreProtectionCompletionSupport method complete.
/**
* Performs completion for any {@code -k} or {@code --protection-attributes} parameters firstly by suggesting an
* option part, then providing completion for values for a specific option.
*
* @param session
* the current {@link Session}
* @param commandLine
* the pre-parsed {@link CommandLine}
* @param candidates
* a list to fill with possible completion candidates
* @return the index of the{@link CommandLine} for which the completion will be relative
*/
@Override
public int complete(final Session session, final CommandLine commandLine, final List<String> candidates) {
final String[] arguments = commandLine.getArguments();
int protectionTypeIdx = -1;
for (int i = 0; i < arguments.length; i++) {
final String argument = arguments[i];
if ("-p".equals(argument) || "--protection-type".equals(argument)) {
protectionTypeIdx = i;
break;
}
}
// do we have protection type specified
if ((protectionTypeIdx < 0) || (arguments.length <= (protectionTypeIdx + 1))) {
return -1;
}
// parse the protection type argument
final String protectionTypeString = arguments[protectionTypeIdx + 1];
final ProtectionType protectionType;
try {
protectionType = ProtectionType.valueOf(protectionTypeString);
} catch (final IllegalArgumentException e) {
return -1;
}
// these are supported options for the specified protection type, we sort them for binarySearch below
final String[] supportedOptions = Arrays.stream(protectionType.getSupportedOptions()).sorted().toArray(String[]::new);
// the user may have already chosen an option ("option=") part
final String option = optionOf(commandLine.getCursorArgument());
if (!option.isEmpty() && (Arrays.binarySearch(supportedOptions, option) >= 0)) {
// if the user has chosen the option part, provide completion on the value part
final String[] options = Arrays.stream(protectionType.getOptionValuesFor(option)).map(o -> option + "=" + o).toArray(String[]::new);
return new StringsCompleter(options).complete(session, commandLine, candidates);
}
final Set<String> usedOptions = usedOptions(arguments);
// use supported options without any used options
final int complete = new StringsCompleter(Arrays.stream(supportedOptions).filter(o -> !usedOptions.contains(o)).map(o -> o + "=").toArray(String[]::new)).complete(session, commandLine, candidates);
for (final ListIterator<String> i = candidates.listIterator(); i.hasNext(); ) {
String candidate = i.next();
if (candidate.endsWith("= ")) {
i.set(candidate.substring(0, candidate.length() - 1));
}
}
return complete;
}
Aggregations