Created
June 22, 2015 03:41
-
-
Save siddontang/804e258ba46867ab6c75 to your computer and use it in GitHub Desktop.
generate import list from Godeps.json
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
package main | |
// This program will read the Godeps.json and generate import list | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"sort" | |
) | |
var path = flag.String("f", "Godeps.json", "Godeps.json file") | |
type item struct { | |
ImportPath string `json:"ImportPath"` | |
Comment string `json:"Comment"` | |
Rev string `json:"Rev"` | |
} | |
type info struct { | |
ImportPath string `json:"ImportPath"` | |
GoVersion string `json:"GoVersion"` | |
Packages []string `json:"GoVersion"` | |
Deps []item `json:"Deps"` | |
} | |
func main() { | |
flag.Parse() | |
f, err := os.Open(*path) | |
if err != nil { | |
fmt.Println(err.Error()) | |
return | |
} | |
defer f.Close() | |
data, err := ioutil.ReadAll(f) | |
if err != nil { | |
fmt.Printf("read %s err %v", *path, err) | |
return | |
} | |
var v info | |
if err = json.Unmarshal(data, &v); err != nil { | |
fmt.Printf("Unmarshal %s json data err %v", *path, err) | |
return | |
} | |
l := make([]string, 0, len(v.Deps)) | |
for _, i := range v.Deps { | |
l = append(l, i.ImportPath) | |
} | |
sort.Strings(l) | |
for _, i := range l { | |
println(i) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment