// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/request/DeployConfigRequest.java
// public class DeployConfigRequest extends ModuleRequest {
// 
//     @JsonCreator
//     private DeployConfigRequest(@JsonProperty("group_id") final String groupId, @JsonProperty("artifact_id") final String artifactId,
//                                 @JsonProperty("version") final String version, @JsonProperty("classifier") final String classifier,
//                                 @JsonProperty("type") final String type) {
//         super(groupId, artifactId, version, classifier, type);
//     }
// 
//     public static DeployConfigRequest build(String groupId, String artifactId, String version, String classifier) {
//         return new DeployConfigRequest(groupId, artifactId, version, classifier, "config");
//     }
// 
//     @Override
//     public boolean deleteBase() {
//         return false;
//     }
// 
//     @Override
//     public boolean checkConfig() {
//         return true;
//     }
// 
//     @Override
//     public String getLogName() {
//         return LogConstants.DEPLOY_CONFIG_REQUEST;
//     }
// }
// 
// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/util/LogConstants.java
// public final class LogConstants {
//     public static final String ERROR_EXECUTING_REQUEST = "Error executing request {}.";
//     public static final String REQUEST_ALREADY_REGISTERED = "[{} - {}]: Request already registered.";
//     public static final String REQUEST_NOT_REGISTERED = "[{} - {}]: Request not registered.";
// 
// 
//     public static final String INVOKE_CONTAINER = "InvokeContainer";
//     public static final String CONSOLE_COMMAND = "ConsoleCommand";
//     public static final String CLUSTER_MANAGER = "ClusterManager";
//     public static final String DEPLOY_REQUEST = "DeployRequest";
//     public static final String DEPLOY_CONFIG_REQUEST = "DeployConfigRequest";
//     public static final String DEPLOY_ARTIFACT_REQUEST = "DeployArtifactRequest";
//     public static final String AWS_ELB_REQUEST = "ConfigureAwsElb";
//     public static final String AWS_AS_REQUEST = "ConfigureAwsAutoScaling";
//     public static final String STARTUP = "Startup";
// 
//     private LogConstants() {
//         // hide
//     }
// }
// 
// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/util/ObservableCommand.java
// public class ObservableCommand<R extends ModuleRequest> {
// 
//     private static final Logger LOG = LoggerFactory.getLogger(ObservableCommand.class);
//     private static final Long POLLING_INTERVAL_IN_MS = 500L;
//     private final Integer expectedResultCode;
//     private final Vertx rxVertx;
//     private final R request;
//     private Process process;
// 
//     public ObservableCommand(R request, Integer expectedResultCode, Vertx vertx) {
//         this.request = request;
//         this.expectedResultCode = expectedResultCode;
//         this.rxVertx = vertx;
//     }
// 
//     public Observable<Integer> execute(ProcessBuilder builder) {
//         return observableCommand(builder)
//                 .flatMap(x -> waitForExit())
//                 .flatMap(x -> {
//                     if (process.exitValue() != expectedResultCode) {
//                         throw new IllegalStateException("Error executing process");
//                     }
//                     return just(x);
//                 });
//     }
// 
//     private Observable<Integer> waitForExit() {
//         return rxVertx.timerStream(POLLING_INTERVAL_IN_MS).toObservable()
//                 .flatMap(x -> {
//                     if (process.isAlive()) {
//                         return waitForExit();
//                     } else {
//                         if (process.exitValue() != expectedResultCode) {
//                             printStream(process.getInputStream(), false);
//                             throw new IllegalStateException(printStream(process.getErrorStream(), true));
//                         } else {
//                             printStream(process.getInputStream(), false);
//                         }
//                         return just(process.exitValue());
//                     }
//                 });
//     }
// 
//     private Observable<String> observableCommand(ProcessBuilder builder) {
//         return Observable.create(subscriber -> {
//             process = null;
//             try {
//                 builder.directory(new File(System.getProperty("java.io.tmpdir")));
//                 process = builder.start();
//             } catch (IOException e) {
//                 subscriber.onError(e);
//             }
//             subscriber.onNext("Done");
//             subscriber.onCompleted();
//         }, Emitter.BackpressureMode.NONE);
//     }
// 
//     private String printStream(InputStream stream, boolean error) {
//         if (stream == null) {
//             return null;
//         }
//         String line;
//         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
//             while ((line = reader.readLine()) != null) {
//                 if (error) {
//                     LOG.error("[{} - {}]: Command output -> '{}'", LogConstants.CONSOLE_COMMAND, request.getId(), line);
//                 } else {
//                     LOG.info("[{} - {}]: Command output -> '{}'", LogConstants.CONSOLE_COMMAND, request.getId(), line);
//                 }
//             }
//             return line;
//         } catch (Exception e) {
//             LOG.error(e.getMessage(), e);
//             throw new IllegalStateException(e);
//         }
//     }
// }


import io.vertx.rxjava.core.Vertx;
import nl.jpoint.vertx.deploy.agent.request.DeployConfigRequest;
import nl.jpoint.vertx.deploy.agent.util.LogConstants;
import nl.jpoint.vertx.deploy.agent.util.ObservableCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import static rx.Observable.just;
package nl.jpoint.vertx.deploy.agent.command;



public class RunConsoleCommand implements Command<DeployConfigRequest> {

    private static final Logger LOG = LoggerFactory.getLogger(RunConsoleCommand.class);

    private final String command;
    private final Vertx rxVertx;

    public RunConsoleCommand(io.vertx.core.Vertx vertx, String command) {
        this.command = command;
        this.rxVertx = new Vertx(vertx);
    }

    @Override
    public Observable<DeployConfigRequest> executeAsync(DeployConfigRequest deployConfigRequest) {
        if (command == null || command.isEmpty()) {
// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/request/DeployConfigRequest.java
// public class DeployConfigRequest extends ModuleRequest {
// 
//     @JsonCreator
//     private DeployConfigRequest(@JsonProperty("group_id") final String groupId, @JsonProperty("artifact_id") final String artifactId,
//                                 @JsonProperty("version") final String version, @JsonProperty("classifier") final String classifier,
//                                 @JsonProperty("type") final String type) {
//         super(groupId, artifactId, version, classifier, type);
//     }
// 
//     public static DeployConfigRequest build(String groupId, String artifactId, String version, String classifier) {
//         return new DeployConfigRequest(groupId, artifactId, version, classifier, "config");
//     }
// 
//     @Override
//     public boolean deleteBase() {
//         return false;
//     }
// 
//     @Override
//     public boolean checkConfig() {
//         return true;
//     }
// 
//     @Override
//     public String getLogName() {
//         return LogConstants.DEPLOY_CONFIG_REQUEST;
//     }
// }
// 
// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/util/LogConstants.java
// public final class LogConstants {
//     public static final String ERROR_EXECUTING_REQUEST = "Error executing request {}.";
//     public static final String REQUEST_ALREADY_REGISTERED = "[{} - {}]: Request already registered.";
//     public static final String REQUEST_NOT_REGISTERED = "[{} - {}]: Request not registered.";
// 
// 
//     public static final String INVOKE_CONTAINER = "InvokeContainer";
//     public static final String CONSOLE_COMMAND = "ConsoleCommand";
//     public static final String CLUSTER_MANAGER = "ClusterManager";
//     public static final String DEPLOY_REQUEST = "DeployRequest";
//     public static final String DEPLOY_CONFIG_REQUEST = "DeployConfigRequest";
//     public static final String DEPLOY_ARTIFACT_REQUEST = "DeployArtifactRequest";
//     public static final String AWS_ELB_REQUEST = "ConfigureAwsElb";
//     public static final String AWS_AS_REQUEST = "ConfigureAwsAutoScaling";
//     public static final String STARTUP = "Startup";
// 
//     private LogConstants() {
//         // hide
//     }
// }
// 
// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/util/ObservableCommand.java
// public class ObservableCommand<R extends ModuleRequest> {
// 
//     private static final Logger LOG = LoggerFactory.getLogger(ObservableCommand.class);
//     private static final Long POLLING_INTERVAL_IN_MS = 500L;
//     private final Integer expectedResultCode;
//     private final Vertx rxVertx;
//     private final R request;
//     private Process process;
// 
//     public ObservableCommand(R request, Integer expectedResultCode, Vertx vertx) {
//         this.request = request;
//         this.expectedResultCode = expectedResultCode;
//         this.rxVertx = vertx;
//     }
// 
//     public Observable<Integer> execute(ProcessBuilder builder) {
//         return observableCommand(builder)
//                 .flatMap(x -> waitForExit())
//                 .flatMap(x -> {
//                     if (process.exitValue() != expectedResultCode) {
//                         throw new IllegalStateException("Error executing process");
//                     }
//                     return just(x);
//                 });
//     }
// 
//     private Observable<Integer> waitForExit() {
//         return rxVertx.timerStream(POLLING_INTERVAL_IN_MS).toObservable()
//                 .flatMap(x -> {
//                     if (process.isAlive()) {
//                         return waitForExit();
//                     } else {
//                         if (process.exitValue() != expectedResultCode) {
//                             printStream(process.getInputStream(), false);
//                             throw new IllegalStateException(printStream(process.getErrorStream(), true));
//                         } else {
//                             printStream(process.getInputStream(), false);
//                         }
//                         return just(process.exitValue());
//                     }
//                 });
//     }
// 
//     private Observable<String> observableCommand(ProcessBuilder builder) {
//         return Observable.create(subscriber -> {
//             process = null;
//             try {
//                 builder.directory(new File(System.getProperty("java.io.tmpdir")));
//                 process = builder.start();
//             } catch (IOException e) {
//                 subscriber.onError(e);
//             }
//             subscriber.onNext("Done");
//             subscriber.onCompleted();
//         }, Emitter.BackpressureMode.NONE);
//     }
// 
//     private String printStream(InputStream stream, boolean error) {
//         if (stream == null) {
//             return null;
//         }
//         String line;
//         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
//             while ((line = reader.readLine()) != null) {
//                 if (error) {
//                     LOG.error("[{} - {}]: Command output -> '{}'", LogConstants.CONSOLE_COMMAND, request.getId(), line);
//                 } else {
//                     LOG.info("[{} - {}]: Command output -> '{}'", LogConstants.CONSOLE_COMMAND, request.getId(), line);
//                 }
//             }
//             return line;
//         } catch (Exception e) {
//             LOG.error(e.getMessage(), e);
//             throw new IllegalStateException(e);
//         }
//     }
// }


// Path: vertx-deploy-agent/src/main/java/nl/jpoint/vertx/deploy/agent/command/RunConsoleCommand.java
import io.vertx.rxjava.core.Vertx;
import nl.jpoint.vertx.deploy.agent.request.DeployConfigRequest;
import nl.jpoint.vertx.deploy.agent.util.LogConstants;
import nl.jpoint.vertx.deploy.agent.util.ObservableCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import static rx.Observable.just;

package nl.jpoint.vertx.deploy.agent.command;



public class RunConsoleCommand implements Command<DeployConfigRequest> {

    private static final Logger LOG = LoggerFactory.getLogger(RunConsoleCommand.class);

    private final String command;
    private final Vertx rxVertx;

    public RunConsoleCommand(io.vertx.core.Vertx vertx, String command) {
        this.command = command;
        this.rxVertx = new Vertx(vertx);
    }

    @Override
    public Observable<DeployConfigRequest> executeAsync(DeployConfigRequest deployConfigRequest) {
        if (command == null || command.isEmpty()) {
            LOG.error("[{} - {}]: Failed to run empty command.", LogConstants.CONSOLE_COMMAND, deployConfigRequest.getId());