github.com/vijayrajah/packer@v1.3.2/post-processor/artifice/post-processor.go (about)

     1  package artifice
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/packer/common"
     8  	"github.com/hashicorp/packer/helper/config"
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/hashicorp/packer/template/interpolate"
    11  )
    12  
    13  // The artifact-override post-processor allows you to specify arbitrary files as
    14  // artifacts. These will override any other artifacts created by the builder.
    15  // This allows you to use a builder and provisioner to create some file, such as
    16  // a compiled binary or tarball, extract it from the builder (VM or container)
    17  // and then save that binary or tarball and throw away the builder.
    18  
    19  type Config struct {
    20  	common.PackerConfig `mapstructure:",squash"`
    21  
    22  	Files []string `mapstructure:"files"`
    23  	Keep  bool     `mapstructure:"keep_input_artifact"`
    24  
    25  	ctx interpolate.Context
    26  }
    27  
    28  type PostProcessor struct {
    29  	config Config
    30  }
    31  
    32  func (p *PostProcessor) Configure(raws ...interface{}) error {
    33  	err := config.Decode(&p.config, &config.DecodeOpts{
    34  		Interpolate:        true,
    35  		InterpolateContext: &p.config.ctx,
    36  		InterpolateFilter: &interpolate.RenderFilter{
    37  			Exclude: []string{},
    38  		},
    39  	}, raws...)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	if len(p.config.Files) == 0 {
    45  		return fmt.Errorf("No files specified in artifice configuration")
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
    52  	if len(artifact.Files()) > 0 {
    53  		ui.Say(fmt.Sprintf("Discarding artifact files: %s", strings.Join(artifact.Files(), ", ")))
    54  	}
    55  
    56  	artifact, err := NewArtifact(p.config.Files)
    57  	ui.Say(fmt.Sprintf("Using these artifact files: %s", strings.Join(artifact.Files(), ", ")))
    58  
    59  	return artifact, true, err
    60  }