package io.fabric8.maven.docker;

import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.service.ServiceHub;

/**
 * Goal for pushing a data-docker container
 *
 * @author roland
 *
 */
@Mojo(name = "push", defaultPhase = LifecyclePhase.DEPLOY)
public class PushMojo extends AbstractDockerMojo {

    // Registry to use for push operations if no registry is specified
    @Parameter(property = "docker.push.registry")
    private String pushRegistry;

    @Parameter(property="docker.skipPush", defaultValue="false")
    private boolean skipPush;

    /** {@inheritDoc} */
    @Override
    public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
        for (ImageConfiguration imageConfig : getResolvedImages()) {
            BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
            String name = imageConfig.getName();
            if (buildConfig != null) {
                String configuredRegistry = getConfiguredRegistry(imageConfig, pushRegistry);
                AuthConfig authConfig = prepareAuthConfig(new ImageName(name), configuredRegistry, true);

                DockerAccess docker = hub.getDockerAccess();

                long start = System.currentTimeMillis();
                docker.pushImage(name, authConfig, configuredRegistry);
                log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));

                for (String tag : imageConfig.getBuildConfiguration().getTags()) {
                    if (tag != null) {
                        docker.pushImage(new ImageName(name,tag).getFullName(), authConfig, configuredRegistry);
                    }
                }
            }
        }
    }
}

@@ -1255,16 +1255,45 @@
 ption %7B%0A
+        if (!skipPush) %7B%0A    
         
@@ -1349,24 +1349,28 @@
 Images()) %7B%0A
+    
             
@@ -1452,24 +1452,28 @@
             
+    
 String name 
@@ -1505,24 +1505,28 @@
             
+  
+  
 if (buildCon
@@ -1532,32 +1532,36 @@
 nfig != null) %7B%0A
+    
                 
@@ -1654,16 +1654,20 @@
         
+ 
+   
 AuthConf
@@ -1765,16 +1765,20 @@
         
+   
+ 
 DockerAc
@@ -1831,16 +1831,20 @@
         
+    
 long sta
@@ -1884,32 +1884,36 @@
                 
+  
+  
 docker.pushImage
@@ -1944,32 +1944,36 @@
 guredRegistry);%0A
+    
                 
@@ -2055,24 +2055,28 @@
             
+    
 for (String 
@@ -2146,24 +2146,28 @@
             
+   
+ 
 if (tag != n
@@ -2165,32 +2165,36 @@
 (tag != null) %7B%0A
+    
                 
@@ -2237,16 +2237,17 @@
 me(name,
+ 
 tag).get
@@ -2283,32 +2283,58 @@
 guredRegistry);%0A
+                        %7D%0A
                 

allow skip push via property docker.skipPush