-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
368 lines (312 loc) · 10.2 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"errors"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/otiai10/copy"
)
const VERSION = "v0.0.23"
// TODO: implement non-debug mode
// TODO: implement versioning
// TODO: implement update check
// TODO: implement help
// TODO: package log inside zip files.
// TODO: vendor only once per go module
// TODO: Detect and show go version
// TODO: Detect and warn on incorrect Go version based on go mod
// TODO: Better error handling when go mod vendor fails
func main() {
log.Debug("Running version " + VERSION)
flag.Parse()
inputPath := flag.Arg(0)
packageDate := flag.Arg(1)
log.Debug("inputPath=" + inputPath)
log.Debug("packageDate=" + packageDate)
absPath, err := filepath.Abs(inputPath)
if err != nil {
log.WithFields(log.Fields{
"inputPath": inputPath,
"originalError": err,
}).Panic("Error getting absolute path. File or path may not exist")
panic("Error getting absolute path. File or path may not exist")
}
log.WithField("absPath", absPath).Debug("Reading go files")
absPathStat, err := os.Stat(absPath)
if err != nil {
log.WithFields(log.Fields{
"absPath": absPath,
"originalError": err,
}).Panic("Error getting stat. File or path may not exist.")
panic("Error getting stat. File or path may not exist.")
}
mainFiles, err := getMainFiles(absPathStat, absPath)
if err != nil {
log.WithFields(log.Fields{
"inputPath": inputPath,
"originalError": err,
}).Panic("Error getting main files")
panic("Error getting main files")
}
log.WithField("mainFiles", mainFiles).Debug("Finished getting mainFiles")
for _, mainFile := range mainFiles {
log.WithField("MainFile", mainFile).Debug("Packaging for mainfile")
err = packageMainFile(mainFile, packageDate, mainFiles)
if err != nil {
log.WithFields(log.Fields{
"mainFile": mainFile,
"originalError": err,
}).Panic("Error getting main file")
panic("Error getting main file")
}
}
log.Debug("Ran version " + VERSION)
}
func getMainFiles(absPathStat os.FileInfo, absPath string) ([]string, error) {
mainFiles := []string{}
if absPathStat.IsDir() {
log.WithField("absPath", absPath).Debug("Input is dir")
err := filepath.Walk(absPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
parsedPackages, err := parser.ParseDir(
token.NewFileSet(),
path,
nil,
parser.ParseComments,
)
if err != nil {
return err
}
for _, pkg := range parsedPackages {
for filename, file := range pkg.Files {
for _, decl := range file.Decls {
if ast.FilterDecl(decl, func(name string) bool { return name == "main" }) {
mainFiles = append(mainFiles, filename)
}
}
}
}
return nil
})
if err != nil {
return []string{}, err
}
} else if absPathStat.Mode().Perm().IsRegular() {
log.Printf("'%s' input is a file", absPath)
parsedFile, err := parser.ParseFile(token.NewFileSet(), absPath, nil, parser.ParseComments)
if err != nil {
return []string{}, err
}
for _, decl := range parsedFile.Decls {
if ast.FilterDecl(decl, func(name string) bool { return name == "main" }) {
mainFiles = append(mainFiles, absPath)
}
}
} else {
return []string{}, fmt.Errorf("'%s' does not exist", absPath)
}
if len(mainFiles) == 0 {
return []string{}, fmt.Errorf("no main files found in %s", absPath)
}
return mainFiles, nil
}
// TODO: Make work with GOPATH
func packageMainFile(mainFile string, packageDate string, mainFiles []string) error {
goModPath := ""
parentDir := filepath.Dir(mainFile)
prevParentDir := ""
log.WithField("parentDir", parentDir).Debug("Starting looking up for go.mod " + mainFile)
for {
goModStat, _ := os.Stat(parentDir + "/go.mod")
if goModStat != nil {
goModPath = parentDir + "/go.mod"
log.WithField("goModPath", goModPath).Debug("Found go.mod path")
break
}
if parentDir != prevParentDir {
prevParentDir = parentDir
parentDir = filepath.Dir(parentDir)
log.WithField("parentDir", parentDir).Debug("Trying parent directory")
continue
}
return errors.New("go.mod not found")
}
tempWorkDir, err := os.MkdirTemp("", "vcgopkg")
log.WithField("tempWorkDir", tempWorkDir).Debug("Creating temporary working directory")
if err != nil {
return err
}
defer os.RemoveAll(tempWorkDir)
copyDir := tempWorkDir + "/" + filepath.Base(filepath.Dir(goModPath))
log.WithFields(log.Fields{"from": parentDir, "to": copyDir}).Debug("Copying files")
err = copy.Copy(parentDir, copyDir, copy.Options{
Skip: func(srcinfo fs.FileInfo, src string, dest string) (bool, error) {
if src != mainFile && strings.Contains(":"+strings.Join(mainFiles, ":")+":", ":"+src+":") {
log.WithField("src", src).Debug("Skipping copying other main file")
return true, nil
}
filename := filepath.Base(src)
if filename == ".git" {
log.WithField("src", src).Debug("Skipping copying .git")
return true, nil
}
return false, nil
},
})
if err != nil {
return err
}
// TODO: Don't copy veracode dir, instead of copying it and then removing it
log.WithField("dir", copyDir+"/veracode").Debug("Removing veracode directory from copy")
err = os.RemoveAll(copyDir + "/veracode")
if err != nil {
return err
}
LogFiles(copyDir, "Copied Files")
err = vendorDir(copyDir)
if err != nil {
return err
}
err = updateVeracodeJson(mainFile, parentDir, copyDir)
if err != nil {
return err
}
err = pkg(goModPath, mainFile, tempWorkDir, parentDir, packageDate)
if err != nil {
return err
}
LogFiles(tempWorkDir, "Temporary workdir after packaging")
LogFiles(parentDir, "ParentDir")
LogFiles(parentDir+"/veracode", "Veracode Dir")
return nil
}
func vendorDir(copyDir string) error {
log.Debug("Starting vendor")
_, goPathErr := exec.LookPath("go")
log.WithField("goPathErr", goPathErr).Debug("Testing if Go is on the path")
_, vendorPathErr := os.Stat(copyDir + "/vendor")
log.WithField("stat error", vendorPathErr).Debug("Tested if vendor directory exists")
canFindVendor := vendorPathErr == nil
canFindGoExecutable := goPathErr == nil
if !canFindVendor && !canFindGoExecutable {
log.Debug("Error getting stat of /vendor")
return fmt.Errorf("no vendor directory and unable to run go mod vendor")
} else if canFindVendor {
log.Debug("Found an existing vendor dir, using that")
return nil
}
log.Debug("Vendor folder did not exist, running go mod vendor, this may take a while")
cmd := exec.Command("go", "mod", "vendor")
cmd.Dir = copyDir
cmdOut, err := cmd.CombinedOutput()
log.WithFields(log.Fields{
"cmdOut": string(cmdOut),
}).Debug("Ran go mod vendor")
if err != nil {
return err
}
_, microsoftVendorLibPathErr := os.Stat(copyDir + "/vendor/github.com/Microsoft")
log.WithField("stat error", microsoftVendorLibPathErr).Debug("Tested if vendor directory exists again")
hasMicrosoftVendorLib := microsoftVendorLibPathErr == nil
if hasMicrosoftVendorLib {
// Hack to fix issues with case-sensitivity.
microsoftPath := filepath.Join(copyDir, "vendor/github.com/Microsoft")
newMicrosoftPath := filepath.Join(copyDir, "vendor/github.com/microsoft")
err = os.Rename(microsoftPath, newMicrosoftPath)
}
if err != nil {
return err
}
_, vendorPathErr = os.Stat(copyDir + "/vendor")
hasVendorDir := vendorPathErr == nil
if hasVendorDir {
err = deleteFilesWithoutGoExtension(copyDir + string(os.PathSeparator) + "vendor")
}
return err
}
// TODO: Find FirstParty
func updateVeracodeJson(mainFile string, parentDir string, copyDir string) error {
log.Debug("Updating veracode.json")
file := copyDir + string(os.PathSeparator) + "veracode.json"
err := CreateEmptyVeracodeJsonFileIfNotExists(file)
if err != nil {
return err
}
veracodeJsonFile, err := NewVeracodeJsonFile(file)
if err != nil {
return err
}
// Given a mainFile at /home/user/Desktop/project/cmd/server/main.go
// Remove the 'parentDir' of the zip, for example: /home/user/Desktop/project
// Should result in "cmd/server"
mainFileRelativePath := strings.TrimPrefix(filepath.Dir(mainFile), parentDir)
mainFileRelativePath = strings.TrimPrefix(mainFileRelativePath, string(os.PathSeparator))
log.WithFields(log.Fields{
"MainFile": mainFile,
"MainFileDir": filepath.Dir(mainFile),
"prefix": parentDir + string(os.PathSeparator),
"MainRoot": mainFileRelativePath,
}).Debug("Setting MainRoot")
veracodeJsonFile.VeracodeJson.MainRoot = mainFileRelativePath
return veracodeJsonFile.WriteToFile()
}
// TODO: Allow writing to output directory
// TODO: Use path to main in output file to support multiple path
// TODO: Test package with go loader
func pkg(goModPath string, mainFile string, tempWorkDir string, parentDir string, packageDate string) error {
goModDir := filepath.Dir(goModPath)
log.Debug("Go.mod dir: " + goModDir)
log.Debug("main.go file:" + mainFile)
baseDir := filepath.Base(goModDir)
if packageDate == "" {
packageDate = time.Now().Format("_20060102150405")
}
// Turn /path/to/module/cmd/main.go into cmd-main
// TODO: use allow-list instead of deny-list
relativeMainPath := strings.TrimSuffix(mainFile[len(goModDir)+1:], ".go")
cmdSlug := "_"
cmdSlug += strings.ReplaceAll(relativeMainPath, "\\", "--")
cmdSlug = strings.ReplaceAll(cmdSlug, "/", "--")
zipFile := baseDir + cmdSlug + packageDate + ".zip"
log.WithFields(log.Fields{
"baseDir": baseDir,
"zipFile": tempWorkDir + string(os.PathSeparator) + zipFile,
}).Debug("Writing zip file")
err := ZipWriter(tempWorkDir, tempWorkDir+string(os.PathSeparator)+zipFile)
if err != nil {
return err
}
veracodeDir := parentDir + string(os.PathSeparator) + "veracode"
_, err = os.Stat(veracodeDir)
if err != nil {
err = os.Mkdir(veracodeDir, 0700)
log.WithField("veracodeDir", veracodeDir).Debug("Created veracode dir for binaries")
if err != nil {
return err
}
}
err = MoveFile(tempWorkDir+string(os.PathSeparator)+zipFile, veracodeDir+string(os.PathSeparator)+zipFile)
if err != nil {
return err
}
log.WithFields(log.Fields{
"from": tempWorkDir + string(os.PathSeparator) + zipFile,
"to": veracodeDir + string(os.PathSeparator) + zipFile,
}).Debug("Rename zipfile")
return nil
}