Skip to content

Commit

Permalink
Merge pull request #232 from fujiwara/diff-code-sha
Browse files Browse the repository at this point in the history
implements for diff --code
  • Loading branch information
fujiwara authored Jun 10, 2022
2 parents 4f59103 + 698c11b commit 36b506f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 37 deletions.
6 changes: 4 additions & 2 deletions archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (

// Archive archives zip
func (app *App) Archive(opt DeployOption) error {
if err := (&opt).Expand(); err != nil {
return errors.Wrap(err, "failed to validate deploy options")
excludes, err := expandExcludeFile(*opt.ExcludeFile)
if err != nil {
return errors.Wrap(err, "failed to parse exclude file")
}
opt.Excludes = append(opt.Excludes, excludes...)

zipfile, _, err := createZipArchive(*opt.Src, opt.Excludes)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/lambroll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ func _main() int {
FilterPattern: logs.Flag("filter-pattern", "The filter pattern to use").Default("").String(),
}

kingpin.Command("diff", "show display diff of function.json compared with latest function")
diff := kingpin.Command("diff", "show display diff of function.json compared with latest function")
diffOption := lambroll.DiffOption{
FunctionFilePath: function,
CodeSha256: diff.Flag("code", "diff of code sha256").Default("false").Bool(),
ExcludeFile: diff.Flag("exclude-file", "exclude file").Default(lambroll.IgnoreFilename).String(),
Src: diff.Flag("src", "function zip archive or src dir").Default(".").String(),
}

versions := kingpin.Command("versions", "manage function versions")
Expand Down
41 changes: 22 additions & 19 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 {
var (
zipfile *os.File
info os.FileInfo
)
func prepareZipfile(src string, excludes []string) (*os.File, os.FileInfo, error) {
if fi, err := os.Stat(src); err != nil {
return nil, nil, errors.Wrapf(err, "src %s is not found", src)
} else if fi.IsDir() {
zipfile, info, err := createZipArchive(src, excludes)
if err != nil {
return nil, nil, err
}
return zipfile, info, nil
} else if !fi.IsDir() {
zipfile, info, err := loadZipArchive(src)
if err != nil {
return nil, nil, err
}
return zipfile, info, nil
}
return nil, nil, fmt.Errorf("src %s is not found", src)
}

func (app *App) prepareFunctionCodeForDeploy(opt DeployOption, fn *Function) error {
if aws.StringValue(fn.PackageType) == packageTypeImage {
if fn.Code == nil || fn.Code.ImageUri == nil {
return errors.New("PackageType=Image requires Code.ImageUri in function definition")
Expand All @@ -36,20 +50,9 @@ func (app *App) prepareFunctionCodeForDeploy(opt DeployOption, fn *Function) err
return nil
}

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

Expand Down
22 changes: 11 additions & 11 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,25 @@ type versionAlias struct {
}

// Expand expands ExcludeFile contents to Excludes
func (opt *DeployOption) Expand() error {
if opt.ExcludeFile == nil {
return nil
}
b, err := ioutil.ReadFile(*opt.ExcludeFile)
func expandExcludeFile(file string) ([]string, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return nil
return nil, nil
}
return err
return nil, err
}
lines := bytes.Split(b, []byte{'\n'})
excludes := make([]string, 0, len(lines))
for _, line := range lines {
line = bytes.TrimSpace(line)
if len(line) == 0 || bytes.HasPrefix(line, []byte{'#'}) {
// skip blank or comment line
continue
}
opt.Excludes = append(opt.Excludes, string(line))
excludes = append(excludes, string(line))
}
return nil
return excludes, nil
}

func (opt *DeployOption) String() string {
Expand All @@ -72,9 +70,11 @@ func (opt *DeployOption) String() string {
// Deploy deployes a new lambda function code
func (app *App) Deploy(opt DeployOption) error {
ctx := context.Background()
if err := (&opt).Expand(); err != nil {
return errors.Wrap(err, "failed to validate deploy options")
excludes, err := expandExcludeFile(*opt.ExcludeFile)
if err != nil {
return errors.Wrap(err, "failed to parse exclude-file")
}
opt.Excludes = append(opt.Excludes, excludes...)
log.Printf("[debug] %s", opt.String())

fn, err := app.loadFunction(*opt.FunctionFilePath)
Expand Down
3 changes: 2 additions & 1 deletion deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ func TestDeployOptionExpand(t *testing.T) {
opt := lambroll.DeployOption{
ExcludeFile: &file,
}
err := (&opt).Expand()
excludes, err := lambroll.ExpandExcludeFile(*opt.ExcludeFile)
if err != nil {
t.Error("failed to expand", err)
}
opt.Excludes = append(opt.Excludes, excludes...)
if len(opt.Excludes) != len(expectExcludes) {
t.Errorf("unexpeted expanded excludes %#v", opt.Excludes)
}
Expand Down
39 changes: 39 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package lambroll

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/fatih/color"
"github.com/kylelemons/godebug/diff"
Expand All @@ -13,10 +17,20 @@ import (
// DiffOption represents options for Diff()
type DiffOption struct {
FunctionFilePath *string
Src *string
Excludes []string
CodeSha256 *bool
ExcludeFile *string
}

// Diff prints diff of function.json compared with latest function
func (app *App) Diff(opt DiffOption) error {
excludes, err := expandExcludeFile(*opt.ExcludeFile)
if err != nil {
return errors.Wrap(err, "failed to parse exclude-file")
}
opt.Excludes = append(opt.Excludes, excludes...)

newFunc, err := app.loadFunction(*opt.FunctionFilePath)
if err != nil {
return errors.Wrap(err, "failed to load function")
Expand All @@ -28,6 +42,7 @@ func (app *App) Diff(opt DiffOption) error {
var code *lambda.FunctionCodeLocation

var tags Tags
var currentCodeSha256, packageType string
if res, err := app.lambda.GetFunction(&lambda.GetFunctionInput{
FunctionName: &name,
}); err != nil {
Expand All @@ -36,6 +51,8 @@ func (app *App) Diff(opt DiffOption) error {
latest = res.Configuration
code = res.Code
tags = res.Tags
currentCodeSha256 = *res.Configuration.CodeSha256
packageType = *res.Configuration.PackageType
}
latestFunc := newFunctionFrom(latest, code, tags)

Expand All @@ -51,6 +68,28 @@ func (app *App) Diff(opt DiffOption) error {
if err := validateUpdateFunction(latest, code, newFunc); err != nil {
return err
}

if aws.BoolValue(opt.CodeSha256) {
if strings.ToLower(packageType) != "zip" {
return errors.New("code-sha256 is only supported for Zip package type")
}
zipfile, _, err := prepareZipfile(*opt.Src, opt.Excludes)
if err != nil {
return err
}
h := sha256.New()
if _, err := io.Copy(h, zipfile); err != nil {
return err
}
newCodeSha256 := base64.StdEncoding.EncodeToString(h.Sum(nil))
prefix := "CodeSha256: "
if ds := diff.Diff(prefix+currentCodeSha256, prefix+newCodeSha256); ds != "" {
fmt.Println(color.RedString("---" + app.functionArn(name)))
fmt.Println(color.GreenString("+++" + "--src=" + *opt.Src))
fmt.Println(coloredDiff(ds))
}
}

return nil
}

Expand Down
7 changes: 4 additions & 3 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package lambroll

var (
CreateZipArchive = createZipArchive
LoadZipArchive = loadZipArchive
MergeTags = mergeTags
CreateZipArchive = createZipArchive
ExpandExcludeFile = expandExcludeFile
LoadZipArchive = loadZipArchive
MergeTags = mergeTags
)

0 comments on commit 36b506f

Please sign in to comment.