Search in sources :

Example 1 with ProtectionType

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;
}
Also used : HashSet(java.util.HashSet) Session(org.apache.karaf.shell.api.console.Session) ProtectionType(org.jboss.fuse.credential.store.karaf.util.ProtectionType) Arrays(java.util.Arrays) List(java.util.List) Completer(org.apache.karaf.shell.api.console.Completer) ListIterator(java.util.ListIterator) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Set(java.util.Set) CommandLine(org.apache.karaf.shell.api.console.CommandLine) ProtectionType(org.jboss.fuse.credential.store.karaf.util.ProtectionType) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter)

Aggregations

Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Set (java.util.Set)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 CommandLine (org.apache.karaf.shell.api.console.CommandLine)1 Completer (org.apache.karaf.shell.api.console.Completer)1 Session (org.apache.karaf.shell.api.console.Session)1 StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)1 ProtectionType (org.jboss.fuse.credential.store.karaf.util.ProtectionType)1