-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjustgo.go
276 lines (227 loc) · 5.93 KB
/
justgo.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"archive/zip"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/satori/go.uuid"
"github.com/urfave/cli"
)
var nonProblematicFiles = map[string]bool{}
func init() {
// Initialize map of the non-problematic files to ignore.
// Also, specify whether they could conflict with any files in the zip.
// and therefore may need to be actively preserved (value == "true")
nonProblematicFiles = map[string]bool{
".git": false,
".gitignore": false,
"justgo": false,
"LICENSE": true,
"README.md": true,
}
}
func main() {
app := cli.NewApp()
app.Name = "justgo"
app.Version = "1.2.1"
app.Usage = "create a new skeleton project for a Go-based API [micro]service"
app.UsageText = app.Name + " <path>"
app.ArgsUsage = "path"
app.Action = func(c *cli.Context) error {
installPath := "."
if c.NArg() > 0 {
installPath = c.Args().Get(0)
}
buildProject(installPath)
return nil
}
app.Run(os.Args)
}
func buildProject(path string) {
fmt.Printf("Building a skeleton microservice at destination path: `%v` \n", path)
exists, err := pathExists(path)
abortIfErr(err)
if exists == true { // need to make sure it's empty!
isPathEmpty := folderIsEmpty(path)
if !isPathEmpty {
err := errors.New("There are files in `" + path + "`. Destination path must be empty. Aborting.")
abortIfErr(err)
}
} else {
err := os.MkdirAll(path, os.ModePerm)
abortIfErr(err)
}
fileUrl := "https://github.com/inadarei/justgo-microservice/archive/master.zip"
tmpFilePath := os.TempDir() + string(os.PathSeparator) + "justgo.zip"
defer os.Remove(tmpFilePath)
// Move all conflicting files to tmp dir and move them back post-build
filesToMove := getConflictingFiles(path)
uniqueToken := uuid.NewV4()
uniqueTempFolder := filepath.Join(os.TempDir(), fmt.Sprintf("%s", uniqueToken))
os.MkdirAll(uniqueTempFolder, os.ModePerm)
defer os.Remove(uniqueTempFolder)
if filesToMove != nil {
for _, file := range filesToMove {
srcPath := filepath.Join(path, file)
tmpMovedFilePath := filepath.Join(uniqueTempFolder, file)
err := os.Rename(srcPath, tmpMovedFilePath)
abortIfErr(err)
defer os.Remove(tmpMovedFilePath)
defer os.Rename(tmpMovedFilePath, srcPath)
}
}
err = downloadFile(tmpFilePath, fileUrl)
abortIfErr(err)
err = unzip(tmpFilePath, path, true)
abortIfErr(err)
cleanup(path)
fmt.Println("Success. Happy coding!")
}
// Not everything in the download is useful. Remove garbage
func cleanup(path string) {
var err error
filesToRemove := []string{"CODE_OF_CONDUCT.md", "CONTRIBUTING.md", "LICENSE"}
for _, file := range filesToRemove {
err = os.Remove(filepath.Join(path, file))
abortIfErr(err)
}
err = os.RemoveAll(filepath.Join(path, "cmd"))
abortIfErr(err)
}
// Simple exit if error, to avoid putting same 4 lines of code in too many places
func abortIfErr(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
// Check whether path already exists
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// check whether a folder is empty
func folderIsEmpty(path string) bool {
// What if we were given path to a file or smth?
fi, err := os.Stat(path)
abortIfErr(err)
if fi.IsDir() == false {
err := errors.New("Destination path `" + path + "` is not a folder!")
abortIfErr(err)
}
f, err := os.Open(path)
if err != nil {
f.Close()
abortIfErr(err)
}
defer f.Close()
filenames, err := f.Readdirnames(0)
abortIfErr(err)
if !containsProblematicFiles(filenames) {
return true
}
// If not already exited, scanning children must have errored-out
abortIfErr(err)
return false
}
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
// Unzip an archive and place contents at 'target' folder.
// Also allows skipping topLevel
func unzip(archive, target string, skipTop bool) error {
reader, err := zip.OpenReader(archive)
if err != nil {
return err
}
firstItem := ""
for _, file := range reader.File {
var (
modPath string
err error
)
if skipTop {
if firstItem == "" {
firstItem = file.Name
continue
} else {
modPath, err = filepath.Rel(firstItem, file.Name)
abortIfErr(err)
}
} else {
modPath = file.Name
}
path := filepath.Join(target, modPath)
if file.FileInfo().IsDir() {
os.MkdirAll(path, file.Mode())
continue
}
fileReader, err := file.Open()
if err != nil {
return err
}
defer fileReader.Close()
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return err
}
defer targetFile.Close()
if _, err := io.Copy(targetFile, fileReader); err != nil {
return err
}
}
return nil
}
// Check whether folder contains any files other than those specified as non-problematic.
func containsProblematicFiles(filesInDir []string) bool {
if len(filesInDir) > len(nonProblematicFiles) {
return true
}
// check if any files in the folder are considered to be problematic
for _, filename := range filesInDir {
// Is the file non-problematic?
_, exists := nonProblematicFiles[filename]
if !exists {
return true
}
}
return false
}
// Get Non-Problematic files in the 'target' folder that conflict with others in the zip.
func getConflictingFiles(path string) []string {
var filesWithConflicts []string
for filename, hasConflict := range nonProblematicFiles {
exists, err := pathExists(filepath.Join(path, filename))
abortIfErr(err)
if exists && hasConflict == true {
filesWithConflicts = append(filesWithConflicts, filename)
}
}
return filesWithConflicts
}