Skip to content

Commit

Permalink
Merge pull request #56 from fujiwara/diff
Browse files Browse the repository at this point in the history
Add diff subcommand
  • Loading branch information
fujiwara authored Jul 1, 2020
2 parents d025822 + 36ac9ab commit 575af71
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ Commands:

archive [<flags>]
archive zip

logs [<flags>]
tail logs using `aws logs tail` (aws-cli v2 required)

diff
show display diff of function.json compared with latest function
```

### Init
Expand Down
7 changes: 7 additions & 0 deletions cmd/lambroll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ 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")
diffOption := lambroll.DiffOption{
FunctionFilePath: function,
}

command := kingpin.Parse()
if command == "version" {
fmt.Println("lambroll", Version)
Expand Down Expand Up @@ -120,6 +125,8 @@ func _main() int {
err = app.Archive(archiveOption)
case "logs":
err = app.Logs(logsOption)
case "diff":
err = app.Diff(diffOption)
}

if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lambroll

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/kylelemons/godebug/diff"
"github.com/pkg/errors"
)

// DiffOption represents options for Diff()
type DiffOption struct {
FunctionFilePath *string
}

// Diff prints diff of function.json compared with latest function
func (app *App) Diff(opt DiffOption) error {
newFunc, err := app.loadFunction(*opt.FunctionFilePath)
if err != nil {
return errors.Wrap(err, "failed to load function")
}
name := *newFunc.FunctionName

var latest *lambda.FunctionConfiguration
if res, err := app.lambda.GetFunction(&lambda.GetFunctionInput{
FunctionName: &name,
}); err != nil {
return errors.Wrapf(err, "failed to GetFunction %s", name)
} else {
latest = res.Configuration
}

arn := app.functionArn(name)
log.Printf("[debug] listing tags of %s", arn)
var tags Tags
if res, err := app.lambda.ListTags(&lambda.ListTagsInput{
Resource: aws.String(arn),
}); err != nil {
return errors.Wrap(err, "faled to list tags")
} else {
tags = res.Tags
}
latestFunc := newFuctionFrom(latest, tags)

latestJSON, _ := marshalJSON(latestFunc)
newJSON, _ := marshalJSON(newFunc)

if ds := diff.Diff(string(latestJSON), string(newJSON)); ds != "" {
fmt.Println("---", arn)
fmt.Println("+++", *opt.FunctionFilePath)
fmt.Println(ds)
}
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ require (
github.com/hashicorp/logutils v1.0.0
github.com/kayac/go-config v0.4.0
github.com/kayac/go-config/tfstate v0.0.0-20200701045006-992317101de7
github.com/kylelemons/godebug v1.1.0
github.com/pkg/errors v0.9.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/strftime v1.0.1 h1:o7qz5pmLzPDLyGW4lG6JvTKPUfTFXwe+vOamIYWtnVU=
Expand Down

0 comments on commit 575af71

Please sign in to comment.