-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.go
128 lines (104 loc) · 2.65 KB
/
build.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
//+build ignore
package main
import (
"archive/zip"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
)
func main() {
fmt.Println("Running build script for the Lambda trigger")
//Get the dir where build.go is present
appDir, err := os.Getwd()
if err != nil {
fmt.Printf(err.Error())
}
var cmd *exec.Cmd
// Clean up
fmt.Println("Cleaning up previous executables")
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/C", "del", "/q", "handler", "handler.zip")
} else {
cmd = exec.Command("rm", "-f", "handler", "handler.zip")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Printf(err.Error())
}
// Install missing indirect modules
fmt.Println("Install modules")
cmd = exec.Command("go", "get")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Join(appDir)
cmd.Env = append(os.Environ(), "GOOS=linux")
err = cmd.Run()
if err != nil {
fmt.Printf(err.Error())
}
// Build an executable for Linux
fmt.Println("Building a new handler file")
cmd = exec.Command("go", "build", "-o", "handler")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Join(appDir)
cmd.Env = append(os.Environ(), "GOOS=linux")
err = cmd.Run()
if err != nil {
fmt.Printf(err.Error())
}
// Zip the executable using the same code as build-lambda-zip
fmt.Println("Zipping the new handler file")
inputExe := filepath.Join(cmd.Dir, "handler")
outputZip := filepath.Join(cmd.Dir, "handler.zip")
if err := compressExe(outputZip, inputExe); err != nil {
fmt.Printf("Failed to compress file: %v", err)
}
err = CopyFile(outputZip, filepath.Join(cmd.Dir, "..", "bin", "handler.zip"))
if err != nil {
fmt.Println("Failed to copy zip file to bin: %v", err)
}
}
func writeExe(writer *zip.Writer, pathInZip string, data []byte) error {
exe, err := writer.CreateHeader(&zip.FileHeader{
CreatorVersion: 3 << 8, // indicates Unix
ExternalAttrs: 0777 << 16, // -rwxrwxrwx file permissions
Name: pathInZip,
Method: zip.Deflate,
})
if err != nil {
return err
}
_, err = exe.Write(data)
return err
}
func compressExe(outZipPath, exePath string) error {
zipFile, err := os.Create(outZipPath)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
data, err := ioutil.ReadFile(exePath)
if err != nil {
return err
}
return writeExe(zipWriter, filepath.Base(exePath), data)
}
func CopyFile(srcFile, destFile string) error {
input, err := ioutil.ReadFile(srcFile)
if err != nil {
return err
}
err = ioutil.WriteFile(destFile, input, 0644)
if err != nil {
return err
}
return nil
}