package cz.xtf.core.openshift;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

import java.io.IOException;
import java.util.Arrays;

@Slf4j
public class OpenShiftBinary {
	private final String path;

	public OpenShiftBinary(String path) {
		this.path = path;
	}

	public void login(String url, String token) {
		this.execute("login", url, "--insecure-skip-tls-verify=true", "--token=" + token);
	}

	public void login(String url, String username, String password) {
		this.execute("login", url, "--insecure-skip-tls-verify=true", "-u", username, "-p", password);
	}

	public void project(String projectName) {
		this.execute("project", projectName);
	}

	public void startBuild(String buildConfig, String sourcePath) {
		this.execute("start-build", buildConfig, "--from-dir=" + sourcePath);
	}

	// Common method for any oc command call
	public void execute(String... args) {
		executeCommand(ArrayUtils.addAll(new String[]{path}, args));
	}

	// Internal
	private void executeCommand(String... args) {
		ProcessBuilder pb = new ProcessBuilder(args);

		pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
		pb.redirectError(ProcessBuilder.Redirect.INHERIT);

		int result = -1;

		try {
			result = pb.start().waitFor();
		} catch (IOException | InterruptedException e) {
			log.error("Failed while executing: " + Arrays.toString(args), e);
		}

		if(result != 0) {
			log.error("Failed while executing (code {}): {}", result, Arrays.toString(args));
		}
	}
}

@@ -59,16 +59,54 @@
 .Slf4j;%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -186,19 +186,244 @@
 ava.
-util.Arrays
+io.InputStream;%0Aimport java.io.StringWriter;%0Aimport java.util.Arrays;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future
 ;%0A%0A@
@@ -1133,20 +1133,22 @@
 %09public 
-void
+String
  execute
@@ -1168,16 +1168,23 @@
 gs) %7B%0A%09%09
+return 
 executeC
@@ -1262,20 +1262,22 @@
 private 
-void
+String
  execute
@@ -1395,23 +1395,20 @@
 edirect.
-INHERIT
+PIPE
 );%0A%09%09pb.
@@ -1424,75 +1424,341 @@
 rror
-(ProcessBuilder.Redirect.INHERIT);%0A%0A%09%09int result = -1;%0A%0A%09%09try %7B%0A%09%09%09
+Stream(true);%0A%0A%09%09try %7B%0A%09%09%09Process p = pb.start();%0A%0A%09%09%09ExecutorService es = Executors.newSingleThreadExecutor();%0A%0A%09%09%09Future%3CString%3E res = es.submit(() -%3E %7B%0A%09%09%09%09try (InputStream is = p.getInputStream(); StringWriter sw = new StringWriter()) %7B%0A%09%09%09%09%09IOUtils.copy(is, sw);%0A%09%09%09%09%09sw.flush();%0A%09%09%09%09%09return sw.toString();%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%0A%09%09%09int 
 resu
@@ -1763,25 +1763,16 @@
 sult = p
-b.start()
 .waitFor
@@ -1779,59 +1779,65 @@
 ();%0A
+%0A
 %09%09
-%7D catch (IOException %7C InterruptedException e)
+%09if(result == 0) %7B%0A%09%09%09%09return res.get();%0A%09%09%09%7D else
  %7B%0A
+%09
 %09%09%09l
@@ -1872,13 +1872,32 @@
 ting
-: %22 +
+ (code %7B%7D): %7B%7D%22, result,
  Arr
@@ -1918,35 +1918,83 @@
 rgs)
-, e
 );%0A%09%09
+%09
 %7D
-%0A
 %0A%09%09
-if(result != 0
+%7D catch (IOException %7C InterruptedException %7CExecutionException e
 ) %7B%0A
@@ -2033,32 +2033,13 @@
 ting
- (code %7B%7D): %7B%7D%22, result,
+: %22 +
  Arr
@@ -2060,14 +2060,33 @@
 rgs)
+, e
 );%0A%09%09%7D
+%0A%0A%09%09return null;
 %0A%09%7D%0A

return String and fix corrupted STDOUT