forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
94 lines (85 loc) · 2.24 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/kardianos/govendor/pkgspec"
"golang.org/x/tools/go/vcs"
)
func Get(logger io.Writer, pkgspecName string, insecure bool) error {
// Get the GOPATHs.
all := os.Getenv("GOPATH")
if len(all) == 0 {
return ErrMissingGOPATH
}
gopathList := filepath.SplitList(all)
gopath := gopathList[0]
cwd, err := os.Getwd()
if err != nil {
return err
}
ps, err := pkgspec.Parse(cwd, pkgspecName)
if err != nil {
return err
}
return get(logger, filepath.Join(gopath, "src"), ps, insecure)
}
func get(logger io.Writer, gopath string, ps *pkgspec.Pkg, insecure bool) error {
pkgDir := filepath.Join(gopath, ps.Path)
sysVcsCmd, repoRoot, err := vcs.FromDir(pkgDir, gopath)
var vcsCmd *VCSCmd
repoRootDir := filepath.Join(gopath, repoRoot)
if err != nil {
rr, err := vcs.RepoRootForImportPath(ps.PathOrigin(), false)
if err != nil {
return err
}
if !insecure && !vcsIsSecure(rr.Repo) {
return fmt.Errorf("repo remote not secure")
}
vcsCmd = updateVcsCmd(rr.VCS)
repoRoot = rr.Root
repoRootDir = filepath.Join(gopath, repoRoot)
err = vcsCmd.Create(repoRootDir, rr.Repo)
if err != nil {
return fmt.Errorf("failed to create repo %q in %q %v", rr.Repo, repoRootDir, err)
}
} else {
vcsCmd = updateVcsCmd(sysVcsCmd)
err = vcsCmd.Download(repoRootDir)
if err != nil {
return fmt.Errorf("failed to download repo into %q %v", repoRootDir, err)
}
}
err = os.MkdirAll(filepath.Join(repoRootDir, "vendor"), 0777)
if err != nil {
return err
}
ctx, err := NewContext(repoRootDir, filepath.Join("vendor", vendorFilename), "vendor", false)
if err != nil {
return err
}
ctx.Insecure = insecure
ctx.Logger = logger
statusList, err := ctx.Status()
if err != nil {
return err
}
added := make(map[string]bool, len(statusList))
for _, item := range statusList {
switch item.Status.Location {
case LocationExternal, LocationNotFound:
if added[item.Pkg.Path] {
continue
}
ctx.ModifyImport(item.Pkg, Fetch)
added[item.Pkg.Path] = true
}
}
defer ctx.WriteVendorFile()
return ctx.Alter()
}