github.com/replicatedhq/ship@v0.55.0/pkg/specs/stategetter/client.go (about)

     1  package stategetter
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"path/filepath"
     7  
     8  	"github.com/go-kit/kit/log"
     9  	"github.com/pkg/errors"
    10  	"github.com/replicatedhq/ship/pkg/state"
    11  	"github.com/spf13/afero"
    12  )
    13  
    14  type StateGetter struct {
    15  	Logger   log.Logger
    16  	Contents *state.UpstreamContents
    17  	Fs       afero.Afero
    18  }
    19  
    20  func NewStateGetter(fs afero.Afero, logger log.Logger, contents *state.UpstreamContents) *StateGetter {
    21  	return &StateGetter{
    22  		Contents: contents,
    23  		Fs:       fs,
    24  		Logger:   logger,
    25  	}
    26  }
    27  
    28  func (g *StateGetter) GetFiles(
    29  	ctx context.Context,
    30  	upstream string,
    31  	destinationPath string,
    32  ) (string, error) {
    33  	stateUnpackPath := filepath.Join(destinationPath, "state")
    34  
    35  	for _, upstreamFile := range g.Contents.UpstreamFiles {
    36  		err := g.Fs.MkdirAll(filepath.Join(stateUnpackPath, filepath.Dir(upstreamFile.FilePath)), 0755)
    37  		if err != nil {
    38  			return "", errors.Wrapf(err, "create dir for file %s", upstreamFile.FilePath)
    39  		}
    40  
    41  		rawContents, err := base64.StdEncoding.DecodeString(upstreamFile.FileContents)
    42  		if err != nil {
    43  			return "", errors.Wrapf(err, "decode contents of file %s", upstreamFile.FilePath)
    44  		}
    45  
    46  		err = g.Fs.WriteFile(filepath.Join(stateUnpackPath, upstreamFile.FilePath), rawContents, 0755)
    47  		if err != nil {
    48  			return "", errors.Wrapf(err, "write contents of file %s", upstreamFile.FilePath)
    49  		}
    50  	}
    51  
    52  	return stateUnpackPath, nil
    53  }