/*
 * Copyright (c) 2014 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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 com.spotify.docker;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.exceptions.ImageNotFoundException;
import com.spotify.docker.client.messages.Image;
import com.spotify.docker.client.messages.RemovedImage;
import com.spotify.docker.client.shaded.javax.ws.rs.NotFoundException;

import java.util.Collections;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.spotify.docker.Utils.parseImageName;

/**
 * Removes a docker image.
 */
@Mojo(name = "removeImage")
public class RemoveImageMojo extends AbstractDockerMojo {

  /**
   * Name of image to remove.
   */
  @Parameter(property = "imageName", required = true)
  private String imageName;

  /**
   * Additional tags to remove.
   */
  @Parameter(property = "dockerImageTags")
  private List<String> imageTags;

  /**
   * Additional tags to tag the image with.
   */
  @Parameter(property = "removeAllTags", defaultValue = "false")
  private boolean removeAllTags;

  @Override
  protected void execute(final DockerClient docker)
      throws MojoExecutionException, DockerException, IOException, InterruptedException {
    final String[] imageNameParts = parseImageName(imageName);
    if (imageTags == null) {
      imageTags = Collections.singletonList("");
    } else if (removeAllTags) {
        getLog().info("Removal of all tags requested, searching for tags");
        // removal of all tags requested, loop over all images to find tags
        for (final Image currImage : docker.listImages()) {
            getLog().debug("Found image: " + currImage.toString());
            String[] parsedRepoTag;
            for (final String repoTag : currImage.repoTags()) {
                parsedRepoTag = parseImageName(repoTag);
                // if repo name matches imageName then save the tag for deletion
                if (parsedRepoTag[0].equals(imageNameParts)) {
                    imageTags.add(parsedRepoTag[1]);
                    getLog().info("Adding tag for removal: " + parsedRepoTag[1]);
                }
            }
        }
    }
    imageTags.add(imageNameParts[1]);

    for (final String imageTag : imageTags) {
      final String currImageName = imageNameParts[0] +
                             ((isNullOrEmpty(imageTag)) ? "" : (":" + imageTag));
      getLog().info("Removing -f " + currImageName);

      try {
        // force the image to be removed but don't remove untagged parents
        for (final RemovedImage removedImage : docker.removeImage(currImageName, true, false)) {
          getLog().info("Removed: " + removedImage.imageId());
        }
      } catch (ImageNotFoundException | NotFoundException e) {
        // ignoring 404 errors only
        getLog().warn("Image " + imageName + " doesn't exist and cannot be deleted - ignoring");
      }
    }
  }
}

@@ -864,24 +864,140 @@
 fy.docker;%0A%0A
+import static com.google.common.base.Strings.isNullOrEmpty;%0Aimport static com.spotify.docker.Utils.parseImageName;%0A%0A
 import com.s
@@ -1340,37 +1340,85 @@
 on;%0A
-%0Aimport java.util.Collections
+import java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List
 ;%0Aim
@@ -1580,204 +1580,8 @@
 r;%0A%0A
-import java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport static com.google.common.base.Strings.isNullOrEmpty;%0Aimport static com.spotify.docker.Utils.parseImageName;%0A%0A
 /**%0A
@@ -2221,21 +2221,8 @@
 ion,
- IOException,
  Int
@@ -2355,36 +2355,65 @@
 s = 
-Collections.singletonList(%22%22
+new ArrayList%3C%3E(1);%0A      imageTags.add(imageNameParts%5B1%5D
 );%0A 
@@ -2445,26 +2445,24 @@
 gs) %7B%0A      
- 
- 
 getLog().inf
@@ -2513,26 +2513,24 @@
 for tags%22);%0A
-  
       // rem
@@ -2589,34 +2589,32 @@
 find tags%0A      
- 
- 
 for (final Image
@@ -2645,28 +2645,24 @@
 Images()) %7B%0A
-    
         getL
@@ -2709,28 +2709,24 @@
 oString());%0A
-    
         Stri
@@ -2749,28 +2749,24 @@
 ag;%0A        
-    
 for (final S
@@ -2815,22 +2815,16 @@
         
-     
- 
 parsedRe
@@ -2856,22 +2856,16 @@
 poTag);%0A
-      
         
@@ -2941,22 +2941,16 @@
         
-  
-    
 if (pars
@@ -2983,16 +2983,19 @@
 ameParts
+%5B0%5D
 )) %7B%0A   
@@ -3003,24 +3003,16 @@
         
- 
-       
 imageTag
@@ -3032,32 +3032,24 @@
 epoTag%5B1%5D);%0A
-        
             
@@ -3114,28 +3114,8 @@
 %5D);%0A
-                %7D%0A  
     
@@ -3124,32 +3124,40 @@
     %7D%0A        %7D%0A
+      %7D%0A
     %7D%0A    imageT
@@ -3265,16 +3265,26 @@
 geName =
+%0A         
  imageNa
@@ -3299,37 +3299,8 @@
 0%5D +
-%0A                            
  ((i

Fix code formatting