forked from aws/aws-lambda-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (67 loc) · 1.61 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
package main
import (
"archive/zip"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/urfave/cli.v1"
)
func main() {
app := cli.NewApp()
app.Name = "build-lambda-zip"
app.Usage = "Put an executable into a zip file that works with AWS Lambda."
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Value: "",
Usage: "output file path for the zip. Defaults to the input file name.",
},
}
app.Action = func(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("No input provided")
}
inputExe := c.Args().First()
outputZip := c.String("output")
if outputZip == "" {
outputZip = fmt.Sprintf("%s.zip", filepath.Base(inputExe))
}
if err := compressExe(outputZip, inputExe); err != nil {
return fmt.Errorf("Failed to compress file: %v", err)
}
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
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)
}