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 <noreply@anthropic.com>
Signed-off-by: Dmitriy Aratin <dima.aratin@mail.ru>
pull/31762/head
Dmitriy Aratin 8 months ago
parent 5bbe84ec4f
commit 0e9b006b16

@ -213,11 +213,11 @@ func splitAndDeannotate(postrendered string) (map[string]string, error) {
return reconstructed, nil 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" // 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 // - skipChartNameDir: removes the root chart name directory
// - skipTemplatesDir: removes all "templates" directories from the path // - 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 { if !skipChartNameDir && !skipTemplatesDir {
return name return name
} }
@ -230,6 +230,10 @@ func transformManifestPath(name string, skipChartNameDir, skipTemplatesDir bool)
var result []string var result []string
for i, part := range parts { 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 // Skip the first part (chart name) if skipChartNameDir is true
if i == 0 && skipChartNameDir { if i == 0 && skipChartNameDir {
continue continue
@ -371,7 +375,7 @@ func (cfg *Configuration) renderResources(ch *chart.Chart, values common.Values,
if outputDir == "" { if outputDir == "" {
fmt.Fprintf(b, "---\n# Source: %s\n%s\n", crd.Filename, string(crd.File.Data[:])) fmt.Fprintf(b, "---\n# Source: %s\n%s\n", crd.Filename, string(crd.File.Data[:]))
} else { } else {
transformedName := transformManifestPath(crd.Filename, skipChartNameDir, skipTemplatesDir) transformedName := TransformManifestPath(crd.Filename, skipChartNameDir, skipTemplatesDir)
err = writeToFile(outputDir, transformedName, string(crd.File.Data[:]), fileWritten[transformedName]) err = writeToFile(outputDir, transformedName, string(crd.File.Data[:]), fileWritten[transformedName])
if err != nil { if err != nil {
return hs, b, "", err 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 // 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 // release, we should move this logic to template only as it is not
// used by install or upgrade // 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]) err = writeToFile(newDir, transformedName, m.Content, fileWritten[transformedName])
if err != nil { if err != nil {
return hs, b, "", err return hs, b, "", err

@ -1064,7 +1064,7 @@ func TestTransformManifestPath(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) assert.Equal(t, tt.expected, result)
}) })
} }

@ -132,7 +132,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if client.UseReleaseName { if client.UseReleaseName {
newDir = filepath.Join(client.OutputDir, client.ReleaseName) 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)) _, err := os.Stat(filepath.Join(newDir, transformedPath))
if err == nil { if err == nil {
fileWritten[transformedPath] = true fileWritten[transformedPath] = true
@ -277,39 +277,4 @@ func ensureDirectoryForFile(file string) error {
} }
return os.MkdirAll(baseDir, 0755) 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, "/")
}
Loading…
Cancel
Save