From 0e9b006b16946a9c150172eef7eeb8ebbbeb6a2f Mon Sep 17 00:00:00 2001 From: Dmitriy Aratin Date: Wed, 28 Jan 2026 23:15:28 +0700 Subject: [PATCH] refactor(template): consolidate TransformManifestPath in action package - Export TransformManifestPath from pkg/action instead of duplicating - Add check to skip empty path parts (handles leading/double slashes) - Remove duplicate function from pkg/cmd/template.go Co-Authored-By: Claude Opus 4.5 Signed-off-by: Dmitriy Aratin --- pkg/action/action.go | 12 ++++++++---- pkg/action/action_test.go | 2 +- pkg/cmd/template.go | 39 ++------------------------------------- 3 files changed, 11 insertions(+), 42 deletions(-) diff --git a/pkg/action/action.go b/pkg/action/action.go index c0a444032..0dd48f447 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -213,11 +213,11 @@ func splitAndDeannotate(postrendered string) (map[string]string, error) { return reconstructed, nil } -// transformManifestPath modifies the manifest path based on the skipChartNameDir and skipTemplatesDir flags. +// TransformManifestPath modifies the manifest path based on the skipChartNameDir and skipTemplatesDir flags. // The input path is typically in the format "chart-name/templates/file.yaml" or "chart-name/charts/subchart/templates/file.yaml" // - skipChartNameDir: removes the root chart name directory // - skipTemplatesDir: removes all "templates" directories from the path -func transformManifestPath(name string, skipChartNameDir, skipTemplatesDir bool) string { +func TransformManifestPath(name string, skipChartNameDir, skipTemplatesDir bool) string { if !skipChartNameDir && !skipTemplatesDir { return name } @@ -230,6 +230,10 @@ func transformManifestPath(name string, skipChartNameDir, skipTemplatesDir bool) var result []string for i, part := range parts { + // Skip empty parts (e.g., from leading slash or double slashes) + if part == "" { + continue + } // Skip the first part (chart name) if skipChartNameDir is true if i == 0 && skipChartNameDir { continue @@ -371,7 +375,7 @@ func (cfg *Configuration) renderResources(ch *chart.Chart, values common.Values, if outputDir == "" { fmt.Fprintf(b, "---\n# Source: %s\n%s\n", crd.Filename, string(crd.File.Data[:])) } else { - transformedName := transformManifestPath(crd.Filename, skipChartNameDir, skipTemplatesDir) + transformedName := TransformManifestPath(crd.Filename, skipChartNameDir, skipTemplatesDir) err = writeToFile(outputDir, transformedName, string(crd.File.Data[:]), fileWritten[transformedName]) if err != nil { return hs, b, "", err @@ -397,7 +401,7 @@ func (cfg *Configuration) renderResources(ch *chart.Chart, values common.Values, // output dir is only used by `helm template`. In the next major // release, we should move this logic to template only as it is not // used by install or upgrade - transformedName := transformManifestPath(m.Name, skipChartNameDir, skipTemplatesDir) + transformedName := TransformManifestPath(m.Name, skipChartNameDir, skipTemplatesDir) err = writeToFile(newDir, transformedName, m.Content, fileWritten[transformedName]) if err != nil { return hs, b, "", err diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 5e57abfa6..38e4fba83 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -1064,7 +1064,7 @@ func TestTransformManifestPath(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := transformManifestPath(tt.input, tt.skipChartNameDir, tt.skipTemplatesDir) + result := TransformManifestPath(tt.input, tt.skipChartNameDir, tt.skipTemplatesDir) assert.Equal(t, tt.expected, result) }) } diff --git a/pkg/cmd/template.go b/pkg/cmd/template.go index e4eb678e7..afd9b584c 100644 --- a/pkg/cmd/template.go +++ b/pkg/cmd/template.go @@ -132,7 +132,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { if client.UseReleaseName { newDir = filepath.Join(client.OutputDir, client.ReleaseName) } - transformedPath := transformManifestPath(m.Path, client.SkipChartNameDir, client.SkipTemplatesDir) + transformedPath := action.TransformManifestPath(m.Path, client.SkipChartNameDir, client.SkipTemplatesDir) _, err := os.Stat(filepath.Join(newDir, transformedPath)) if err == nil { fileWritten[transformedPath] = true @@ -277,39 +277,4 @@ func ensureDirectoryForFile(file string) error { } return os.MkdirAll(baseDir, 0755) -} - -// transformManifestPath modifies the manifest path based on the skipChartNameDir and skipTemplatesDir flags. -// The input path is typically in the format "chart-name/templates/file.yaml" or "chart-name/charts/subchart/templates/file.yaml" -// - skipChartNameDir: removes the root chart name directory -// - skipTemplatesDir: removes all "templates" directories from the path -func transformManifestPath(name string, skipChartNameDir, skipTemplatesDir bool) string { - if !skipChartNameDir && !skipTemplatesDir { - return name - } - - parts := strings.Split(name, "/") - if len(parts) == 0 { - return name - } - - var result []string - - for i, part := range parts { - // Skip the first part (chart name) if skipChartNameDir is true - if i == 0 && skipChartNameDir { - continue - } - // Skip "templates" directories if skipTemplatesDir is true - if skipTemplatesDir && part == "templates" { - continue - } - result = append(result, part) - } - - if len(result) == 0 { - return name - } - - return strings.Join(result, "/") -} +} \ No newline at end of file