|
|
|
@ -15,11 +15,9 @@
|
|
|
|
|
package cmd |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"os" |
|
|
|
|
"path" |
|
|
|
|
"path/filepath" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
"github.com/Unknwon/com" |
|
|
|
@ -31,11 +29,13 @@ import (
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
installRepoPath string // The path of gopm local repository.
|
|
|
|
|
installGopath string // The first path in the GOPATH.
|
|
|
|
|
downloadCache map[string]bool // Saves packages that have been downloaded.
|
|
|
|
|
downloadCount int |
|
|
|
|
failConut int |
|
|
|
|
installRepoPath string // The path of gopm local repository.
|
|
|
|
|
installGopath string // The first path in the GOPATH.
|
|
|
|
|
isHasGopath bool // Indicates whether system has GOPATH.
|
|
|
|
|
|
|
|
|
|
downloadCache map[string]bool // Saves packages that have been downloaded.
|
|
|
|
|
downloadCount int |
|
|
|
|
failConut int |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var CmdGet = cli.Command{ |
|
|
|
@ -50,16 +50,16 @@ gopm get <package name>@[<tag|commit|branch>:<value>]
|
|
|
|
|
|
|
|
|
|
Can specify one or more: gopm get beego@tag:v0.9.0 github.com/beego/bee |
|
|
|
|
|
|
|
|
|
If no argument is supplied, then gopmfile must be present. |
|
|
|
|
If no version specified and package exists in GOPATH, |
|
|
|
|
it will be skipped unless user enabled '--remote, -r' option
|
|
|
|
|
then all the packages go into gopm local repository.`, |
|
|
|
|
Action: runGet, |
|
|
|
|
Flags: []cli.Flag{ |
|
|
|
|
cli.BoolFlag{"gopath, g", "download all pakcages to GOPATH"}, |
|
|
|
|
cli.BoolFlag{"force, f", "force to update pakcage(s) and dependencies"}, |
|
|
|
|
cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"}, |
|
|
|
|
cli.BoolFlag{"example, e", "download dependencies for example folder"}, |
|
|
|
|
cli.BoolFlag{"remote, r", "download all pakcages to gopm local repository"}, |
|
|
|
|
cli.BoolFlag{"verbose, v", "show process details"}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -68,8 +68,7 @@ func init() {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func runGet(ctx *cli.Context) { |
|
|
|
|
log.PureMode = ctx.GlobalBool("noterm") |
|
|
|
|
log.Verbose = ctx.GlobalBool("verbose") |
|
|
|
|
setup(ctx) |
|
|
|
|
|
|
|
|
|
// Check conflicts.
|
|
|
|
|
if ctx.Bool("gopath") && ctx.Bool("remote") { |
|
|
|
@ -82,85 +81,62 @@ func runGet(ctx *cli.Context) {
|
|
|
|
|
if !ctx.Bool("remote") { |
|
|
|
|
// Get GOPATH.
|
|
|
|
|
installGopath = com.GetGOPATHs()[0] |
|
|
|
|
if !com.IsDir(installGopath) { |
|
|
|
|
log.Error("get", "Invalid GOPATH path") |
|
|
|
|
log.Error("", "GOPATH does not exist or is not a directory:") |
|
|
|
|
log.Error("", "\t"+installGopath) |
|
|
|
|
log.Help("Try 'go help gopath' to get more information") |
|
|
|
|
if com.IsDir(installGopath) { |
|
|
|
|
isHasGopath = true |
|
|
|
|
log.Log("Indicated GOPATH: %s", installGopath) |
|
|
|
|
installGopath += "/src" |
|
|
|
|
} else { |
|
|
|
|
if ctx.Bool("gopath") { |
|
|
|
|
log.Error("get", "Invalid GOPATH path") |
|
|
|
|
log.Error("", "GOPATH does not exist or is not a directory:") |
|
|
|
|
log.Error("", "\t"+installGopath) |
|
|
|
|
log.Help("Try 'go help gopath' to get more information") |
|
|
|
|
} else { |
|
|
|
|
// It's OK that no GOPATH setting
|
|
|
|
|
// when user does not specify to use.
|
|
|
|
|
log.Warn("No GOPATH setting available") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
log.Log("Indicated GOPATH: %s", installGopath) |
|
|
|
|
installGopath += "/src" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// The gopm local repository.
|
|
|
|
|
installRepoPath = doc.HomeDir + "/repos" |
|
|
|
|
log.Log("Local repository path: %s", installRepoPath) |
|
|
|
|
|
|
|
|
|
// Check number of arguments.
|
|
|
|
|
// Check number of arguments to decide which function to call.
|
|
|
|
|
switch len(ctx.Args()) { |
|
|
|
|
case 0: |
|
|
|
|
getByGopmfile(ctx) |
|
|
|
|
default: |
|
|
|
|
getByPath(ctx) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getByGopmfile(ctx *cli.Context) { |
|
|
|
|
// Check if gopmfile exists and generate one if not.
|
|
|
|
|
if !com.IsFile(".gopmfile") { |
|
|
|
|
log.Error("get", "Gopmfile not found") |
|
|
|
|
log.Error("", "No argument is supplied and no gopmfile exists") |
|
|
|
|
log.Help("\n%s\n%s\n%s", |
|
|
|
|
"Work directory is supposed to have gopmfile when there is no argument supplied", |
|
|
|
|
"Try 'gopm gen' to auto-generate gopmfile", |
|
|
|
|
"Try 'gopm help gen' to get more information") |
|
|
|
|
runGen(ctx) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gf := doc.NewGopmfile(".") |
|
|
|
|
|
|
|
|
|
absPath, err := filepath.Abs(".") |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("get", "Fail to get absolute path of work directory") |
|
|
|
|
log.Fatal("", "\t"+err.Error()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Log("Work directory: %s", absPath) |
|
|
|
|
|
|
|
|
|
// Get dependencies.
|
|
|
|
|
imports := doc.GetAllImports([]string{absPath}, |
|
|
|
|
gf.MustValue("target", "path"), ctx.Bool("example")) |
|
|
|
|
|
|
|
|
|
imports := doc.GetAllImports([]string{workDir}, |
|
|
|
|
parseTarget(gf.MustValue("target", "path")), ctx.Bool("example")) |
|
|
|
|
nodes := make([]*doc.Node, 0, len(imports)) |
|
|
|
|
for _, p := range imports { |
|
|
|
|
node := doc.NewNode(p, p, doc.BRANCH, "", true) |
|
|
|
|
|
|
|
|
|
// Check if user specified the version.
|
|
|
|
|
if v, err := gf.GetValue("deps", p); err == nil && len(v) > 0 { |
|
|
|
|
tp, ver, err := validPath(v) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("get", "Cannot parse dependency version") |
|
|
|
|
log.Error("", err.Error()+":") |
|
|
|
|
log.Error("", "\t"+v) |
|
|
|
|
log.Help("Try 'gopm help get' to get more information") |
|
|
|
|
} |
|
|
|
|
node.Type = tp |
|
|
|
|
node.Value = ver |
|
|
|
|
node.Type, node.Value = validPath(v) |
|
|
|
|
} |
|
|
|
|
nodes = append(nodes, node) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
downloadPackages(ctx, nodes) |
|
|
|
|
doc.SaveLocalNodes() |
|
|
|
|
|
|
|
|
|
if doc.LocalNodes != nil { |
|
|
|
|
if err := goconfig.SaveConfigFile(doc.LocalNodes, |
|
|
|
|
doc.HomeDir+doc.LocalNodesFile); err != nil { |
|
|
|
|
log.Error("get", "Fail to save localnodes.list:") |
|
|
|
|
log.Error("", "\t"+err.Error()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Log("%d package(s) downloaded, %d failed", |
|
|
|
|
downloadCount, failConut) |
|
|
|
|
log.Log("%d package(s) downloaded, %d failed", downloadCount, failConut) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getByPath(ctx *cli.Context) { |
|
|
|
@ -171,13 +147,7 @@ func getByPath(ctx *cli.Context) {
|
|
|
|
|
|
|
|
|
|
if i := strings.Index(info, "@"); i > -1 { |
|
|
|
|
pkgPath = info[:i] |
|
|
|
|
tp, ver, err := validPath(info[i+1:]) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("get", "Cannot parse dependency version") |
|
|
|
|
log.Error("", err.Error()+":") |
|
|
|
|
log.Error("", "\t"+info[i+1:]) |
|
|
|
|
log.Help("Try 'gopm help get' to get more information") |
|
|
|
|
} |
|
|
|
|
tp, ver := validPath(info[i+1:]) |
|
|
|
|
node = doc.NewNode(pkgPath, pkgPath, tp, ver, true) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -190,17 +160,9 @@ func getByPath(ctx *cli.Context) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
downloadPackages(ctx, nodes) |
|
|
|
|
doc.SaveLocalNodes() |
|
|
|
|
|
|
|
|
|
if doc.LocalNodes != nil { |
|
|
|
|
if err := goconfig.SaveConfigFile(doc.LocalNodes, |
|
|
|
|
doc.HomeDir+doc.LocalNodesFile); err != nil { |
|
|
|
|
log.Error("get", "Fail to save localnodes.list:") |
|
|
|
|
log.Error("", "\t"+err.Error()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Log("%d package(s) downloaded, %d failed", |
|
|
|
|
downloadCount, failConut) |
|
|
|
|
log.Log("%d package(s) downloaded, %d failed", downloadCount, failConut) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func copyToGopath(srcPath, destPath string) { |
|
|
|
@ -234,6 +196,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
|
|
|
|
|
|
|
|
|
|
if ctx.Bool("gopath") { |
|
|
|
|
copyToGopath(installPath, gopathDir) |
|
|
|
|
log.Log("Package copied to GOPATH: %s", n.ImportPath) |
|
|
|
|
} |
|
|
|
|
continue |
|
|
|
|
} else { |
|
|
|
@ -241,7 +204,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !downloadCache[n.ImportPath] { |
|
|
|
|
if !downloadCache[n.RootPath] { |
|
|
|
|
// Download package.
|
|
|
|
|
nod, imports := downloadPackage(ctx, n) |
|
|
|
|
if len(imports) > 0 { |
|
|
|
@ -249,7 +212,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
|
|
|
|
|
|
|
|
|
|
// Check if has gopmfile
|
|
|
|
|
if com.IsFile(installPath + "/" + doc.GOPM_FILE_NAME) { |
|
|
|
|
log.Log("Found gopmgile: %s@%s:%s", |
|
|
|
|
log.Log("Found gopmfile: %s@%s:%s", |
|
|
|
|
n.ImportPath, n.Type, doc.CheckNodeValue(n.Value)) |
|
|
|
|
|
|
|
|
|
gf = doc.NewGopmfile(installPath) |
|
|
|
@ -268,15 +231,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
|
|
|
|
|
// Check if user specified the version.
|
|
|
|
|
if v, err := gf.GetValue("deps", imports[i]); err == nil && |
|
|
|
|
len(v) > 0 { |
|
|
|
|
tp, ver, err := validPath(v) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("download", "Cannot parse dependency version") |
|
|
|
|
log.Error("", err.Error()+":") |
|
|
|
|
log.Error("", "\t"+v) |
|
|
|
|
log.Help("Try 'gopm help get' to get more information") |
|
|
|
|
} |
|
|
|
|
nodes[i].Type = tp |
|
|
|
|
nodes[i].Value = ver |
|
|
|
|
nodes[i].Type, nodes[i].Value = validPath(v) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
downloadPackages(ctx, nodes) |
|
|
|
@ -296,6 +251,7 @@ func downloadPackages(ctx *cli.Context, nodes []*doc.Node) {
|
|
|
|
|
|
|
|
|
|
if ctx.Bool("gopath") { |
|
|
|
|
copyToGopath(installPath, gopathDir) |
|
|
|
|
log.Log("Package copied to GOPATH: %s", n.ImportPath) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
@ -318,7 +274,7 @@ func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) {
|
|
|
|
|
log.Message("Downloading", fmt.Sprintf("package: %s@%s:%s", |
|
|
|
|
nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value))) |
|
|
|
|
// Mark as donwloaded.
|
|
|
|
|
downloadCache[nod.ImportPath] = true |
|
|
|
|
downloadCache[nod.RootPath] = true |
|
|
|
|
|
|
|
|
|
nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value") |
|
|
|
|
imports, err := doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags)
|
|
|
|
@ -332,29 +288,3 @@ func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) {
|
|
|
|
|
} |
|
|
|
|
return nod, imports |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// validPath checks if the information of the package is valid.
|
|
|
|
|
func validPath(info string) (string, string, error) { |
|
|
|
|
infos := strings.Split(info, ":") |
|
|
|
|
|
|
|
|
|
l := len(infos) |
|
|
|
|
switch { |
|
|
|
|
case l == 1: |
|
|
|
|
return doc.BRANCH, "", nil |
|
|
|
|
case l == 2: |
|
|
|
|
switch infos[1] { |
|
|
|
|
case doc.TRUNK, doc.MASTER, doc.DEFAULT: |
|
|
|
|
infos[1] = "" |
|
|
|
|
} |
|
|
|
|
return infos[0], infos[1], nil |
|
|
|
|
default: |
|
|
|
|
return "", "", errors.New("Invalid version information") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func versionSuffix(value string) string { |
|
|
|
|
if len(value) > 0 { |
|
|
|
|
return "." + value |
|
|
|
|
} |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|