-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (65 loc) · 2.14 KB
/
main.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
// Command fetch_repo is similar to "go get -d" but it works even if the given
// repository path is not a buildable Go package and it checks out a specific
// revision rather than the latest revision.
//
// The difference between fetch_repo and "git clone" or {new_,}git_repository is
// that fetch_repo recognizes import redirection of Go and it supports other
// version control systems than git.
//
// These differences help us to manage external Go repositories in the manner of
// Bazel.
package main
import (
"flag"
"fmt"
"log"
"golang.org/x/tools/go/vcs"
)
var (
remote = flag.String("remote", "", "The URI of the remote repository. Must be used with the --vcs flag.")
cmd = flag.String("vcs", "", "Version control system to use to fetch the repository. Should be one of: git,hg,svn,bzr. Must be used with the --remote flag.")
rev = flag.String("rev", "", "target revision")
dest = flag.String("dest", "", "destination directory")
importpath = flag.String("importpath", "", "Go importpath to the repository fetch")
// Used for overriding in tests to disable network calls.
repoRootForImportPath = vcs.RepoRootForImportPath
)
func getRepoRoot(remote, cmd, importpath string) (*vcs.RepoRoot, error) {
if (cmd == "") != (remote == "") {
return nil, fmt.Errorf("--remote should be used with the --vcs flag. If this is an import path, use --importpath instead.")
}
if cmd != "" && remote != "" {
v := vcs.ByCmd(cmd)
if v == nil {
return nil, fmt.Errorf("invalid VCS type: %s", cmd)
}
return &vcs.RepoRoot{
VCS: v,
Repo: remote,
Root: importpath,
}, nil
}
// User did not give us complete information for VCS / Remote.
// Try to figure out the information from the import path.
r, err := repoRootForImportPath(importpath, true)
if err != nil {
return nil, err
}
if importpath != r.Root {
return nil, fmt.Errorf("not a root of a repository: %s", importpath)
}
return r, nil
}
func run() error {
r, err := getRepoRoot(*remote, *cmd, *importpath)
if err != nil {
return err
}
return r.VCS.CreateAtRev(*dest, r.Repo, *rev)
}
func main() {
flag.Parse()
if err := run(); err != nil {
log.Fatal(err)
}
}