package com.walmart.otto.tools;

import com.walmart.otto.configurator.Configurator;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class ProcessExecutor {
    private Configurator configurator;

    public ProcessExecutor(Configurator configurator) {
        this.configurator = configurator;
    }
    // skip: ' - [113/113 files][  2.1 MiB/  2.1 MiB] 100% Done '
    private static Pattern uploadProgress = Pattern.compile(".*\\[\\d*/\\d* files]\\[.*% Done.*");

    public void executeCommand(final String[] commands, List<String> inputStream, List<String> errorStream) {
        Boolean isDebug = configurator.isDebug();
        String debugString = "";
        try {
            if (isDebug) {
                StringBuilder command = new StringBuilder();
                command.append("\u001B[32m"); // green
                for (String cmd : commands) {
                    command.append(cmd).append(" ");
                }
                command.append("\u001B[0m");
                debugString = command.toString();
                System.out.println("$ " + debugString);
            }
            new ProcessBuilder(commands, inputStream, errorStream);
            if (isDebug) {
                List<String> cleanErrorStream = new ArrayList<>();

                for (String line : errorStream) {
                    if (!uploadProgress.matcher(line).matches()) {
                        cleanErrorStream.add(line);
                    }
                }

                printStreams(inputStream, cleanErrorStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void printStreams(List<String> inputStream, List<String> errorStream) {
        for (String line : inputStream) {
            System.out.println(line);
        }

        for (String line : errorStream) {
            System.out.println(line);
        }
    }
}

@@ -667,41 +667,8 @@
 ();%0A
-        String debugString = %22%22;%0A
     
@@ -986,58 +986,8 @@
 %22);%0A
-                debugString = command.toString();%0A
     
@@ -1028,19 +1028,26 @@
 %22 + 
-debug
+command.to
 String
+()
 );%0A 

Remove debugString