-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
156 lines (135 loc) · 2.83 KB
/
lib.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func catalystSay(content string) {
fmt.Println("\033[0;32m[ catalyst ]\033[0m : " + content)
}
func catalystStartAndDone(content string) func() {
catalystSay(content + " ...start")
return func() {
catalystSay(content + " ...done")
fmt.Println("")
}
}
func cmdexer(cmdstr string) string {
cmd := exec.Command("/bin/bash", "-c", cmdstr)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err.Error())
}
return out.String()
}
func cmdarrexer(c string, args []string) string {
cmd := exec.Command(c, args...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err.Error())
}
return out.String()
}
func pwd() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return dir
}
func myGopath() []string {
gopathString := os.Getenv("GOPATH")
gopathes := strings.Split(gopathString, ":")
return gopathes
}
func getMyWordDir(projectName string) string {
pwd := pwd()
gopathes := myGopath()
workdirPrefix := ""
for _, gopath := range gopathes {
srcdir := ""
if endWithSlash(gopath) {
srcdir = gopath + "src/"
} else {
srcdir = gopath + "/src/"
}
if strings.HasPrefix(pwd, srcdir) {
workdirPrefix = pwd[len(srcdir):]
break
}
}
if workdirPrefix == "" {
log.Fatal(errors.New("play under your gopath src dir please"))
}
if !endWithSlash(workdirPrefix) {
workdirPrefix = workdirPrefix + "/"
}
return workdirPrefix + projectName
}
func endWithSlash(str string) bool {
if str == "" {
return false
}
if str[len(str)-1] == '/' {
return true
}
return false
}
func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
var out []byte
buf := make([]byte, 1024, 1024)
for {
n, err := r.Read(buf[:])
if n > 0 {
d := buf[:n]
out = append(out, d...)
_, err := w.Write(d)
if err != nil {
return out, err
}
}
if err != nil {
// Read returns io.EOF at the end of file, which is not an error for us
if err == io.EOF {
err = nil
}
return out, err
}
}
// never reached
panic(true)
return nil, nil
}
func cmdwithstdout(cmdstr string) {
cmd := exec.Command("/bin/bash", "-c", cmdstr)
var stdout, stderr []byte
var errStdout, errStderr error
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
cmd.Start()
go func() {
stdout, errStdout = copyAndCapture(os.Stdout, stdoutIn)
}()
go func() {
stderr, errStderr = copyAndCapture(os.Stderr, stderrIn)
}()
err := cmd.Wait()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
if errStdout != nil || errStderr != nil {
log.Fatal("failed to capture stdout or stderr\n")
}
outStr, errStr := string(stdout), string(stderr)
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
}