package org.opencb.commons.utils;

import org.opencb.commons.exec.Command;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;

public class DockerUtils {

    /**
     * Create and run the command line to execute the docker image.
     *
     * @param image Docker image name
     * @param inputBindings Array of bind mounts for docker input volumes (source-target)
     * @param outputBinding Bind mount for docker output volume (source-target)
     * @param cmdParams Image command parameters
     * @param dockerParams Docker parameters
     * @return The command line
     * @throws IOException IO exception
     */
    public static String run(String image, List<AbstractMap.SimpleEntry<String, String>> inputBindings,
                             AbstractMap.SimpleEntry<String, String> outputBinding, String cmdParams,
                             Map<String, String> dockerParams) throws IOException {
        // Sanity check
        if (outputBinding == null) {
           throw new IOException("Missing output binding");
        }

        // Docker run
        StringBuilder commandLine = new StringBuilder("docker run --rm ");

        // Docker params
        boolean setUser = true;
        if (dockerParams != null) {
            if (dockerParams.containsKey("user")) {
                setUser = false;
            }
            for (String key : dockerParams.keySet()) {
                commandLine.append("--").append(key).append(" ").append(dockerParams.get(key)).append(" ");
            }
        }

        if (setUser) {
            // User: array of two strings, the first string, the user; the second, the group
            String[] user = FileUtils.getUserAndGroup(Paths.get(outputBinding.getKey()), true);
            commandLine.append("--user ").append(user[0]).append(":").append(user[1]).append(" ");
        }

        if (inputBindings != null) {
            // Mount management (bindings)
            for (AbstractMap.SimpleEntry<String, String> binding : inputBindings) {
                commandLine.append("--mount type=bind,source=\"").append(binding.getKey()).append("\",target=\"").append(binding.getValue())
                        .append("\" ");
            }
        }
        commandLine.append("--mount type=bind,source=\"").append(outputBinding.getKey()).append("\",target=\"")
                .append(outputBinding.getValue()).append("\" ");

        // Docker image and version
        commandLine.append(image).append(" ");

        // Image command params
        commandLine.append(cmdParams);

        // Execute command
        Command cmd = new Command(commandLine.toString());
        cmd.run();

        // Debugger
        System.out.println("Docker command line:\n" + commandLine);

        return commandLine.toString();
    }
}

@@ -67,16 +67,73 @@
 Command;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -286,16 +286,97 @@
 tils %7B%0A%0A
+%0A    private static Logger logger = LoggerFactory.getLogger(DockerUtils.class);%0A%0A
     /**%0A
@@ -393,16 +393,8 @@
 ate 
-and run 
 the 
@@ -849,19 +849,32 @@
  String 
-run
+buildCommandLine
 (String 
@@ -970,16 +970,36 @@
         
+                    
 Abstract
@@ -1092,16 +1092,36 @@
         
+                    
 Map%3CStri
@@ -1239,16 +1239,17 @@
         
+ 
 throw ne
@@ -1251,17 +1251,30 @@
 ow new I
-O
+llegalArgument
 Exceptio
@@ -2819,241 +2819,1229 @@
 s);%0A
+        return commandLine.toString();
 %0A    
+%7D%0A%0A
     /
-/ Execute command
+**%0A     * Create and run the command line to execute the docker image.
 %0A     
+*%0A
    
-Command cmd = new Command(
+  * @param image Docker image name%0A     * @param inputBindings Array of bind mounts for docker input volumes (source-target)%0A     * @param outputBinding Bind mount for docker output volume (source-target)%0A     * @param cmdParams Image command parameters%0A     * @param dockerParams Docker parameters%0A     * @return The 
 command
-Line.toString());%0A        cmd.run();%0A%0A        // Debugger%0A        System.out.println(%22Docker command line:%5Cn%22 + commandLine);%0A%0A        return commandLine.toString()
+ line%0A     * @throws IOException IO exception%0A     */%0A    public static String run(String image, List%3CAbstractMap.SimpleEntry%3CString, String%3E%3E inputBindings,%0A                             AbstractMap.SimpleEntry%3CString, String%3E outputBinding, String cmdParams,%0A                             Map%3CString, String%3E dockerParams) throws IOException %7B%0A        String commandLine = buildCommandLine(image, inputBindings, outputBinding, cmdParams, dockerParams);%0A%0A        logger.info(%22Run docker command line%22);%0A        logger.info(%22============================%22);%0A        logger.info(commandLine);%0A        logger.info(%22============================%22);%0A%0A        // Execute command%0A        Command cmd = new Command(commandLine);%0A        cmd.run();%0A%0A        return commandLine
 ;%0A  

Replace System.out with logger.info