/**
 * Licensed to jclouds, Inc. (jclouds) under one or more
 * contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  jclouds licenses this file
 * to you 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.jclouds.karaf.commands.blobstore;

import java.util.Collection;

import com.google.common.collect.Lists;

import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.gogo.commands.Option;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.options.ListContainerOptions;

/**
 * @author: iocanel
 */
@Command(scope = "jclouds", name = "blobstore-list", description = "Lists all containers")
public class BlobListCommand extends BlobStoreCommandWithOptions {

   @Argument(index = 0, name = "containerNames", description = "The name of the container", required = false, multiValued = true)
   final Collection<String> containerNames = Lists.newArrayList();

   @Option(name = "-a", aliases = "--all", description = "List all containers", required = false)
   boolean listAllContainers = false;

   private static final String LISTFORMAT = "%-40s %-40s";

   @Override
   protected Object doExecute() throws Exception {
      BlobStore blobStore = null;
      try {
         blobStore = getBlobStore();
      } catch (Throwable t) {
         System.err.println(t.getMessage());
         return null;
      }
      System.out.println(String.format(LISTFORMAT, "[Container]", "[Blob]"));

      if (listAllContainers) {
         containerNames.clear();
         for (StorageMetadata containerMetadata : blobStore.list()) {
            String containerName = containerMetadata.getName();
            containerNames.add(containerName);
            cacheProvider.getProviderCacheForType("container").put(containerMetadata.getProviderId(), containerName);
         }
      } else if (containerNames.isEmpty()) {
         throw new IllegalArgumentException("Must specify container names or --all");
      }

      for (String containerName : containerNames) {
         boolean firstBlob = true;
         ListContainerOptions options = ListContainerOptions.Builder.recursive();

         while (true) {
            PageSet<? extends StorageMetadata> blobStoreMetadatas = blobStore.list(containerName, options);

            for (StorageMetadata blobMetadata : blobStoreMetadatas) {
               String blobName = blobMetadata.getName();
               cacheProvider.getProviderCacheForType("blob").put(blobMetadata.getProviderId(), blobName);
               System.out.println(String.format(LISTFORMAT, firstBlob ? containerName : "", blobName));
               firstBlob = false;
            }

            String marker = blobStoreMetadatas.getNextMarker();
            if (marker == null) {
               if (firstBlob) {
                  System.out.println(String.format(LISTFORMAT, containerName, "<empty>"));
               }
               break;
            }

            options = options.afterMarker(marker);
         }

         System.out.println(String.format(LISTFORMAT, "", ""));
      }
      return null;
   }
}

@@ -837,16 +837,44 @@
 store;%0A%0A
+import java.io.PrintStream;%0A
 import j
@@ -1825,41 +1825,36 @@
 nal 
-String LISTFORMAT = %22%25-40s %25-40s%22
+PrintStream out = System.out
 ;%0A%0A 
@@ -2107,86 +2107,8 @@
    %7D
-%0A      System.out.println(String.format(LISTFORMAT, %22%5BContainer%5D%22, %22%5BBlob%5D%22));
 %0A%0A  
@@ -2684,33 +2684,66 @@
     
-boolean firstBlob = true;
+out.println(containerName + %22:%22);%0A         out.println();%0A
 %0A   
@@ -3195,39 +3195,32 @@
 %0A               
-System.
 out.println(Stri
@@ -3219,110 +3219,26 @@
 tln(
-String.format(LISTFORMAT, firstBlob ? containerName : %22%22, blobName));%0A               firstBlob = false
+%22    %22 + blobName)
 ;%0A  
@@ -3352,148 +3352,8 @@
 ) %7B%0A
-               if (firstBlob) %7B%0A                  System.out.println(String.format(LISTFORMAT, containerName, %22%3Cempty%3E%22));%0A               %7D%0A
     
@@ -3457,23 +3457,16 @@
         
-System.
 out.prin
@@ -3473,41 +3473,8 @@
 tln(
-String.format(LISTFORMAT, %22%22, %22%22)
 );%0A 

Simplify list container output