forked from FiloSottile/gvt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (100 loc) · 2.28 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"path/filepath"
"strings"
)
var fs = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
func init() {
fs.Usage = func() {}
}
type Command struct {
Name string
UsageLine string
Short string
Long string
Run func(args []string) error
AddFlags func(fs *flag.FlagSet)
}
var commands = []*Command{
cmdFetch,
cmdRestore,
cmdUpdate,
cmdList,
cmdDelete,
}
func main() {
args := os.Args[1:]
switch {
case len(args) < 1, args[0] == "-h", args[0] == "-help":
printUsage(os.Stdout)
os.Exit(0)
case args[0] == "help":
help(args[1:])
return
case args[0] == "rebuild":
// rebuild was renamed restore, alias for backwards compatibility
args[0] = "restore"
}
for _, command := range commands {
if command.Name == args[0] {
// add extra flags if necessary
if command.AddFlags != nil {
command.AddFlags(fs)
}
if err := fs.Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
help(args[:1])
os.Exit(0)
}
fmt.Fprint(os.Stderr, "\n")
help(args[:1])
os.Exit(3)
}
if err := command.Run(fs.Args()); err != nil {
log.Fatalf("command %q failed: %v", command.Name, err)
}
if err := GlobalDownloader.Flush(); err != nil {
log.Fatalf("failed to delete tempdirs: %v", err)
}
return
}
}
fmt.Fprintf(os.Stderr, "unknown command: %q\n\n", args[0])
printUsage(os.Stderr)
os.Exit(3)
}
var (
vendorDir, manifestFile string
importPath string
)
func init() {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
vendorDir = filepath.Join(wd, "vendor")
manifestFile = filepath.Join(vendorDir, "manifest")
var srcTree []string
for _, p := range filepath.SplitList(build.Default.GOPATH) {
srcTree = append(srcTree, filepath.Join(p, "src")+string(filepath.Separator))
}
var pathMismatch int
for _, p := range srcTree {
if !strings.HasPrefix(wd, p) && wd != p[:len(p)-1] {
pathMismatch++
continue
}
importPath = filepath.ToSlash(strings.TrimPrefix(wd, p))
break
}
if _, err := os.Stat(filepath.Join(wd, "Makefile")); os.IsNotExist(err) {
if build.Default.GOPATH == "" || len(srcTree) == pathMismatch {
log.Println("WARNING: for go vendoring to work your project needs to be somewhere under $GOPATH/src/")
}
}
}