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 c32343d..f99b7d4 100644 --- a/diff.go +++ b/diff.go @@ -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" ) @@ -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 } @@ -44,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 } @@ -57,11 +74,19 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error { 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 { 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=