Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit a0df3d3

Browse files
committed
Improve file access
1 parent b42a563 commit a0df3d3

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

pkg/tarmak/config/config.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -322,27 +322,13 @@ func (c *Config) configPath() string {
322322

323323
func (c *Config) ReadConfig() (*tarmakv1alpha1.Config, error) {
324324
var config *tarmakv1alpha1.Config
325-
var configBytes bytes.Buffer
326-
var result *multierror.Error
327-
328-
for _, suffix := range c.flags.ConfigSuffixes {
329-
b, err := c.readConfigFragment(suffix)
330-
if err != nil {
331-
result = multierror.Append(result, fmt.Errorf("failed to read main tarmak configs: %v", err))
332-
continue
333-
}
334-
335-
if _, err := configBytes.Write(b); err != nil {
336-
result = multierror.Append(result, err)
337-
continue
338-
}
339-
}
340325

341-
if result.ErrorOrNil() != nil {
342-
return nil, result.ErrorOrNil()
326+
configBytes, err := c.readConfigFragments(c.flags.ConfigSuffixes)
327+
if err != nil {
328+
return nil, fmt.Errorf("failed to read tarmak configs: %v", err)
343329
}
344330

345-
configObj, gvk, err := c.codecs.UniversalDecoder(tarmakv1alpha1.SchemeGroupVersion).Decode(configBytes.Bytes(), nil, nil)
331+
configObj, gvk, err := c.codecs.UniversalDecoder(tarmakv1alpha1.SchemeGroupVersion).Decode(configBytes, nil, nil)
346332
if err != nil {
347333
return nil, fmt.Errorf("failed to decode config bytes from file: %v", err)
348334
}
@@ -356,16 +342,23 @@ func (c *Config) ReadConfig() (*tarmakv1alpha1.Config, error) {
356342
return config, nil
357343
}
358344

359-
func (c *Config) readConfigFragment(fragment string) ([]byte, error) {
345+
func (c *Config) readConfigFragments(suffixes []string) ([]byte, error) {
360346
dir, err := ioutil.ReadDir(c.tarmak.ConfigPath())
361347
if err != nil {
362-
return nil, nil
348+
return nil, err
363349
}
364350

365351
var fragFiles []os.FileInfo
366352
for _, f := range dir {
367-
if !f.IsDir() && strings.HasPrefix(f.Name(), fragment) && strings.HasSuffix(f.Name(), ".yaml") {
368-
fragFiles = append(fragFiles, f)
353+
if !f.IsDir() && strings.HasSuffix(f.Name(), ".yaml") {
354+
355+
for _, suffix := range suffixes {
356+
if strings.HasPrefix(f.Name(), suffix) {
357+
fragFiles = append(fragFiles, f)
358+
break
359+
}
360+
}
361+
369362
}
370363
}
371364

0 commit comments

Comments
 (0)