github.com/kekek/gb@v0.4.5-0.20170222120241-d4ba64b0b297/cmd/gb-vendor/restore.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"path/filepath"
     7  	"sync"
     8  
     9  	"github.com/constabulary/gb"
    10  	"github.com/constabulary/gb/cmd"
    11  	"github.com/constabulary/gb/internal/fileutils"
    12  	"github.com/constabulary/gb/internal/vendor"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func addRestoreFlags(fs *flag.FlagSet) {
    17  	fs.BoolVar(&insecure, "precaire", false, "allow the use of insecure protocols")
    18  }
    19  
    20  var cmdRestore = &cmd.Command{
    21  	Name:      "restore",
    22  	UsageLine: "restore [-precaire]",
    23  	Short:     "restore dependencies from the manifest",
    24  	Long: `Restore vendor dependencies.
    25  
    26  Flags:
    27  	-precaire
    28  		allow the use of insecure protocols.
    29  
    30  `,
    31  	Run: func(ctx *gb.Context, args []string) error {
    32  		return restore(ctx)
    33  	},
    34  	AddFlags: addRestoreFlags,
    35  }
    36  
    37  func restore(ctx *gb.Context) error {
    38  	m, err := vendor.ReadManifest(manifestFile(ctx))
    39  	if err != nil {
    40  		return errors.Wrap(err, "could not load manifest")
    41  	}
    42  
    43  	errChan := make(chan error, 1)
    44  	var wg sync.WaitGroup
    45  
    46  	wg.Add(len(m.Dependencies))
    47  	for _, dep := range m.Dependencies {
    48  		go func(dep vendor.Dependency) {
    49  			defer wg.Done()
    50  			fmt.Printf("Getting %s\n", dep.Importpath)
    51  			repo, _, err := vendor.DeduceRemoteRepo(dep.Importpath, insecure)
    52  			if err != nil {
    53  				errChan <- errors.Wrap(err, "could not process dependency")
    54  				return
    55  			}
    56  			wc, err := repo.Checkout("", "", dep.Revision)
    57  			if err != nil {
    58  				errChan <- errors.Wrap(err, "could not retrieve dependency")
    59  				return
    60  			}
    61  			dst := filepath.Join(ctx.Projectdir(), "vendor", "src", dep.Importpath)
    62  			src := filepath.Join(wc.Dir(), dep.Path)
    63  
    64  			if err := fileutils.Copypath(dst, src); err != nil {
    65  				errChan <- err
    66  				return
    67  			}
    68  
    69  			if err := wc.Destroy(); err != nil {
    70  				errChan <- err
    71  				return
    72  			}
    73  		}(dep)
    74  
    75  	}
    76  
    77  	wg.Wait()
    78  	close(errChan)
    79  	return <-errChan
    80  }