get.porter.sh/porter@v1.3.0/pkg/exec/builder/output_file.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"get.porter.sh/porter/pkg/runtime"
     8  	"get.porter.sh/porter/pkg/tracing"
     9  )
    10  
    11  type OutputFile interface {
    12  	Output
    13  	GetFilePath() string
    14  }
    15  
    16  // ProcessFileOutputs makes the contents of a file specified by any OutputFile interface available as an output.
    17  func ProcessFileOutputs(ctx context.Context, cfg runtime.RuntimeConfig, step StepWithOutputs) error {
    18  	_, span := tracing.StartSpan(ctx)
    19  	defer span.EndSpan()
    20  
    21  	outputs := step.GetOutputs()
    22  
    23  	if len(outputs) == 0 {
    24  		return nil
    25  	}
    26  
    27  	for _, o := range outputs {
    28  		output, ok := o.(OutputFile)
    29  		if !ok {
    30  			continue
    31  		}
    32  
    33  		outputName := output.GetName()
    34  		outputPath := output.GetFilePath()
    35  		if outputPath == "" {
    36  			continue
    37  		}
    38  
    39  		span.Debugf("Processing file output %s...", outputName)
    40  
    41  		valueB, err := cfg.FileSystem.ReadFile(outputPath)
    42  		if err != nil {
    43  			return fmt.Errorf("error evaluating filepath %q for output %q: %w", outputPath, outputName, err)
    44  		}
    45  
    46  		err = cfg.WriteMixinOutputToFile(outputName, valueB)
    47  		if err != nil {
    48  			return fmt.Errorf("error writing mixin output for %q: %w", outputName, err)
    49  		}
    50  	}
    51  
    52  	return nil
    53  }