github.com/sneal/packer@v0.5.2/builder/vmware/iso/output_dir.go (about)

     1  package iso
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  )
     7  
     8  // OutputDir is an interface type that abstracts the creation and handling
     9  // of the output directory for VMware-based products. The abstraction is made
    10  // so that the output directory can be properly made on remote (ESXi) based
    11  // VMware products as well as local.
    12  type OutputDir interface {
    13  	DirExists() (bool, error)
    14  	ListFiles() ([]string, error)
    15  	MkdirAll() error
    16  	Remove(string) error
    17  	RemoveAll() error
    18  	SetOutputDir(string)
    19  }
    20  
    21  // localOutputDir is an OutputDir implementation where the directory
    22  // is on the local machine.
    23  type localOutputDir struct {
    24  	dir string
    25  }
    26  
    27  func (d *localOutputDir) DirExists() (bool, error) {
    28  	_, err := os.Stat(d.dir)
    29  	return err == nil, nil
    30  }
    31  
    32  func (d *localOutputDir) ListFiles() ([]string, error) {
    33  	files := make([]string, 0, 10)
    34  
    35  	visit := func(path string, info os.FileInfo, err error) error {
    36  		if err != nil {
    37  			return err
    38  		}
    39  		if !info.IsDir() {
    40  			files = append(files, path)
    41  		}
    42  		return nil
    43  	}
    44  
    45  	return files, filepath.Walk(d.dir, visit)
    46  }
    47  
    48  func (d *localOutputDir) MkdirAll() error {
    49  	return os.MkdirAll(d.dir, 0755)
    50  }
    51  
    52  func (d *localOutputDir) Remove(path string) error {
    53  	return os.Remove(path)
    54  }
    55  
    56  func (d *localOutputDir) RemoveAll() error {
    57  	return os.RemoveAll(d.dir)
    58  }
    59  
    60  func (d *localOutputDir) SetOutputDir(path string) {
    61  	d.dir = path
    62  }
    63  
    64  func (d *localOutputDir) String() string {
    65  	return d.dir
    66  }