/**
 * 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) {
         PageSet<? extends StorageMetadata> blobStoreMetadatas = blobStore.list(containerName,
                  ListContainerOptions.Builder.recursive());

         if (blobStoreMetadatas == null || !blobStoreMetadatas.isEmpty()) {
            for (StorageMetadata blobMetadata : blobStoreMetadatas) {
               String blobName = blobMetadata.getName();
               cacheProvider.getProviderCacheForType("blob").put(blobMetadata.getProviderId(), blobName);
               System.out.println(String.format(LISTFORMAT, containerName, blobName));
               containerName = "";
            }
            System.out.println(String.format(LISTFORMAT, "", ""));
         } else {
            System.out.println(String.format(LISTFORMAT, containerName, "<empty>"));
            System.out.println(String.format(LISTFORMAT, "", ""));
         }
      }
      return null;
   }
}
@@ -2739,88 +2739,56 @@
     
-PageSet%3C? extends StorageMetadata%3E blobStoreMetadatas = blobStore.list(c
+boolean firstBlob = true;%0A         ListC
 ontainer
 Name
@@ -2787,31 +2787,25 @@
 iner
-Name,%0A                 
+Options options =
  Lis
@@ -2841,17 +2841,16 @@
 ursive()
-)
 ;%0A%0A     
@@ -2857,20 +2857,59 @@
     
-if (blob
+while (true) %7B%0A            PageSet%3C? extends 
 Stor
+ag
 eMet
@@ -2917,22 +2917,10 @@
 data
-s == null %7C%7C !
+%3E 
 blob
@@ -2933,29 +2933,59 @@
 etadatas
-.isEmpty()) %7B
+ = blobStore.list(containerName, options);%0A
 %0A       
@@ -3261,32 +3261,44 @@
 rmat(LISTFORMAT,
+ firstBlob ?
  containerName, 
@@ -3295,16 +3295,21 @@
 inerName
+ : %22%22
 , blobNa
@@ -3333,26 +3333,25 @@
     
-containerName = %22%22
+firstBlob = false
 ;%0A  
@@ -3358,24 +3358,25 @@
           %7D%0A
+%0A
             
@@ -3380,77 +3380,122 @@
    S
-ystem.out.println(String.format(LISTFORMAT, %22%22, %22%22));%0A         %7D else
+tring marker = blobStoreMetadatas.getNextMarker();%0A            if (marker == null) %7B%0A               if (firstBlob)
  %7B%0A 
@@ -3497,32 +3497,38 @@
 ) %7B%0A            
+      
 System.out.print
@@ -3583,24 +3583,138 @@
 pty%3E%22));%0A   
+            %7D%0A               break;%0A            %7D%0A%0A            options = options.afterMarker(marker);%0A         %7D%0A%0A
          Sys
@@ -3765,27 +3765,16 @@
 , %22%22));%0A
-         %7D%0A
       %7D%0A
@@ -3798,8 +3798,9 @@
 ;%0A   %7D%0A%7D
+%0A

Use marker when listing a container