-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.go
89 lines (74 loc) · 2.01 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
package main
import (
"errors"
"os"
"path"
"strconv"
"strings"
"github.com/nitrous-io/goop/colors"
"github.com/nitrous-io/goop/goop"
)
func main() {
name := path.Base(os.Args[0])
pwd, err := os.Getwd()
if err != nil {
os.Stderr.WriteString(colors.Error + name + ": failed to determine present working directory!" + colors.Reset + "\n")
}
g := goop.NewGoop(path.Join(pwd), os.Stdin, os.Stdout, os.Stderr)
if len(os.Args) < 2 {
printUsage()
}
cmd := os.Args[1]
switch cmd {
case "help":
printUsage()
case "install":
err = g.Install()
case "update":
err = g.Update()
case "exec":
if len(os.Args) < 3 {
printUsage()
}
err = g.Exec(os.Args[2], os.Args[3:]...)
case "go":
if len(os.Args) < 3 {
printUsage()
}
err = g.Exec("go", os.Args[2:]...)
case "env":
g.PrintEnv()
default:
err = errors.New(`unrecognized command "` + cmd + `"`)
}
if err != nil {
errMsg := err.Error()
code := 1
// go does not provide a cross-platform way to get exit status, so inspect error message instead
// https://code.google.com/p/go/source/browse/src/pkg/os/exec_posix.go#119
if strings.HasPrefix(errMsg, "exit status ") {
code, err = strconv.Atoi(errMsg[len("exit status "):])
if err != nil {
code = 1
}
errMsg = "Command failed with " + errMsg
}
os.Stderr.WriteString(colors.Error + name + ": " + errMsg + colors.Reset + "\n")
os.Exit(code)
}
}
func printUsage() {
os.Stdout.WriteString(strings.TrimSpace(usage) + "\n\n")
os.Exit(0)
}
const usage = `
Goop is a tool for managing Go dependencies.
goop command [arguments]
The commands are:
install install the dependencies specified by Goopfile or Goopfile.lock
update update dependencies to their latest versions
env print GOPATH and PATH environment variables, with the vendor path prepended
exec execute a command in the context of the installed dependencies
go execute a go command in the context of the installed dependencies
help print this message
`