Skip to content

Commit

Permalink
Merge pull request #38 from fujiwara/zip
Browse files Browse the repository at this point in the history
deploy --src accepts a zip file.
  • Loading branch information
fujiwara authored May 13, 2020
2 parents 6c5bf7f + d07fbf7 commit 35fe4ca
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Flags:
--region="ap-northeast-1" AWS region
--log-level=info log level (trace, debug, info, warn, error)
--function="function.json" Function file path
--src="." function zip archive src dir
--src="." function zip archive or src dir
--exclude-file=".lambdaignore"
exclude file
--dry-run dry run
Expand Down
27 changes: 26 additions & 1 deletion archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (app *App) Archive(opt DeployOption) error {
return errors.Wrap(err, "failed to validate deploy options")
}

zipfile, _, err := createZipArchive(*opt.SrcDir, opt.Excludes)
zipfile, _, err := createZipArchive(*opt.Src, opt.Excludes)
if err != nil {
return err
}
Expand All @@ -30,6 +30,31 @@ func (app *App) Archive(opt DeployOption) error {
return err
}

func loadZipArchive(src string) (*os.File, os.FileInfo, error) {
log.Printf("[info] reading zip archive from %s", src)
r, err := zip.OpenReader(src)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to open zip file %s", src)
}
for _, f := range r.File {
header := f.FileHeader
log.Printf("[debug] %s %10d %s %s",
header.Mode(),
header.UncompressedSize64,
header.Modified.Format(time.RFC3339),
header.Name,
)
}
r.Close()
info, err := os.Stat(src)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to stat %s", src)
}
log.Printf("[info] zip archive %d bytes", info.Size())
fh, err := os.Open(src)
return fh, info, err
}

// createZipArchive creates a zip archive
func createZipArchive(src string, excludes []string) (*os.File, os.FileInfo, error) {
log.Printf("[info] creating zip archive from %s", src)
Expand Down
20 changes: 20 additions & 0 deletions archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ func TestCreateZipArchive(t *testing.T) {
t.Errorf("too small file got %d bytes", info.Size())
}
}

func TestLoadZipArchive(t *testing.T) {
r, info, err := lambroll.LoadZipArchive("test/src.zip")
if err != nil {
t.Error("failed to LoadZipArchive", err)
}
defer r.Close()

if info.Size() < 100 {
t.Errorf("too small file got %d bytes", info.Size())
}
}

func TestLoadNotZipArchive(t *testing.T) {
_, _, err := lambroll.LoadZipArchive("test/src/hello.txt")
if err == nil {
t.Error("must be failed to load not a zip file")
}
t.Log(err)
}
4 changes: 2 additions & 2 deletions cmd/lambroll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func _main() int {
deploy := kingpin.Command("deploy", "deploy or create function")
deployOption := lambroll.DeployOption{
FunctionFilePath: function,
SrcDir: deploy.Flag("src", "function zip archive src dir").Default(".").String(),
Src: deploy.Flag("src", "function zip archive or src dir").Default(".").String(),
ExcludeFile: deploy.Flag("exclude-file", "exclude file").Default(lambroll.IgnoreFilename).String(),
DryRun: deploy.Flag("dry-run", "dry run").Bool(),
Publish: deploy.Flag("publish", "publish function").Default("true").Bool(),
Expand Down Expand Up @@ -69,7 +69,7 @@ func _main() int {

archive := kingpin.Command("archive", "archive zip")
archiveOption := lambroll.DeployOption{
SrcDir: archive.Flag("src", "function zip archive src dir").Default(".").String(),
Src: archive.Flag("src", "function src dir for archive").Default(".").String(),
ExcludeFile: archive.Flag("exclude-file", "exclude file").Default(lambroll.IgnoreFilename).String(),
}

Expand Down
22 changes: 18 additions & 4 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,26 @@ import (
var directUploadThreshold = int64(50 * 1024 * 1024) // 50MB

func (app *App) prepareFunctionCodeForDeploy(opt DeployOption, fn *Function) error {
zipfile, info, err := createZipArchive(*opt.SrcDir, opt.Excludes)
if err != nil {
return err
var (
zipfile *os.File
info os.FileInfo
)

if fi, err := os.Stat(*opt.Src); err != nil {
return errors.Wrapf(err, "src %s is not found", *opt.Src)
} else if fi.IsDir() {
zipfile, info, err = createZipArchive(fi.Name(), opt.Excludes)
if err != nil {
return err
}
defer os.Remove(zipfile.Name())
} else if !fi.IsDir() {
zipfile, info, err = loadZipArchive(fi.Name())
if err != nil {
return err
}
}
defer zipfile.Close()
defer os.Remove(zipfile.Name())

if fn.Code != nil {
if bucket, key := fn.Code.S3Bucket, fn.Code.S3Key; bucket != nil && key != nil {
Expand Down
2 changes: 1 addition & 1 deletion deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// DeployOption represens an option for Deploy()
type DeployOption struct {
FunctionFilePath *string
SrcDir *string
Src *string
Excludes []string
ExcludeFile *string
Publish *bool
Expand Down
1 change: 1 addition & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package lambroll

var (
CreateZipArchive = createZipArchive
LoadZipArchive = loadZipArchive
MergeTags = mergeTags
)
Binary file added test/src.zip
Binary file not shown.

0 comments on commit 35fe4ca

Please sign in to comment.