-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinit.go
177 lines (161 loc) · 4.43 KB
/
init.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"os"
"strings"
"fmt"
"path/filepath"
"io/ioutil"
"io"
)
// 初始化项目
func initProject(path string) {
// 得到当前在src下的路径
// 得到当前路径
//path, err := os.Getwd()
//if err != nil {
// panic(err)
//}
goSrcPath := os.Getenv("GOPATH") + "/src/"
if !strings.Contains(path, goSrcPath) { // 包含这个路径
fmt.Printf("You must use dys command in the $GOPATH directory.\n")
fmt.Printf("\tNow the current path: " + path + "\n")
fmt.Println("\tNow the $GOPATH: " + goSrcPath + "\n")
return
}
p := path[len(goSrcPath):]
// 从当前路径下查找bingo_template
bingoTemplatePath := os.Getenv("GOPATH") + `/src/github.com/silsuer/bingo/gtpl`
// 如果是dir
stat, err := os.Stat(bingoTemplatePath)
if err != nil {
if os.IsNotExist(err) {
fmt.Println(`Can not find the template directory in ` + bingoTemplatePath)
fmt.Println(`Please use go get -u github.com/silsuer/bingo...`)
return
} else {
panic(err)
}
}
if !stat.IsDir() {
fmt.Println(`No such directory in ` + bingoTemplatePath)
return
}
copyDir(bingoTemplatePath, path, p)
// 录入首页信息
// 打印欢迎信息
welecome()
}
func welecome() {
info := `
__ _______ _ ____ ___ __ __ _____ ____ ___ _ _ ____ ___ _
\ \ / / ____| | / ___/ _ \| \/ | ____| | __ )_ _| \ | |/ ___|/ _ \| |
\ \ /\ / /| _| | | | | | | | | |\/| | _| | _ \| || \| | | _| | | | |
\ V V / | |___| |__| |__| |_| | | | | |___ | |_) | || |\ | |_| | |_| |_|
\_/\_/ |_____|_____\____\___/|_| |_|_____| |____/___|_| \_|\____|\___/(_)
Now you can start a development server with: make dev
`
fmt.Printf("\n %c[0;48;32m%s%c[0m\n\n", 0x1B, info, 0x1B)
}
func copyDir(src string, dest string, variable string) {
src_original := src
err := filepath.Walk(src, func(src string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
// fmt.Println(f.Name())
// copyDir(f.Name(), dest+"/"+f.Name())
} else {
dest_new := strings.Replace(src, src_original, dest, -1)
CopyFile(src, dest_new, variable)
}
//println(path)
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}
//egodic directories
func getFilelist(path string) {
err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
println(path)
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
//copy file
func CopyFile(src, dst string, variable string) (w int, err error) {
srcFile, err := os.Open(src)
if err != nil {
fmt.Println(err.Error())
return
}
defer srcFile.Close()
dst_slices := strings.Split(dst, string(os.PathSeparator))
dst_slices_len := len(dst_slices)
dest_dir := ""
for i := 0; i < dst_slices_len-1; i++ {
dest_dir = dest_dir + dst_slices[i] + string(os.PathSeparator)
}
b, err := PathExists(dest_dir)
if b == false {
err := os.MkdirAll(dest_dir, os.ModePerm) //在当前目录下生成md目录
if err != nil {
fmt.Println(err)
}
}
ext := filepath.Ext(dst)
if ext == ".gtpl" {
newNameSlice := strings.Split(filepath.Base(dst), ".")
newName := strings.Join(newNameSlice[:len(newNameSlice)-1], ".")
dst = filepath.Dir(dst) + "/" + newName + ".go"
} else if ext == ".ghtml" {
newNameSlice := strings.Split(filepath.Base(dst), ".")
newName := strings.Join(newNameSlice[:len(newNameSlice)-1], ".")
dst = filepath.Dir(dst) + "/" + newName + ".html"
}
dstFile, err := os.Create(dst)
if err != nil {
fmt.Println(err.Error())
return
}
bytes, err := ioutil.ReadAll(srcFile)
if err != nil {
fmt.Println(err.Error())
return
}
// 获取srcFile中的所有数据,并替换变量
defer dstFile.Close()
var str string
if fName := filepath.Base(dst); fName == "Makefile" || fName == ".gitignore" {
// 如果是makefile,则把其中的 ${name} 替换成 当前目录
// 否则再进行判断后缀名的操作
// 如果是.gitignore,则也进行替换
v := strings.Split(variable, "/")
vv := v[len(v)-1]
str = strings.Replace(string(bytes), "${name}", vv, -1)
} else {
str = strings.Replace(string(bytes), "${path}", variable, -1)
}
// 这个文件写入dstFile中
return io.WriteString(dstFile, str)
}