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

     1  package asset
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // State is the state of an Asset.
    11  type State struct {
    12  	Contents []Content
    13  }
    14  
    15  // Content is a generated portion of an Asset.
    16  type Content struct {
    17  	Name string // the path on disk for this content.
    18  	Data []byte
    19  }
    20  
    21  // PersistToFile persists the data in the State to files. Each Content entry that
    22  // has a non-empty Name will be persisted to a file with that name.
    23  func (s *State) PersistToFile(directory string) error {
    24  	if s == nil {
    25  		return nil
    26  	}
    27  
    28  	for _, c := range s.Contents {
    29  		if c.Name == "" {
    30  			continue
    31  		}
    32  		path := filepath.Join(directory, c.Name)
    33  		if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
    34  			return errors.Wrap(err, "failed to create dir")
    35  		}
    36  		if err := os.WriteFile(path, c.Data, 0o644); err != nil { //nolint:gosec // no sensitive info
    37  			return errors.Wrap(err, "failed to write file")
    38  		}
    39  	}
    40  	return nil
    41  }