Skip to content

Commit

Permalink
Merge pull request #328 from fujiwara/unified-diff
Browse files Browse the repository at this point in the history
add diff -u by default
  • Loading branch information
fujiwara authored Oct 19, 2023
2 parents 0df0157 + e64f399 commit 54fb48d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type ArchiveOption struct {
Src string `help:"function zip archive or src dir" default:"."`
Dest string `help:"destination file path" default:"archive.zip"`
Dest string `help:"destination file path" default:"function.zip"`

ExcludeFileOption
}
Expand Down
35 changes: 30 additions & 5 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import (
"strings"

"github.com/fatih/color"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/kylelemons/godebug/diff"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
)
Expand All @@ -19,6 +23,8 @@ import (
type DiffOption struct {
Src string `help:"function zip archive or src dir" default:"."`
CodeSha256 bool `help:"diff of code sha256" default:"false"`
Unified bool `help:"unified diff" default:"true" negatable:"" short:"u"`
Qualifier string `help:"compare with" default:"$LATEST"`

ExcludeFileOption
}
Expand All @@ -44,24 +50,43 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error {
var packageType types.PackageType
if res, err := app.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: &name,
Qualifier: &opt.Qualifier,
}); err != nil {
return fmt.Errorf("failed to GetFunction %s: %w", name, err)
} else {
latest = res.Configuration
code = res.Code
tags = res.Tags
{
res, err := app.lambda.ListTags(ctx, &lambda.ListTagsInput{
// Tagging operations are permitted on Lambda functions only.
// Tags on aliases and versions are not supported.
Resource: aws.String(app.functionArn(ctx, name)),
})
if err != nil {
return fmt.Errorf("faled to list tags: %w", err)
}
tags = res.Tags
}
currentCodeSha256 = *res.Configuration.CodeSha256
packageType = res.Configuration.PackageType
}
latestFunc := newFunctionFrom(latest, code, tags)

latestJSON, _ := marshalJSON(latestFunc)
newJSON, _ := marshalJSON(newFunc)
remoteArn := app.functionArn(ctx, name) + ":" + opt.Qualifier

if ds := diff.Diff(string(latestJSON), string(newJSON)); ds != "" {
fmt.Println(color.RedString("---" + app.functionArn(ctx, name)))
fmt.Println(color.GreenString("+++" + app.functionFilePath))
fmt.Println(coloredDiff(ds))
if opt.Unified {
edits := myers.ComputeEdits(span.URIFromPath(remoteArn), string(latestJSON), string(newJSON))
if ds := fmt.Sprint(gotextdiff.ToUnified(remoteArn, app.functionFilePath, string(latestJSON), edits)); ds != "" {
fmt.Print(coloredDiff(ds))
}
} else {
if ds := diff.Diff(string(latestJSON), string(newJSON)); ds != "" {
fmt.Println(color.RedString("---" + remoteArn))
fmt.Println(color.GreenString("+++" + app.functionFilePath))
fmt.Print(coloredDiff(ds))
}
}

if err := validateUpdateFunction(latest, code, newFunc); err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/google/go-cmp v0.5.9
github.com/google/go-jsonnet v0.20.0
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e
github.com/hexops/gotextdiff v1.0.3
github.com/kayac/go-config v0.6.0
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-isatty v0.0.17
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d h1:9ARUJJ1VVynB176G1HCwleORqCaXm/Vx0uUi0dL26I0=
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/itchyny/gojq v0.12.11 h1:YhLueoHhHiN4mkfM+3AyJV6EPcCxKZsOnYf+aVSwaQw=
Expand Down

0 comments on commit 54fb48d

Please sign in to comment.