github.com/alde/glide@v0.0.0-20160309204240-d5fc6b676a75/action/install.go (about)

     1  package action
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	"github.com/Masterminds/glide/cfg"
     8  	"github.com/Masterminds/glide/dependency"
     9  	"github.com/Masterminds/glide/msg"
    10  	gpath "github.com/Masterminds/glide/path"
    11  	"github.com/Masterminds/glide/repo"
    12  )
    13  
    14  // Install installs a vendor directory based on an existing Glide configuration.
    15  func Install(installer *repo.Installer) {
    16  	base := "."
    17  	// Ensure GOPATH
    18  	EnsureGopath()
    19  	EnsureVendorDir()
    20  	conf := EnsureConfig()
    21  
    22  	// Lockfile exists
    23  	if !gpath.HasLock(base) {
    24  		msg.Info("Lock file (glide.lock) does not exist. Performing update.")
    25  		Update(installer, false)
    26  		return
    27  	}
    28  	// Load lockfile
    29  	lock, err := LoadLockfile(base, conf)
    30  	if err != nil {
    31  		msg.Die("Could not load lockfile.")
    32  	}
    33  
    34  	// Delete unused packages
    35  	if installer.DeleteUnused {
    36  		// It's unclear whether this should operate off of the lock, or off
    37  		// of the glide.yaml file. I'd think that doing this based on the
    38  		// lock would be much more reliable.
    39  		dependency.DeleteUnused(conf)
    40  	}
    41  
    42  	// Install
    43  	newConf, err := installer.Install(lock, conf)
    44  	if err != nil {
    45  		msg.Die("Failed to install: %s", err)
    46  	}
    47  
    48  	msg.Info("Setting references.")
    49  
    50  	// Set reference
    51  	if err := repo.SetReference(newConf); err != nil {
    52  		msg.Err("Failed to set references: %s (Skip to cleanup)", err)
    53  	}
    54  
    55  	// VendoredCleanup. This should ONLY be run if UpdateVendored was specified.
    56  	if installer.UpdateVendored {
    57  		repo.VendoredCleanup(newConf)
    58  	}
    59  }
    60  
    61  // LoadLockfile loads the contents of a glide.lock file.
    62  //
    63  // TODO: This should go in another package.
    64  func LoadLockfile(base string, conf *cfg.Config) (*cfg.Lockfile, error) {
    65  	yml, err := ioutil.ReadFile(filepath.Join(base, gpath.LockFile))
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	lock, err := cfg.LockfileFromYaml(yml)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	hash, err := conf.Hash()
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	if hash != lock.Hash {
    80  		msg.Warn("Lock file may be out of date. Hash check of YAML failed. You may need to run 'update'")
    81  	}
    82  
    83  	return lock, nil
    84  }