github.com/myles-mcdonnell/glide@v0.12.3/repo/set_reference.go (about)

     1  package repo
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/Masterminds/glide/cache"
     7  	"github.com/Masterminds/glide/cfg"
     8  	"github.com/Masterminds/glide/msg"
     9  	"github.com/codegangsta/cli"
    10  )
    11  
    12  // SetReference is a command to set the VCS reference (commit id, tag, etc) for
    13  // a project.
    14  func SetReference(conf *cfg.Config, resolveTest bool) error {
    15  
    16  	if len(conf.Imports) == 0 && len(conf.DevImports) == 0 {
    17  		msg.Info("No references set.\n")
    18  		return nil
    19  	}
    20  
    21  	done := make(chan struct{}, concurrentWorkers)
    22  	in := make(chan *cfg.Dependency, concurrentWorkers)
    23  	var wg sync.WaitGroup
    24  	var lock sync.Mutex
    25  	var returnErr error
    26  
    27  	for i := 0; i < concurrentWorkers; i++ {
    28  		go func(ch <-chan *cfg.Dependency) {
    29  			for {
    30  				select {
    31  				case dep := <-ch:
    32  
    33  					var loc string
    34  					if dep.Repository != "" {
    35  						loc = dep.Repository
    36  					} else {
    37  						loc = "https://" + dep.Name
    38  					}
    39  					key, err := cache.Key(loc)
    40  					if err != nil {
    41  						msg.Die(err.Error())
    42  					}
    43  					cache.Lock(key)
    44  					if err := VcsVersion(dep); err != nil {
    45  						msg.Err("Failed to set version on %s to %s: %s\n", dep.Name, dep.Reference, err)
    46  
    47  						// Capture the error while making sure the concurrent
    48  						// operations don't step on each other.
    49  						lock.Lock()
    50  						if returnErr == nil {
    51  							returnErr = err
    52  						} else {
    53  							returnErr = cli.NewMultiError(returnErr, err)
    54  						}
    55  						lock.Unlock()
    56  					}
    57  					cache.Unlock(key)
    58  					wg.Done()
    59  				case <-done:
    60  					return
    61  				}
    62  			}
    63  		}(in)
    64  	}
    65  
    66  	for _, dep := range conf.Imports {
    67  		if !conf.HasIgnore(dep.Name) {
    68  			wg.Add(1)
    69  			in <- dep
    70  		}
    71  	}
    72  
    73  	if resolveTest {
    74  		for _, dep := range conf.DevImports {
    75  			if !conf.HasIgnore(dep.Name) {
    76  				wg.Add(1)
    77  				in <- dep
    78  			}
    79  		}
    80  	}
    81  
    82  	wg.Wait()
    83  	// Close goroutines setting the version
    84  	for i := 0; i < concurrentWorkers; i++ {
    85  		done <- struct{}{}
    86  	}
    87  	// close(done)
    88  	// close(in)
    89  
    90  	return returnErr
    91  }