Skip to content

Instantly share code, notes, and snippets.

@dcarney
Created June 8, 2015 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcarney/8b28fb753c50bab1a233 to your computer and use it in GitHub Desktop.
Save dcarney/8b28fb753c50bab1a233 to your computer and use it in GitHub Desktop.
Vendors a set of go dependencies using 'gb-vendor', by reading a godep manifest (e.g. Godeps.json)
// aids in converting a godep project to a gb project:
//
// reads a Godeps.json manifest and uses the 'gb-vendor' plugin to vendor the
// same revision of each dependency into a gb-based project.
//
// Example:
// To vendor all the dependencies of the godep-based project at /go/src/github.com/username/foo
// into the new gb-based project at /path/to/gb/proj, run the following:
//
// $ go run godep2gb.go -gbroot=/path/to/gb/proj/ -godeps=/go/src/github.com/username/foo/Godeps/Godeps.json
//
// Note that this script will fail if any of the deps have already been vendored using gb-vendor.
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os/exec"
)
var (
gbRoot string
godepsPath string
)
type dep struct {
ImportPath string `json:"ImportPath"`
Rev string `json:"Rev"`
}
type godepsManifest struct {
Deps []dep `json:"Deps"`
}
func main() {
flag.StringVar(&gbRoot, "gbroot", "", "the project root for the gb project")
flag.StringVar(&godepsPath, "godeps", "", "path to the Godeps.json file to read")
flag.Parse()
var godeps godepsManifest
file, err := ioutil.ReadFile(godepsPath)
if err != nil {
panic(err)
}
if err := json.Unmarshal(file, &godeps); err != nil {
panic(err)
}
for _, dep := range godeps.Deps {
fmt.Printf("vendoring pkg %s, rev %s...\n", dep.ImportPath, dep.Rev)
cmd := exec.Command("gb", "vendor", "fetch", "--revision", dep.Rev, dep.ImportPath)
cmd.Dir = gbRoot
byts, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(byts))
panic(err)
}
fmt.Println(string(byts))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment