/*
 * 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.config.shell;

import org.apache.karaf.cellar.config.Constants;
import org.apache.karaf.cellar.core.Configurations;
import org.apache.karaf.cellar.core.Group;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

@Command(scope = "cluster", name = "config-list", description = "List the configuration PIDs assigned to a group")
public class ListCommand extends ConfigCommandSupport {

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

    @Option(name = "-m", aliases = { "--minimal" }, description = "Don't display the properties of each configuration", required = false, multiValued = false)
    boolean minimal;

    @Override
    protected Object doExecute() throws Exception {
        // check if the group exists
        Group group = groupManager.findGroupByName(groupName);
        if (group == null) {
            System.err.println("Cluster group " + groupName + " doesn't exist");
            return null;
        }

        Map<String, Properties> distributedConfigurations = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + groupName);

        if (distributedConfigurations != null && !distributedConfigurations.isEmpty()) {
            for (String pid : distributedConfigurations.keySet()) {
                System.out.println("----------------------------------------------------------------");
                System.out.println("Pid:            " + pid);
                if (!minimal) {
                    Properties properties = distributedConfigurations.get(pid);
                    if (properties != null) {
                        System.out.println("Properties:");
                        for (Enumeration e = properties.keys(); e.hasMoreElements();) {
                            Object key = e.nextElement();
                            System.out.println("   " + key + " = " + properties.get(key));
                        }
                    }
                }
            }
        } else System.err.println("No configuration PID found for cluster group " + groupName);

        return null;
    }

}

@@ -1289,16 +1289,168 @@
 pName;%0A%0A
+    @Argument(index = 1, name = %22pid%22, description = %22The configuration PID to look for%22, required = false, multiValued = false)%0A    String searchPid;%0A%0A
     @Opt
@@ -1477,17 +1477,16 @@
 ases = %7B
- 
 %22--minim
@@ -1488,17 +1488,16 @@
 minimal%22
- 
 %7D, descr
@@ -2237,24 +2237,117 @@
 keySet()) %7B%0A
+                if (searchPid == null %7C%7C (searchPid != null && searchPid.equals(pid))) %7B%0A    
             
@@ -2446,32 +2446,36 @@
                 
+    
 System.out.print
@@ -2516,24 +2516,28 @@
             
+ 
+   
 if (!minimal
@@ -2536,24 +2536,28 @@
 !minimal) %7B%0A
+    
             
@@ -2640,24 +2640,28 @@
             
+   
+ 
 if (properti
@@ -2666,32 +2666,36 @@
 ties != null) %7B%0A
+    
                 
@@ -2733,24 +2733,28 @@
 perties:%22);%0A
+    
             
@@ -2825,20 +2825,25 @@
 ments();
+ 
 ) %7B%0A
+    
         
@@ -2892,16 +2892,20 @@
 ment();%0A
+    
         
@@ -2987,16 +2987,46 @@
 (key));%0A
+                            %7D%0A
         

Add PID filtering on cluster:config-list command