github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/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  	if err := os.RemoveAll(tmpDir); err != nil {
    41  		return err
    42  	}
    43  	defer os.RemoveAll(tmpDir)
    44  
    45  	// Get to that temporary dir
    46  	if err := getter.Get(tmpDir, src); err != nil {
    47  		return err
    48  	}
    49  
    50  	// Make sure the destination exists
    51  	if err := os.MkdirAll(dst, 0755); err != nil {
    52  		return err
    53  	}
    54  
    55  	// Copy to the final location
    56  	return copyDir(dst, tmpDir)
    57  }
    58  
    59  func getStorage(s getter.Storage, key string, src string, mode GetMode) (string, bool, error) {
    60  	// Get the module with the level specified if we were told to.
    61  	if mode > GetModeNone {
    62  		if err := s.Get(key, src, mode == GetModeUpdate); err != nil {
    63  			return "", false, err
    64  		}
    65  	}
    66  
    67  	// Get the directory where the module is.
    68  	return s.Dir(key)
    69  }