package org.opencb.commons.utils;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.opencb.commons.exec.Command;

import java.util.List;

public class DockerUtils {

    /**
     * Create and run the command line to execute the docker image.
     *
     * @param image Docker image name
     * @param user  Array of two strings: the first string, the user; the second, the group
     * @param bindings Array of bind mounts for docker volumes: source-target pairs
     * @param params Image command parameter
     * @return The command line
     */
    public static String run(String image, String[] user, List<Pair<String, String>> bindings, String params) {
        // Docker run
        StringBuilder commandLine = new StringBuilder("docker run ");

        // User
        if (ArrayUtils.isNotEmpty(user)) {
            String u = user[0];
            if (user.length > 1) {
                u += (":" + user[1]);
            }
            commandLine.append("-u ").append(u).append(" ");
        }

        // Mount management (bindings)
        for (Pair<String, String> binding : bindings) {
            commandLine.append("--mount type=bind,source=\"").append(binding.getLeft()).append("\",target=\"").append(binding.getRight())
                    .append("\" ");

        }

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

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

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

        return commandLine.toString();
    }
}

@@ -31,38 +31,38 @@
 ls;%0A%0Aimport org.
-apache
+opencb
 .commons.lang3.A
@@ -58,68 +58,77 @@
 ons.
-lang3.ArrayUtils
+exec.Command
 ;%0A
+%0A
 import 
-org.apache.commons.lang3.tup
+java.io.IOException;%0Aimport java.nio.fi
 le.Pa
-ir
+ths
 ;%0Aim
@@ -136,41 +136,30 @@
 ort 
-org.opencb.commons.exec.Command;%0A
+java.util.AbstractMap;
 %0Aimp
@@ -177,16 +177,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Apublic
@@ -368,119 +368,113 @@
 ram 
-user  Array of two strings: the first string, the user; the second, the group%0A     * @param bindings Array of b
+inputBindings Array of bind mounts for docker input volumes (source-target)%0A     * @param outputBinding B
 ind 
@@ -470,33 +470,32 @@
 nding Bind mount
-s
  for docker volu
@@ -494,17 +494,23 @@
 ker 
+output 
 volume
-s:
  
+(
 sour
@@ -522,14 +522,9 @@
 rget
- pairs
+)
 %0A   
@@ -534,17 +534,20 @@
  @param 
-p
+cmdP
 arams Im
@@ -571,308 +571,658 @@
 eter
-%0A     * @return The command line%0A     */%0A    public static String run(String image, String%5B%5D user, List%3CPair%3CString, String%3E%3E bindings, String params) %7B%0A        // Docker run%0A        StringBuilder commandLine = new StringBuilder(%22docker run %22);%0A%0A        // User%0A        if (ArrayUtils.isNotEmpt
+s%0A     * @param dockerParams Docker parameters%0A     * @return The command 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        // Docker run%0A        StringBuilder commandLine = new StringBuilder(%22docker run %22);%0A%0A        // Docker params%0A        boolean setUser = true;%0A        if (dockerParams != null) %7B%0A            if (dockerParams.containsKe
 y(
+%22
 user
+%22
 )) %7B
@@ -1238,114 +1238,442 @@
     
+   
-String u = user%5B0%5D;%0A            if (user.length %3E 1) %7B%0A                u += (%22:%22 + user%5B1%5D);%0A            %7D
+ setUser = false;%0A            %7D%0A            for (String key : dockerParams.keySet()) %7B%0A                commandLine.append(%22--%22).append(key).append(%22 %22).append(dockerParams.get(key)).append(%22 %22);%0A            %7D%0A        %7D%0A%0A        if (setUser) %7B%0A            // User: array of two strings, the first string, the user; the second, the group%0A            String%5B%5D user = FileUtils.getUserAndGroup(Paths.get(outputBinding.getKey()), true);
 %0A   
@@ -1702,17 +1702,21 @@
 ppend(%22-
-u
+-user
  %22).appe
@@ -1719,16 +1719,50 @@
 append(u
+ser%5B0%5D).append(%22:%22).append(user%5B1%5D
 ).append
@@ -1835,12 +1835,31 @@
 or (
-Pair
+AbstractMap.SimpleEntry
 %3CStr
@@ -1881,17 +1881,22 @@
 nding : 
-b
+inputB
 indings)
@@ -1982,12 +1982,11 @@
 .get
-Left
+Key
 ()).
@@ -2030,13 +2030,13 @@
 .get
-Right
+Value
 ())%0A
@@ -2067,25 +2067,24 @@
 end(%22%5C%22 %22);%0A
-%0A
         %7D%0A%0A 
@@ -2072,32 +2072,209 @@
 %5C%22 %22);%0A        %7D
+%0A        commandLine.append(%22--mount type=bind,source=%5C%22%22).append(outputBinding.getKey()).append(%22%5C%22,target=%5C%22%22)%0A                .append(outputBinding.getValue()).append(%22%5C%22 %22);
 %0A%0A        // Doc
@@ -2402,17 +2402,20 @@
 .append(
-p
+cmdP
 arams);%0A

update DockerUtils.run to get user/group IDs from output directory, #45