/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thoughtworks.go.domain.builder.pluggableTask;

import com.thoughtworks.go.plugin.api.task.Console;
import com.thoughtworks.go.util.command.ErrorConsumer;
import com.thoughtworks.go.util.command.OutputConsumer;
import com.thoughtworks.go.util.command.SafeOutputStreamConsumer;
import com.thoughtworks.go.util.command.StreamPumper;

import java.io.InputStream;
import java.util.Map;

public class PluggableTaskConsole implements Console {
    public static final String MASK_VALUE = "********";
    private final SafeOutputStreamConsumer safeOutputStreamConsumer;
    private final String consoleLogCharset;

    public PluggableTaskConsole(SafeOutputStreamConsumer safeOutputStreamConsumer, String consoleLogCharset) {
        this.safeOutputStreamConsumer = safeOutputStreamConsumer;
        this.consoleLogCharset = consoleLogCharset;
    }

    @Override
    public void printLine(String line) {
        safeOutputStreamConsumer.stdOutput(line);
    }

    @Override
    public void readErrorOf(InputStream in) {
        StreamPumper.pump(in, new ErrorConsumer(safeOutputStreamConsumer), "", consoleLogCharset);
    }

    @Override
    public void readOutputOf(InputStream in) {
        StreamPumper.pump(in, new OutputConsumer(safeOutputStreamConsumer), "", consoleLogCharset);
    }

    @Override
    public void printEnvironment(Map<String, String> environment, SecureEnvVarSpecifier secureEnvVarSpecifier) {
        safeOutputStreamConsumer.stdOutput("Environment variables: ");
        for (String key : environment.keySet()) {
            boolean secure = secureEnvVarSpecifier.isSecure(key);
            safeOutputStreamConsumer.stdOutput(String.format("Name= %s  Value= %s", key, secure ? MASK_VALUE : environment.get(key)));
        }
    }
}

@@ -1213,16 +1213,100 @@
 Charset;
+%0A    private ErrorConsumer errorConsumer;%0A    private OutputConsumer outputConsumer;
 %0A%0A    pu
@@ -1472,24 +1472,164 @@
 amConsumer;%0A
+        outputConsumer = new OutputConsumer(safeOutputStreamConsumer);%0A        errorConsumer = new ErrorConsumer(safeOutputStreamConsumer);%0A
         this
@@ -1877,21 +1877,17 @@
 ump(in, 
-new E
+e
 rrorCons
@@ -1894,34 +1894,8 @@
 umer
-(safeOutputStreamConsumer)
 , %22%22
@@ -2018,51 +2018,22 @@
 in, 
-new OutputConsumer(safeOutputStream
+output
 Consumer
 ), %22
@@ -2028,17 +2028,16 @@
 Consumer
-)
 , %22%22, co

Refactor to make output and error consumer instance variables.