@@ -6,8 +6,12 @@ import (
66 "path"
77 "regexp"
88 "strings"
9+
10+ "github.com/cockroachdb/errors"
911)
1012
13+ const presetGenericName = "generic"
14+
1115//go:embed all:cozystack all:generic all:talm
1216var embeddedCharts embed.FS
1317
@@ -16,83 +20,87 @@ var embeddedCharts embed.FS
1620func PresetFiles () (map [string ]string , error ) {
1721 filesMap := make (map [string ]string )
1822 regex := regexp .MustCompile (`(name|version): \S+` )
19-
20- err := fs .WalkDir (embeddedCharts , "." , func (filePath string , d fs.DirEntry , err error ) error {
23+
24+ err := fs .WalkDir (embeddedCharts , "." , func (filePath string , entry fs.DirEntry , err error ) error {
2125 if err != nil {
22- return err
26+ // WalkDir surfaces a plain *fs.PathError on failure;
27+ // wrap with the offending path so a downstream caller
28+ // reading just the error message can locate the bad file
29+ // without re-running with extra logging.
30+ return errors .Wrapf (err , "walking embedded charts at %q" , filePath )
2331 }
24-
25- if d .IsDir () {
32+
33+ if entry .IsDir () {
2634 return nil
2735 }
28-
36+
2937 // Skip talm subdirectories in preset charts (cozystack/charts/talm, generic/charts/talm)
3038 // but include files from the main talm chart (talm/templates/_helpers.tpl, etc.)
31- if strings .HasPrefix (filePath , "cozystack/charts/talm/" ) ||
32- strings .HasPrefix (filePath , "generic/charts/talm/" ) {
39+ if strings .HasPrefix (filePath , "cozystack/charts/talm/" ) ||
40+ strings .HasPrefix (filePath , "generic/charts/talm/" ) {
3341 return nil
3442 }
35-
43+
3644 // Read file content
3745 data , err := embeddedCharts .ReadFile (filePath )
3846 if err != nil {
39- return err
47+ return errors . Wrapf ( err , "reading embedded chart file %q" , filePath )
4048 }
41-
49+
4250 content := string (data )
43-
51+
4452 // For Chart.yaml files, replace name and version with %s
4553 if path .Base (filePath ) == "Chart.yaml" {
4654 content = regex .ReplaceAllString (content , "$1: %s" )
4755 }
48-
56+
4957 // Use the file path as-is (relative to charts directory)
5058 filesMap [filePath ] = content
51-
59+
5260 return nil
5361 })
54-
5562 if err != nil {
56- return nil , err
63+ return nil , errors . Wrap ( err , "walking embedded charts" )
5764 }
58-
65+
5966 return filesMap , nil
6067}
6168
6269// AvailablePresets returns a list of available preset chart names.
63- // The "generic" preset is always first if it exists.
70+ // The presetGenericName preset is always first if it exists.
6471func AvailablePresets () ([]string , error ) {
65- var presets []string
66- var hasGeneric bool
67-
72+ var (
73+ presets []string
74+ hasGeneric bool
75+ )
76+
6877 entries , err := embeddedCharts .ReadDir ("." )
6978 if err != nil {
70- return nil , err
79+ return nil , errors . Wrap ( err , "reading embedded charts root" )
7180 }
72-
81+
7382 for _ , entry := range entries {
7483 if ! entry .IsDir () {
7584 continue
7685 }
77-
86+
7887 name := entry .Name ()
7988 // Skip talm as it's a library chart, not a preset
8089 if name == "talm" {
8190 continue
8291 }
83-
84- if name == "generic" {
92+
93+ if name == presetGenericName {
8594 hasGeneric = true
8695 } else {
8796 presets = append (presets , name )
8897 }
8998 }
90-
99+
91100 // Put generic first if it exists
92101 if hasGeneric {
93- presets = append ([]string {"generic" }, presets ... )
102+ presets = append ([]string {presetGenericName }, presets ... )
94103 }
95-
104+
96105 return presets , nil
97106}
98-
0 commit comments