Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNMGMT-42: Autocomplete operation names for mgmt exec command.
Browse files Browse the repository at this point in the history
  • Loading branch information
nscavell committed Apr 1, 2013
1 parent 4490147 commit 7b1bfdb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 22 deletions.
Expand Up @@ -22,7 +22,6 @@

package org.gatein.management.cli.crash.arguments;

import org.crsh.cmdline.ParameterDescriptor;
import org.crsh.cmdline.annotations.Man;
import org.crsh.cmdline.annotations.Option;
import org.crsh.cmdline.annotations.Usage;
Expand All @@ -31,10 +30,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -47,27 +43,13 @@
@Man("The import mode for an import operation. Valid values are: conserve, insert, merge, and overwrite.")
public @interface ImportModeOption
{
public static class ImportModeCompleter implements Completer
public static class ImportModeCompleter extends StringCollectionCompleter implements Completer
{
public static final Set<String> modes = new HashSet<String>(Arrays.asList("conserve", "insert", "merge", "overwrite"));

@Override
public Map<String, Boolean> complete(ParameterDescriptor<?> parameter, String prefix) throws Exception
public ImportModeCompleter()
{
Map<String, Boolean> completions = new HashMap<String, Boolean>(modes.size());
for (String mode : modes)
{
if ("".equals(prefix))
{
completions.put(mode, true);
}
else if (mode.startsWith(prefix))
{
completions.put(mode.substring(prefix.length()), true);
}
}

return completions;
super(modes);
}
}
}
Expand Up @@ -25,18 +25,43 @@
import org.crsh.cmdline.annotations.Man;
import org.crsh.cmdline.annotations.Option;
import org.crsh.cmdline.annotations.Usage;
import org.crsh.cmdline.spi.Completer;
import org.gatein.management.api.operation.OperationNames;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a>
* @version $Revision$
*/
@Retention(RetentionPolicy.RUNTIME)
@Option(names = {"o", "operation"})
@Option(names = {"o", "operation"}, completer = OperationOption.OperationOptionCompleter.class)
@Usage("Operation name")
@Man("Specifies the name of the operation of the management request.")
public @interface OperationOption
{
public static class OperationOptionCompleter extends StringCollectionCompleter implements Completer
{
public OperationOptionCompleter()
{
super(operationNames);
}

private static final Set<String> operationNames;
static
{
Set<String> names = new LinkedHashSet<String>(4);

// Just add the CRUD operations for now
names.add(OperationNames.READ_RESOURCE);
names.add(OperationNames.UPDATE_RESOURCE);
names.add(OperationNames.ADD_RESOURCE);
names.add(OperationNames.REMOVE_RESOURCE);

operationNames = names;
}
}
}
@@ -0,0 +1,60 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.gatein.management.cli.crash.arguments;

import org.crsh.cmdline.ParameterDescriptor;
import org.crsh.cmdline.spi.Completer;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a>
*/
public class StringCollectionCompleter implements Completer
{
private final Collection<String> collection;

public StringCollectionCompleter(Collection<String> collection)
{
this.collection = collection;
}

@Override
public Map<String, Boolean> complete(ParameterDescriptor<?> parameter, String prefix) throws Exception
{
Map<String, Boolean> completions = Collections.emptyMap();
for (String value : collection) {
if (value.startsWith(prefix)) {
if (completions.isEmpty()) {
completions = new LinkedHashMap<String, Boolean>();
}
completions.put(value.substring(prefix.length()), true);
}
}

return completions;
}
}

0 comments on commit 7b1bfdb

Please sign in to comment.