This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
exec.go
99 lines (87 loc) · 1.67 KB
/
exec.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 (
"github.com/daviddengcn/go-colortext"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
)
type Color int
const (
None Color = Color(ct.None)
Red Color = Color(ct.Red)
Blue Color = Color(ct.Blue)
Green Color = Color(ct.Green)
)
func handleSignal() {
sc := make(chan os.Signal, 10)
signal.Notify(sc, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
go func() {
<-sc
ct.ResetColor()
os.Exit(0)
}()
}
func ready() error {
dir, err := os.Getwd()
if err != nil {
return err
}
vendor, err := filepath.Abs(vendorFolder)
if err != nil {
return err
}
for {
file := filepath.Join(dir, "Gomfile")
if isFile(file) {
vendor = filepath.Join(dir, vendorFolder)
break
}
next := filepath.Clean(filepath.Join(dir, ".."))
if next == dir {
dir = ""
break
}
dir = next
}
binPath := os.Getenv("PATH") +
string(filepath.ListSeparator) +
filepath.Join(vendor, "bin")
err = os.Setenv("PATH", binPath)
if err != nil {
return err
}
var paths []string
if dir == "" {
paths = []string{vendor, os.Getenv("GOPATH")}
} else {
paths = []string{vendor, dir, os.Getenv("GOPATH")}
}
vendor = strings.Join(paths, string(filepath.ListSeparator))
err = os.Setenv("GOPATH", vendor)
if err != nil {
return err
}
return nil
}
var stdout = os.Stdout
var stderr = os.Stderr
var stdin = os.Stdin
func run(args []string, c Color) error {
if err := ready(); err != nil {
return err
}
if len(args) == 0 {
usage()
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = stdin
ct.ChangeColor(ct.Color(c), true, ct.None, false)
err := cmd.Run()
ct.ResetColor()
return err
}