Created
January 20, 2016 00:16
-
-
Save vishh/80f7e5da847c6eb93f86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Godep restore wrapper | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"strings" | |
"github.com/golang/glog" | |
) | |
func updateGitRev(pkg, rev string) error { | |
err := os.Chdir(fmt.Sprintf("%s/src/%s", os.Getenv("GOPATH"), pkg)) | |
if err != nil { | |
return fmt.Errorf("failed to chdir into package %q - %v", pkg, err) | |
} | |
out, err := exec.Command("git", "checkout", rev).CombinedOutput() | |
if err != nil { | |
return fmt.Errorf("failed to checkout revision %q for package %q. Out - %v, Err- %v", rev, pkg, string(out), err) | |
} | |
glog.V(1).Infof("Updated Package %q to revision %q", pkg, rev) | |
return nil | |
} | |
type Godeps struct { | |
Deps []Godep `"json:Deps"` | |
} | |
type Godep struct { | |
ImportPath string `"json:ImportPath"` | |
Rev string `"json:Rev"` | |
} | |
func godepRestore() error { | |
out, err := os.Command("godep", "restore").CombinedOutput() | |
if err != nil { | |
return fmt.Errorf("godep restore failed. Out - %v, Err - %v", out, err) | |
} | |
return nil | |
} | |
func main() { | |
cwd, err := os.Getwd() | |
if err != nil { | |
glog.Fatal(err) | |
} | |
godepsFile := os.Args[1] | |
if godepsFile == "" { | |
glog.Fatalf("Usage: %s <path to Godeps File>", os.Args[0]) | |
} | |
if err := godepRestore(); err != nil { | |
glog.Fatalf(err) | |
} | |
out, err := ioutil.ReadFile(godepsFile) | |
if err != nil { | |
glog.Fatal(err) | |
} | |
var godeps Godeps | |
if err = json.Unmarshal(out, &godeps); err != nil { | |
glog.Fatal(err) | |
} | |
for _, dep := range godeps.Deps { | |
if !strings.Contains(strings.Split(dep.ImportPath, "/")[0], "git") { | |
continue | |
} | |
if err := updateGitRev(dep.ImportPath, dep.Rev); err != nil { | |
glog.Errorf("%s\n", err) | |
} | |
} | |
glog.Info("updated all dependencies to the revisions in Godeps") | |
if err := os.Chdir(cwd); err != nil { | |
glog.Fatalf("failed to restore working directory to %q - %v", cwd, err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment