forked from FiloSottile/gvt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (68 loc) · 1.35 KB
/
main.go
File metadata and controls
82 lines (68 loc) · 1.35 KB
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
package main
import (
"flag"
"log"
"os"
"path/filepath"
)
var fs = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
func init() {
fs.Usage = func() {
printUsage(os.Stderr)
os.Exit(2)
}
}
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,
cmdRebuild,
cmdUpdate,
cmdList,
cmdDelete,
}
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
}
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 {
log.Fatalf("could not parse flags: %v", err)
}
args = fs.Args() // reset args to the leftovers from fs.Parse
if err := command.Run(args); err != nil {
log.Fatalf("command %q failed: %v", command.Name, err)
}
return
}
}
log.Fatalf("unknown command %q ", args[0])
}
const manifestfile = "manifest"
func vendorDir() string {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return filepath.Join(wd, "vendor")
}
func manifestFile() string {
return filepath.Join(vendorDir(), manifestfile)
}