/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.karaf.cellar.shell.group;

import org.apache.karaf.cellar.core.Group;
import org.apache.karaf.cellar.core.Node;
import org.apache.karaf.cellar.core.control.ManageGroupAction;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Command(scope = "cluster", name = "group-pick", description = "Picks a number of nodes from one cluster group and moves them into an other.")
public class GroupPickCommand extends GroupSupport {

    @Argument(index = 0, name = "sourceGroupName", description = "The source cluster group name.", required = true, multiValued = false)
    String sourceGroupName;

    @Argument(index = 1, name = "targetGroupName", description = "The destination cluster group name.", required = true, multiValued = false)
    String targetGroupName;

    @Argument(index = 2, name = "count", description = "The number of nodes to transfer.", required = false, multiValued = false)
    int count = 1;

    @Override
    protected Object doExecute() throws Exception {
        Group sourceGroup = groupManager.findGroupByName(sourceGroupName);
        if (sourceGroup != null) {
            Set<Node> groupMembers = sourceGroup.getNodes();

            for (Node node : groupMembers) {
                List<String> recipients = new LinkedList<String>();
                recipients.add(node.getId());
                doExecute(ManageGroupAction.SET, targetGroupName, sourceGroup, recipients);
            }

            doExecute(ManageGroupAction.LIST, null, null, new ArrayList(), false);

        } else System.err.println("Cannot find source group with name: " + sourceGroupName);
        return null;
    }

}

@@ -1869,24 +1869,47 @@
 etNodes();%0A%0A
+            int i = 0;%0A
             
@@ -1937,24 +1937,83 @@
 pMembers) %7B%0A
+                if (i %3E= count)%0A                    break;%0A
             
@@ -2206,16 +2206,37 @@
 ients);%0A
+                i++;%0A
         

Fix usage of the group-pick count option