package jetbrains.buildServer.helm;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.runner.BuildServiceAdapter;
import jetbrains.buildServer.agent.runner.ProgramCommandLine;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static jetbrains.buildServer.helm.HelmConstants.*;

/**
 * Created by Evgeniy Koshkin (evgeniy.koshkin@jetbrains.com) on 18.10.17.
 */
public class InstallBuildService extends BuildServiceAdapter {
    @NotNull
    @Override
    public ProgramCommandLine makeProgramCommandLine() throws RunBuildException {
        return new ProgramCommandLine() {
            @NotNull
            @Override
            public String getExecutablePath() throws RunBuildException {
                return getConfigParameters().get(HELM_PATH_CONFIG_PARAM);
            }

            @NotNull
            @Override
            public String getWorkingDirectory() throws RunBuildException {
                return getRunnerContext().getBuild().getCheckoutDirectory().getAbsolutePath();
            }

            @NotNull
            @Override
            public List<String> getArguments() throws RunBuildException {
                Map<String, String> runnerParameters = getRunnerParameters();
                List<String> result = new LinkedList<String>();
                result.add(HELM_INSTALL_COMMAND);
                result.add(StringUtil.emptyIfNull(runnerParameters.get(CHART)));
                result.add(StringUtil.emptyIfNull(runnerParameters.get(ADDITIONAL_FLAGS)));
                return result;
            }

            @NotNull
            @Override
            public Map<String, String> getEnvironment() throws RunBuildException {
                return getRunnerContext().getBuildParameters().getEnvironmentVariables();
            }
        };
    }
}

@@ -296,37 +296,8 @@
 l;%0A%0A
-import java.util.LinkedList;%0A
 impo
@@ -1342,133 +1342,106 @@
     
-List%3CString%3E result = new LinkedList%3CString%3E();%0A                result.add(HELM_INSTALL_COMMAND);%0A                result.add(
+return StringUtil.splitCommandArgumentsAndUnquote(String.format(%22%25s %25s %25s%22, HELM_INSTALL_COMMAND, 
 Stri
@@ -1491,38 +1491,10 @@
 RT))
-);%0A                result.add(
+, 
 Stri
@@ -1556,39 +1556,9 @@
 S)))
-;%0A                return result
+)
 ;%0A  

fix passing parameters to helm install