package com.netflix.spinnaker.rosco.manifests.helm;

import com.netflix.spinnaker.rosco.jobs.BakeRecipe;
import com.netflix.spinnaker.rosco.manifests.BakeManifestRequest;
import com.netflix.spinnaker.rosco.manifests.TemplateUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

@Component
public class HelmTemplateUtils extends TemplateUtils {
  @Override
  public BakeRecipe buildBakeRecipe(BakeManifestRequest request) {
    BakeRecipe result = super.buildBakeRecipe(request);
    Path templatePath;
    try {
      templatePath = downloadArtifactToTmpFile(request.getInputArtifact());
    } catch (IOException e) {
      throw new IllegalStateException("Failed to fetch helm template: " + e.getMessage(), e);
    }

    List<String> command = new ArrayList<>();
    command.add("helm");
    command.add("template");
    command.add(templatePath.toString());
    command.add("--name");
    command.add(request.getOutputName());

    result.setCommand(command);

    return result;
  }
}

@@ -379,16 +379,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A@Compo
@@ -1052,24 +1052,312 @@
 utName());%0A%0A
+    Map%3CString, Object%3E overrides = request.getOverrides();%0A    if (overrides != null) %7B%0A      for (Map.Entry%3CString, Object%3E entry : overrides.entrySet()) %7B%0A        command.add(%22--set%22);%0A        command.add(%22'%22 + entry.getKey() + %22=%22 + entry.getValue().toString() + %22'%22);%0A      %7D%0A    %7D%0A%0A
     result.s

support helm `--set` overrides (#247)