-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
81 lines (67 loc) · 1.47 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
projectroot = os.Getenv("GB_PROJECT_DIR")
args []string = os.Args[1:]
)
func main() {
fatalf := func(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "FATAL: "+format+"\n", args...)
os.Exit(1)
}
switch {
case len(args) < 1, args[0] == "-h", args[0] == "-help", args[0] == "help":
printUsage()
os.Exit(0)
case projectroot == "":
fatalf("don't run this binary directly, it is meant to be run as 'gb run ...'")
default:
}
env := mergeEnv(os.Environ(), map[string]string{
"GOPATH": projectroot + ":" + filepath.Join(projectroot, "vendor"),
})
path, err := exec.LookPath(args[0])
if err != nil {
fatalf("run: unable to locate %q: %v", args[0], err)
}
cmd := exec.Cmd{
Path: path,
Args: args,
Env: env,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if err := cmd.Run(); err != nil {
fmt.Print(err)
os.Exit(1)
}
fmt.Println("Done")
}
func mergeEnv(env []string, args map[string]string) []string {
m := make(map[string]string)
for _, e := range env {
v := strings.SplitN(e, "=", 2)
m[v[0]] = v[1]
}
for k, v := range args {
m[k] = v
}
env = make([]string, 0, len(m))
for k, v := range m {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
return env
}
func printUsage() {
fmt.Println(`gb-run, a gb plugin to run apps with a GOPATH set to match the current gb project.
Usage:
gb run command [arguments]
`)
}