2017-10-21 10:56:53 -05:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
const manifestName = "modules.json"
|
|
|
|
|
2017-10-21 10:56:53 -05:00
|
|
|
// moduleManifest is the serialization structure used to record the stored
|
|
|
|
// module's metadata.
|
|
|
|
type moduleManifest struct {
|
|
|
|
Modules []moduleRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
// moduleRecords represents the stored module's metadata.
|
|
|
|
// This is compared for equality using '==', so all fields needs to remain
|
|
|
|
// comparable.
|
|
|
|
type moduleRecord struct {
|
|
|
|
// Source is the module source string, minus any subdirectory.
|
|
|
|
// If it is sourced from a registry, it will include the hostname if it is
|
|
|
|
// supplied in configuration.
|
|
|
|
Source string
|
|
|
|
|
|
|
|
// Version is the exact version string that is stored in this Key.
|
|
|
|
Version string
|
|
|
|
|
|
|
|
// Dir is the directory name returned by the FileStorage. This is what
|
|
|
|
// allows us to correlate a particular module version with the location on
|
|
|
|
// disk.
|
|
|
|
Dir string
|
|
|
|
|
|
|
|
// Root is the root directory containing the module. If the module is
|
|
|
|
// unpacked from an archive, and not located in the root directory, this is
|
|
|
|
// used to direct the loader to the correct subdirectory. This is
|
|
|
|
// independent from any subdirectory in the original source string, which
|
|
|
|
// may traverse further into the module tree.
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
// moduleStorgae implements methods to record and fetch metadata about the
|
|
|
|
// modules that have been fetched and stored locally. The getter.Storgae
|
|
|
|
// abstraction doesn't provide the information needed to know which versions of
|
|
|
|
// a module have been stored, or their location.
|
|
|
|
type moduleStorage struct {
|
|
|
|
storageDir string
|
2017-10-21 10:56:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadManifest returns the moduleManifest file from the parent directory.
|
2017-10-23 10:19:24 -05:00
|
|
|
func (m moduleStorage) loadManifest() (moduleManifest, error) {
|
2017-10-21 10:56:53 -05:00
|
|
|
manifest := moduleManifest{}
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
manifestPath := filepath.Join(m.storageDir, manifestName)
|
2017-10-21 10:56:53 -05:00
|
|
|
data, err := ioutil.ReadFile(manifestPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return manifest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &manifest); err != nil {
|
|
|
|
return manifest, err
|
|
|
|
}
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the location of the module, along with the version used and the module
|
|
|
|
// root directory. The storage method loads the entire file and rewrites it
|
|
|
|
// each time. This is only done a few times during init, so efficiency is
|
|
|
|
// not a concern.
|
2017-10-23 10:19:24 -05:00
|
|
|
func (m moduleStorage) recordModule(rec moduleRecord) error {
|
|
|
|
manifest, err := m.loadManifest()
|
2017-10-21 10:56:53 -05:00
|
|
|
if err != nil {
|
|
|
|
// if there was a problem with the file, we will attempt to write a new
|
|
|
|
// one. Any non-data related error should surface there.
|
2017-10-23 10:19:24 -05:00
|
|
|
log.Printf("[WARN] error reading module manifest: %s", err)
|
2017-10-21 10:56:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// do nothing if we already have the exact module
|
|
|
|
for i, stored := range manifest.Modules {
|
2017-10-23 10:19:24 -05:00
|
|
|
if rec == stored {
|
2017-10-21 10:56:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// they are not equal, but if the storage path is the same we need to
|
2017-10-23 10:19:24 -05:00
|
|
|
// remove this rec to be replaced.
|
|
|
|
if rec.Dir == stored.Dir {
|
2017-10-21 10:56:53 -05:00
|
|
|
manifest.Modules[i] = manifest.Modules[len(manifest.Modules)-1]
|
|
|
|
manifest.Modules = manifest.Modules[:len(manifest.Modules)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
manifest.Modules = append(manifest.Modules, rec)
|
2017-10-21 10:56:53 -05:00
|
|
|
|
|
|
|
js, err := json.Marshal(manifest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
manifestPath := filepath.Join(m.storageDir, manifestName)
|
2017-10-21 10:56:53 -05:00
|
|
|
return ioutil.WriteFile(manifestPath, js, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return only the root directory of the module stored in dir.
|
2017-10-23 10:19:24 -05:00
|
|
|
func (m moduleStorage) getModuleRoot(dir string) (string, error) {
|
|
|
|
manifest, err := m.loadManifest()
|
2017-10-21 10:56:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
for _, mod := range manifest.Modules {
|
|
|
|
if mod.Dir == dir {
|
|
|
|
return mod.Root, nil
|
2017-10-21 10:56:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// record only the Root directory for the module stored at dir.
|
|
|
|
// TODO: remove this compatibility function to store the full moduleRecord.
|
2017-10-23 10:19:24 -05:00
|
|
|
func (m moduleStorage) recordModuleRoot(dir, root string) error {
|
|
|
|
rec := moduleRecord{
|
2017-10-21 10:56:53 -05:00
|
|
|
Dir: dir,
|
|
|
|
Root: root,
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:19:24 -05:00
|
|
|
return m.recordModule(rec)
|
2017-10-21 10:56:53 -05:00
|
|
|
}
|