From 837e48a83165ae9f01e51425d3df65d2db6395f7 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 14 Oct 2023 03:21:34 +0900 Subject: [PATCH 1/2] add diff -u by default --- diff.go | 20 ++++++++++++++++---- go.mod | 1 + go.sum | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/diff.go b/diff.go index c32343d..fe457d4 100644 --- a/diff.go +++ b/diff.go @@ -9,6 +9,9 @@ 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/service/lambda" @@ -19,6 +22,7 @@ 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"` ExcludeFileOption } @@ -58,10 +62,18 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error { latestJSON, _ := marshalJSON(latestFunc) newJSON, _ := marshalJSON(newFunc) - 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 { + remoteArn := app.functionArn(ctx, name) + 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("---" + app.functionArn(ctx, name))) + fmt.Println(color.GreenString("+++" + app.functionFilePath)) + fmt.Print(coloredDiff(ds)) + } } if err := validateUpdateFunction(latest, code, newFunc); err != nil { diff --git a/go.mod b/go.mod index 6142920..4f179f1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9605b1b..5ff08a9 100644 --- a/go.sum +++ b/go.sum @@ -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= From e64f399a655767d99474c57471b5208fe713373c Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 14 Oct 2023 04:01:51 +0900 Subject: [PATCH 2/2] diff --unified by default diff --qualifier compare with specfied quarlifer. --- archive.go | 2 +- diff.go | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/archive.go b/archive.go index 0eedee5..ec62780 100644 --- a/archive.go +++ b/archive.go @@ -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 } diff --git a/diff.go b/diff.go index fe457d4..f99b7d4 100644 --- a/diff.go +++ b/diff.go @@ -14,6 +14,7 @@ import ( "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" ) @@ -23,6 +24,7 @@ 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 } @@ -48,12 +50,23 @@ 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 } @@ -61,16 +74,16 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error { latestJSON, _ := marshalJSON(latestFunc) newJSON, _ := marshalJSON(newFunc) + remoteArn := app.functionArn(ctx, name) + ":" + opt.Qualifier if opt.Unified { - remoteArn := app.functionArn(ctx, name) 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("---" + app.functionArn(ctx, name))) + fmt.Println(color.RedString("---" + remoteArn)) fmt.Println(color.GreenString("+++" + app.functionFilePath)) fmt.Print(coloredDiff(ds)) }