Last active
September 25, 2017 04:15
-
-
Save ariefrahmansyah/0fa4a0db291eccccd80362ebe285c705 to your computer and use it in GitHub Desktop.
Simple tool to migrate kardianos's govendor to golang's dep
This file contains hidden or 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
import ( | |
"io/ioutil" | |
"log" | |
"sort" | |
vendorfile "github.com/kardianos/govendor/vendorfile" | |
) | |
func readFile(file string) (string, error) { | |
data, err := ioutil.ReadFile(file) | |
if err != nil { | |
return "", err | |
} | |
return string(data), nil | |
} | |
func writeFile(file string, text string) error { | |
err := ioutil.WriteFile(file, []byte(text), 0644) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
type PackageSorter []*vendorfile.Package | |
func (pkgs PackageSorter) Len() int { return len(pkgs) } | |
func (pkgs PackageSorter) Swap(i, j int) { pkgs[i], pkgs[j] = pkgs[j], pkgs[i] } | |
func (pkgs PackageSorter) Less(i, j int) bool { return pkgs[i].Path < pkgs[j].Path } | |
func main() { | |
// read from vendor.json | |
vendorString, err := readFile("vendor/vendor.json") | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
vf := &vendorfile.File{} | |
err = vf.Unmarshal(strings.NewReader(vendorString)) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
sort.Sort(PackageSorter(vf.Package)) | |
var output string | |
for _, pkg := range vf.Package { | |
output += "[[constraint]]\n" | |
output += " name = \"" + pkg.Path + "\"\n" | |
if pkg.Revision != "" { | |
output += " revision = \"" + pkg.Revision + "\"\n" | |
} else if pkg.Version != "" { | |
output += " version = \"" + pkg.Version + "\"\n" | |
} | |
output += "\n" | |
} | |
manifest := "Gopkg.toml" | |
if err := writeFile(manifest, output); err != nil { | |
log.Println(err) | |
return | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment