github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/config/module/get.go (about)

     1  package module
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/hashicorp/go-getter"
     8  )
     9  
    10  // GetMode is an enum that describes how modules are loaded.
    11  //
    12  // GetModeLoad says that modules will not be downloaded or updated, they will
    13  // only be loaded from the storage.
    14  //
    15  // GetModeGet says that modules can be initially downloaded if they don't
    16  // exist, but otherwise to just load from the current version in storage.
    17  //
    18  // GetModeUpdate says that modules should be checked for updates and
    19  // downloaded prior to loading. If there are no updates, we load the version
    20  // from disk, otherwise we download first and then load.
    21  type GetMode byte
    22  
    23  const (
    24  	GetModeNone GetMode = iota
    25  	GetModeGet
    26  	GetModeUpdate
    27  )
    28  
    29  // GetCopy is the same as Get except that it downloads a copy of the
    30  // module represented by source.
    31  //
    32  // This copy will omit and dot-prefixed files (such as .git/, .hg/) and
    33  // can't be updated on its own.
    34  func GetCopy(dst, src string) error {
    35  	// Create the temporary directory to do the real Get to
    36  	tmpDir, err := ioutil.TempDir("", "tf")
    37  	if err != nil {
    38  		return err
    39  	}
    40  	// FIXME: This isn't completely safe. Creating and removing our temp path
    41  	//        exposes where to race to inject files.
    42  	if err := os.RemoveAll(tmpDir); err != nil {
    43  		return err
    44  	}
    45  	defer os.RemoveAll(tmpDir)
    46  
    47  	// Get to that temporary dir
    48  	if err := getter.Get(tmpDir, src); err != nil {
    49  		return err
    50  	}
    51  
    52  	// Make sure the destination exists
    53  	if err := os.MkdirAll(dst, 0755); err != nil {
    54  		return err
    55  	}
    56  
    57  	// Copy to the final location
    58  	return copyDir(dst, tmpDir)
    59  }
    60  
    61  func getStorage(s getter.Storage, key string, src string, mode GetMode) (string, bool, error) {
    62  	// Get the module with the level specified if we were told to.
    63  	if mode > GetModeNone {
    64  		if err := s.Get(key, src, mode == GetModeUpdate); err != nil {
    65  			return "", false, err
    66  		}
    67  	}
    68  
    69  	// Get the directory where the module is.
    70  	return s.Dir(key)
    71  }