github.com/n0needt0/glide@v0.0.0-20160325160517-844a77136d85/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, strip, stripVendor bool) {
    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, strip, stripVendor)
    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  	// When stripping VCS happens this will happen as well. No need for double
    57  	// effort.
    58  	if installer.UpdateVendored && !strip {
    59  		repo.VendoredCleanup(newConf)
    60  	}
    61  
    62  	if strip {
    63  		msg.Info("Removing version control data from vendor directory...")
    64  		gpath.StripVcs()
    65  	}
    66  
    67  	if stripVendor {
    68  		msg.Info("Removing nested vendor and Godeps/_workspace directories...")
    69  		err := gpath.StripVendor()
    70  		if err != nil {
    71  			msg.Err("Unable to strip vendor directories: %s", err)
    72  		}
    73  	}
    74  }
    75  
    76  // LoadLockfile loads the contents of a glide.lock file.
    77  //
    78  // TODO: This should go in another package.
    79  func LoadLockfile(base string, conf *cfg.Config) (*cfg.Lockfile, error) {
    80  	yml, err := ioutil.ReadFile(filepath.Join(base, gpath.LockFile))
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	lock, err := cfg.LockfileFromYaml(yml)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	hash, err := conf.Hash()
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	if hash != lock.Hash {
    95  		msg.Warn("Lock file may be out of date. Hash check of YAML failed. You may need to run 'update'")
    96  	}
    97  
    98  	return lock, nil
    99  }