github.com/openshift/installer@v1.4.17/pkg/asset/store/filefetcher.go (about)

     1  package store
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/openshift/installer/pkg/asset"
     8  )
     9  
    10  type fileFetcher struct {
    11  	directory string
    12  }
    13  
    14  // FetchByName returns the file with the given name.
    15  func (f *fileFetcher) FetchByName(name string) (*asset.File, error) {
    16  	data, err := os.ReadFile(filepath.Join(f.directory, name))
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	return &asset.File{Filename: name, Data: data}, nil
    21  }
    22  
    23  // FetchByPattern returns the files whose name match the given regexp.
    24  func (f *fileFetcher) FetchByPattern(pattern string) (files []*asset.File, err error) {
    25  	matches, err := filepath.Glob(filepath.Join(f.directory, pattern))
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	files = make([]*asset.File, 0, len(matches))
    31  	for _, path := range matches {
    32  		data, err := os.ReadFile(path)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  
    37  		filename, err := filepath.Rel(f.directory, path)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  		files = append(files, &asset.File{
    43  			Filename: filename,
    44  			Data:     data,
    45  		})
    46  	}
    47  
    48  	return files, nil
    49  }