2014-09-14 02:45:56 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
2015-10-15 22:48:58 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2015-10-15 22:36:58 +02:00
|
|
|
"github.com/hashicorp/go-getter"
|
2014-09-14 02:45:56 +02:00
|
|
|
)
|
|
|
|
|
2015-10-15 22:36:58 +02:00
|
|
|
// GetMode is an enum that describes how modules are loaded.
|
2014-09-14 02:45:56 +02:00
|
|
|
//
|
2015-10-15 22:36:58 +02:00
|
|
|
// GetModeLoad says that modules will not be downloaded or updated, they will
|
|
|
|
// only be loaded from the storage.
|
2014-09-27 01:21:33 +02:00
|
|
|
//
|
2015-10-15 22:36:58 +02:00
|
|
|
// GetModeGet says that modules can be initially downloaded if they don't
|
|
|
|
// exist, but otherwise to just load from the current version in storage.
|
|
|
|
//
|
|
|
|
// GetModeUpdate says that modules should be checked for updates and
|
|
|
|
// downloaded prior to loading. If there are no updates, we load the version
|
|
|
|
// from disk, otherwise we download first and then load.
|
|
|
|
type GetMode byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
GetModeNone GetMode = iota
|
|
|
|
GetModeGet
|
|
|
|
GetModeUpdate
|
|
|
|
)
|
2014-09-27 01:21:33 +02:00
|
|
|
|
2015-10-15 22:48:58 +02:00
|
|
|
// GetCopy is the same as Get except that it downloads a copy of the
|
|
|
|
// module represented by source.
|
|
|
|
//
|
|
|
|
// This copy will omit and dot-prefixed files (such as .git/, .hg/) and
|
|
|
|
// can't be updated on its own.
|
|
|
|
func GetCopy(dst, src string) error {
|
|
|
|
// Create the temporary directory to do the real Get to
|
|
|
|
tmpDir, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-22 16:28:44 +02:00
|
|
|
// FIXME: This isn't completely safe. Creating and removing our temp path
|
|
|
|
// exposes where to race to inject files.
|
2015-10-15 22:48:58 +02:00
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
// Get to that temporary dir
|
|
|
|
if err := getter.Get(tmpDir, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the destination exists
|
|
|
|
if err := os.MkdirAll(dst, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy to the final location
|
|
|
|
return copyDir(dst, tmpDir)
|
|
|
|
}
|
|
|
|
|
2015-10-15 22:36:58 +02:00
|
|
|
func getStorage(s getter.Storage, key string, src string, mode GetMode) (string, bool, error) {
|
|
|
|
// Get the module with the level specified if we were told to.
|
|
|
|
if mode > GetModeNone {
|
|
|
|
if err := s.Get(key, src, mode == GetModeUpdate); err != nil {
|
|
|
|
return "", false, err
|
2014-09-16 08:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 22:36:58 +02:00
|
|
|
// Get the directory where the module is.
|
|
|
|
return s.Dir(key)
|
2014-09-16 08:48:56 +02:00
|
|
|
}
|