github.com/sc0rp1us/gb@v0.4.1-0.20160319180011-4ba8cf1baa5a/cmd/gb-vendor/restore.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "path/filepath" 7 8 "github.com/constabulary/gb" 9 "github.com/constabulary/gb/cmd" 10 "github.com/constabulary/gb/fileutils" 11 "github.com/constabulary/gb/vendor" 12 ) 13 14 func addRestoreFlags(fs *flag.FlagSet) { 15 fs.BoolVar(&insecure, "precaire", false, "allow the use of insecure protocols") 16 } 17 18 var cmdRestore = &cmd.Command{ 19 Name: "restore", 20 UsageLine: "restore [-precaire]", 21 Short: "restore dependencies from the manifest", 22 Long: `Restore vendor dependecies. 23 24 Flags: 25 -precaire 26 allow the use of insecure protocols. 27 28 `, 29 Run: func(ctx *gb.Context, args []string) error { 30 return restore(ctx) 31 }, 32 AddFlags: addRestoreFlags, 33 } 34 35 func restore(ctx *gb.Context) error { 36 m, err := vendor.ReadManifest(manifestFile(ctx)) 37 if err != nil { 38 return fmt.Errorf("could not load manifest: %v", err) 39 } 40 41 for _, dep := range m.Dependencies { 42 fmt.Printf("Getting %s\n", dep.Importpath) 43 repo, _, err := vendor.DeduceRemoteRepo(dep.Importpath, insecure) 44 if err != nil { 45 return fmt.Errorf("Could not process dependency: %s", err) 46 } 47 wc, err := repo.Checkout("", "", dep.Revision) 48 if err != nil { 49 return fmt.Errorf("Could not retrieve dependency: %s", err) 50 } 51 dst := filepath.Join(ctx.Projectdir(), "vendor", "src", dep.Importpath) 52 src := filepath.Join(wc.Dir(), dep.Path) 53 54 if err := fileutils.Copypath(dst, src); err != nil { 55 return err 56 } 57 58 if err := wc.Destroy(); err != nil { 59 return err 60 } 61 62 } 63 return nil 64 }