github.com/mitchellh/packer@v1.3.2/builder/virtualbox/common/artifact.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  // This is the common builder ID to all of these artifacts.
    12  const BuilderId = "mitchellh.virtualbox"
    13  
    14  // Artifact is the result of running the VirtualBox builder, namely a set
    15  // of files associated with the resulting machine.
    16  type artifact struct {
    17  	dir string
    18  	f   []string
    19  }
    20  
    21  // NewArtifact returns a VirtualBox artifact containing the files
    22  // in the given directory.
    23  func NewArtifact(dir string) (packer.Artifact, error) {
    24  	files := make([]string, 0, 5)
    25  	visit := func(path string, info os.FileInfo, err error) error {
    26  		if err != nil {
    27  			return err
    28  		}
    29  		if !info.IsDir() {
    30  			files = append(files, path)
    31  		}
    32  
    33  		return err
    34  	}
    35  
    36  	if err := filepath.Walk(dir, visit); err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return &artifact{
    41  		dir: dir,
    42  		f:   files,
    43  	}, nil
    44  }
    45  
    46  func (*artifact) BuilderId() string {
    47  	return BuilderId
    48  }
    49  
    50  func (a *artifact) Files() []string {
    51  	return a.f
    52  }
    53  
    54  func (*artifact) Id() string {
    55  	return "VM"
    56  }
    57  
    58  func (a *artifact) String() string {
    59  	return fmt.Sprintf("VM files in directory: %s", a.dir)
    60  }
    61  
    62  func (a *artifact) State(name string) interface{} {
    63  	return nil
    64  }
    65  
    66  func (a *artifact) Destroy() error {
    67  	return os.RemoveAll(a.dir)
    68  }