6
6
"errors"
7
7
"fmt"
8
8
"io/fs"
9
+ "path"
9
10
"path/filepath"
10
11
"regexp"
11
12
"sort"
@@ -46,7 +47,7 @@ type ChartFile struct {
46
47
ManifestContent string
47
48
}
48
49
49
- func New (path string , opts ... Option ) (* Parser , error ) {
50
+ func New (src string , opts ... Option ) (* Parser , error ) {
50
51
51
52
client := action .NewInstall (& action.Configuration {})
52
53
client .DryRun = true // don't do anything
@@ -55,7 +56,7 @@ func New(path string, opts ...Option) (*Parser, error) {
55
56
56
57
p := & Parser {
57
58
helmClient : client ,
58
- ChartSource : path ,
59
+ ChartSource : src ,
59
60
logger : log .WithPrefix ("helm parser" ),
60
61
}
61
62
@@ -79,10 +80,10 @@ func New(path string, opts ...Option) (*Parser, error) {
79
80
return p , nil
80
81
}
81
82
82
- func (p * Parser ) ParseFS (ctx context.Context , target fs.FS , path string ) error {
83
- p .workingFS = target
83
+ func (p * Parser ) ParseFS (ctx context.Context , fsys fs.FS , target string ) error {
84
+ p .workingFS = fsys
84
85
85
- if err := fs .WalkDir (p .workingFS , filepath .ToSlash (path ), func (path string , entry fs.DirEntry , err error ) error {
86
+ if err := fs .WalkDir (p .workingFS , filepath .ToSlash (target ), func (filePath string , entry fs.DirEntry , err error ) error {
86
87
select {
87
88
case <- ctx .Done ():
88
89
return ctx .Err ()
@@ -95,16 +96,20 @@ func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) error {
95
96
return nil
96
97
}
97
98
98
- if detection .IsArchive (path ) {
99
- tarFS , err := p .addTarToFS (path )
99
+ if _ , err := fs .Stat (p .workingFS , filePath ); err != nil {
100
+ return nil
101
+ }
102
+
103
+ if detection .IsArchive (filePath ) && ! isDependencyChartArchive (p .workingFS , filePath ) {
104
+ tarFS , err := p .addTarToFS (filePath )
100
105
if errors .Is (err , errSkipFS ) {
101
106
// an unpacked Chart already exists
102
107
return nil
103
108
} else if err != nil {
104
- return fmt .Errorf ("failed to add tar %q to FS: %w" , path , err )
109
+ return fmt .Errorf ("failed to add tar %q to FS: %w" , filePath , err )
105
110
}
106
111
107
- targetPath := filepath .Dir (path )
112
+ targetPath := filepath .Dir (filePath )
108
113
if targetPath == "" {
109
114
targetPath = "."
110
115
}
@@ -114,7 +119,7 @@ func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) error {
114
119
}
115
120
return nil
116
121
} else {
117
- return p .addPaths (path )
122
+ return p .addPaths (filePath )
118
123
}
119
124
}); err != nil {
120
125
return fmt .Errorf ("walk dir error: %w" , err )
@@ -123,19 +128,29 @@ func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) error {
123
128
return nil
124
129
}
125
130
131
+ func isDependencyChartArchive (fsys fs.FS , archivePath string ) bool {
132
+ parent := path .Dir (archivePath )
133
+ if path .Base (parent ) != "charts" {
134
+ return false
135
+ }
136
+
137
+ _ , err := fs .Stat (fsys , path .Join (parent , ".." , "Chart.yaml" ))
138
+ return err == nil
139
+ }
140
+
126
141
func (p * Parser ) addPaths (paths ... string ) error {
127
- for _ , path := range paths {
128
- if _ , err := fs .Stat (p .workingFS , path ); err != nil {
142
+ for _ , filePath := range paths {
143
+ if _ , err := fs .Stat (p .workingFS , filePath ); err != nil {
129
144
return err
130
145
}
131
146
132
- if strings .HasSuffix (path , "Chart.yaml" ) && p .rootPath == "" {
133
- if err := p .extractChartName (path ); err != nil {
147
+ if strings .HasSuffix (filePath , "Chart.yaml" ) && p .rootPath == "" {
148
+ if err := p .extractChartName (filePath ); err != nil {
134
149
return err
135
150
}
136
- p .rootPath = filepath .Dir (path )
151
+ p .rootPath = filepath .Dir (filePath )
137
152
}
138
- p .filepaths = append (p .filepaths , path )
153
+ p .filepaths = append (p .filepaths , filePath )
139
154
}
140
155
return nil
141
156
}
0 commit comments