forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (84 loc) · 2 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
package main
import (
"flag"
"os"
"path/filepath"
"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
"github.com/constabulary/gb/log"
)
var (
fs = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
projectroot = os.Getenv("GB_PROJECT_DIR")
args []string
)
func init() {
fs.Usage = func() {
printUsage(os.Stderr)
os.Exit(2)
}
}
var commands = []*cmd.Command{
cmdFetch,
cmdUpdate,
cmdList,
cmdDelete,
cmdPurge,
cmdRestore,
}
func main() {
args := os.Args[1:]
switch {
case len(args) < 1, args[0] == "-h", args[0] == "-help":
fs.Usage()
os.Exit(1)
case args[0] == "help":
help(args[1:])
return
case projectroot == "":
log.Fatalf("don't run this binary directly, it is meant to be run as 'gb vendor ...'")
default:
}
root, err := cmd.FindProjectroot(projectroot)
if err != nil {
log.Fatalf("could not locate project root: %v", err)
}
project := gb.NewProject(root,
gb.SourceDir(filepath.Join(root, "src")),
gb.SourceDir(filepath.Join(root, "vendor", "src")))
log.Debugf("project root %q", project.Projectdir())
for _, command := range commands {
if command.Name == args[0] && command.Runnable() {
// add extra flags if necessary
if command.AddFlags != nil {
command.AddFlags(fs)
}
if command.FlagParse != nil {
err = command.FlagParse(fs, args)
} else {
err = fs.Parse(args[1:])
}
if err != nil {
log.Fatalf("could not parse flags: %v", err)
}
args = fs.Args() // reset args to the leftovers from fs.Parse
log.Debugf("args: %v", args)
ctx, err := project.NewContext(
gb.GcToolchain(),
)
if err != nil {
log.Fatalf("unable to construct context: %v", err)
}
defer ctx.Destroy()
if err := command.Run(ctx, args); err != nil {
log.Fatalf("command %q failed: %v", command.Name, err)
}
return
}
}
log.Fatalf("unknown command %q ", args[0])
}
const manifestfile = "manifest"
func manifestFile(ctx *gb.Context) string {
return filepath.Join(ctx.Projectdir(), "vendor", manifestfile)
}