fix: address review comments - correct HasPrefix args, fix output dir structure, fix test mock init
Agent-Logs-Url: https://github.com/helmfile/helmfile/sessions/33d92423-fc47-4080-8307-5af9b16dd9c6 Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
parent
9685ec2bb1
commit
1ec00a378f
|
|
@ -650,7 +650,7 @@ func (helm *execer) TemplateRelease(name string, chart string, flags ...string)
|
|||
var hasPostRenderer bool
|
||||
|
||||
for _, f := range flags {
|
||||
if strings.HasPrefix("--output-dir", f) {
|
||||
if f == "--output-dir" || strings.HasPrefix(f, "--output-dir=") {
|
||||
outputToFile = true
|
||||
}
|
||||
if f == "--post-renderer" {
|
||||
|
|
@ -678,13 +678,28 @@ func (helm *execer) TemplateRelease(name string, chart string, flags ...string)
|
|||
filteredFlags = append(filteredFlags, flags[i])
|
||||
}
|
||||
|
||||
if outputDir == "" {
|
||||
return fmt.Errorf("output dir not found for template command")
|
||||
}
|
||||
|
||||
out, err := helm.exec(append(args, filteredFlags...), map[string]string{}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templatesDir := filepath.Join(outputDir, "templates")
|
||||
legacyOutputPath := filepath.Join(outputDir, name+".yaml")
|
||||
|
||||
if removeErr := os.Remove(legacyOutputPath); removeErr != nil && !os.IsNotExist(removeErr) {
|
||||
return removeErr
|
||||
}
|
||||
|
||||
if len(out) > 0 {
|
||||
outputPath := filepath.Join(outputDir, name+".yaml")
|
||||
if mkdirErr := os.MkdirAll(templatesDir, 0755); mkdirErr != nil {
|
||||
return mkdirErr
|
||||
}
|
||||
|
||||
outputPath := filepath.Join(templatesDir, name+".yaml")
|
||||
if writeErr := os.WriteFile(outputPath, append(out, '\n'), 0644); writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1260,12 +1260,14 @@ func Test_Template_PostRendererWithOutputDir(t *testing.T) {
|
|||
var buffer bytes.Buffer
|
||||
logger := NewLogger(&buffer, "debug")
|
||||
|
||||
runner := &mockRunner{output: []byte("apiVersion: v1\nkind: Namespace\n")}
|
||||
runner := &mockRunner{}
|
||||
helm, err := New("helm", HelmExecOptions{}, logger, "config", "dev", runner)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
runner.output = []byte("apiVersion: v1\nkind: Namespace\n")
|
||||
|
||||
err = helm.TemplateRelease("myrelease", "path/to/chart",
|
||||
"--post-renderer", "/bin/echo",
|
||||
"--output-dir", tmpDir,
|
||||
|
|
@ -1275,7 +1277,7 @@ func Test_Template_PostRendererWithOutputDir(t *testing.T) {
|
|||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
outputPath := filepath.Join(tmpDir, "myrelease.yaml")
|
||||
outputPath := filepath.Join(tmpDir, "templates", "myrelease.yaml")
|
||||
data, err := os.ReadFile(outputPath)
|
||||
if err != nil {
|
||||
t.Fatalf("expected output file %s to exist: %v", outputPath, err)
|
||||
|
|
|
|||
|
|
@ -17,9 +17,14 @@ else
|
|||
--output-dir-template "${issue_2515_output_dir}/{{.Release.Name}}" \
|
||||
&> ${issue_2515_tmp}/log || fail "helmfile template should not fail"
|
||||
|
||||
issue_2515_output_file="${issue_2515_output_dir}/issue-2515/issue-2515.yaml"
|
||||
if [ ! -f "${issue_2515_output_file}" ]; then
|
||||
fail "Expected output file ${issue_2515_output_file} to exist"
|
||||
issue_2515_templates_dir="${issue_2515_output_dir}/issue-2515/templates"
|
||||
if [ ! -d "${issue_2515_templates_dir}" ]; then
|
||||
fail "Expected templates directory ${issue_2515_templates_dir} to exist"
|
||||
fi
|
||||
|
||||
issue_2515_output_file=$(find "${issue_2515_templates_dir}" -type f \( -name '*.yaml' -o -name '*.yml' \) | head -n 1)
|
||||
if [ -z "${issue_2515_output_file}" ]; then
|
||||
fail "Expected rendered YAML file under ${issue_2515_templates_dir}"
|
||||
fi
|
||||
|
||||
if grep -q "original-cm" "${issue_2515_output_file}"; then
|
||||
|
|
|
|||
Loading…
Reference in New Issue