From 4c4da773cc7e71f647934c93abd7f94ed6efbd3a Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 27 Jan 2024 09:48:07 +0900 Subject: [PATCH 1/2] Update deploy.go, diff.go, and render.go changes to the FunctionURL field in each file, adding an environment variable for LAMBROLL_FUNCTION_URL. --- deploy.go | 2 +- diff.go | 4 ++-- render.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deploy.go b/deploy.go index 896494e..bf0c4cf 100644 --- a/deploy.go +++ b/deploy.go @@ -23,7 +23,7 @@ type DeployOption struct { DryRun bool `help:"dry run" default:"false"` SkipArchive bool `help:"skip to create zip archive. requires Code.S3Bucket and Code.S3Key in function definition" default:"false"` KeepVersions int `help:"Number of latest versions to keep. Older versions will be deleted. (Optional value: default 0)." default:"0"` - FunctionURL string `help:"path to function-url definiton" default:""` + FunctionURL string `help:"path to function-url definiton" default:"" env:"LAMBROLL_FUNCTION_URL"` SkipFunction bool `help:"skip to deploy a function. deploy function-url only" default:"false"` ExcludeFileOption diff --git a/diff.go b/diff.go index 89e5550..37ddd11 100644 --- a/diff.go +++ b/diff.go @@ -26,8 +26,8 @@ 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"` - FunctionURL string `help:"path to function-url definiton" default:""` + Qualifier *string `help:"the qualifier to compare"` + FunctionURL string `help:"path to function-url definiton" default:"" env:"LAMBROLL_FUNCTION_URL"` ExcludeFileOption } diff --git a/render.go b/render.go index 1c4c102..d2ef9fa 100644 --- a/render.go +++ b/render.go @@ -8,7 +8,7 @@ import ( type RenderOption struct { Jsonnet bool `default:"false" help:"render function.json as jsonnet"` - FunctionURL string `help:"render function-url definiton file" default:""` + FunctionURL string `help:"render function-url definiton file" default:"" env:"LAMBROLL_FUNCTION_URL"` } // Invoke invokes function From 0f925be21f337079b283700d87fbdb85e9aaa13f Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 27 Jan 2024 09:48:50 +0900 Subject: [PATCH 2/2] Add output formatting options to status command --- status.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++---- versions.go | 2 +- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/status.go b/status.go index 2f26fa2..85742a5 100644 --- a/status.go +++ b/status.go @@ -2,15 +2,50 @@ package lambroll import ( "context" + "errors" "fmt" + "strings" "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" + "github.com/olekukonko/tablewriter" ) // StatusOption represents options for Status() type StatusOption struct { Qualifier *string `help:"compare with"` + Output string `help:"output format" default:"table" enum:"table,json"` +} + +type StatusOutput struct { + FunctionName string `json:"FunctionName"` + FunctionArn string `json:"FunctionArn"` + Version string `json:"Version"` + Runtime string `json:"Runtime,omitempty"` + PackageType string `json:"PackageType"` + State string `json:"State"` + LastUpdateState string `json:"LastUpdateState"` + FunctionURL string `json:"FunctionURL,omitempty"` +} + +func (o *StatusOutput) String() string { + buf := new(strings.Builder) + w := tablewriter.NewWriter(buf) + w.Append([]string{"FunctionName", o.FunctionName}) + w.Append([]string{"FunctionArn", o.FunctionArn}) + w.Append([]string{"Version", o.Version}) + if o.Runtime != "" { + w.Append([]string{"Runtime", o.Runtime}) + } + w.Append([]string{"PackageType", o.PackageType}) + w.Append([]string{"State", o.State}) + w.Append([]string{"LastUpdateState", o.LastUpdateState}) + if o.FunctionURL != "" { + w.Append([]string{"FunctionURL", o.FunctionURL}) + } + w.Render() + return buf.String() } // Status prints status of function @@ -28,10 +63,34 @@ func (app *App) Status(ctx context.Context, opt *StatusOption) error { if err != nil { return fmt.Errorf("failed to GetFunction %s: %w", name, err) } - fmt.Printf("FunctionName: %s\n", aws.ToString(res.Configuration.FunctionName)) - fmt.Printf("Version: %s\n", aws.ToString(res.Configuration.Version)) - fmt.Printf("FunctionArn: %s\n", aws.ToString(res.Configuration.FunctionArn)) - fmt.Printf("State: %s\n", string(res.Configuration.State)) - fmt.Printf("LastUpdateStatus: %s\n", string(res.Configuration.LastUpdateStatus)) + out := &StatusOutput{ + FunctionName: aws.ToString(res.Configuration.FunctionName), + FunctionArn: aws.ToString(res.Configuration.FunctionArn), + Version: aws.ToString(res.Configuration.Version), + Runtime: string(res.Configuration.Runtime), + PackageType: string(res.Configuration.PackageType), + State: string(res.Configuration.State), + LastUpdateState: string(res.Configuration.LastUpdateStatus), + } + if res, err := app.lambda.GetFunctionUrlConfig(ctx, &lambda.GetFunctionUrlConfigInput{ + FunctionName: &name, + Qualifier: opt.Qualifier, + }); err != nil { + var nfe *types.ResourceNotFoundException + if errors.As(err, &nfe) { + // do nothing + } else { + return fmt.Errorf("failed to GetFunctionUrlConfig %s: %w", name, err) + } + } else { + out.FunctionURL = aws.ToString(res.FunctionUrl) + } + switch opt.Output { + case "table": + fmt.Print(out.String()) + case "json": + b, _ := marshalJSON(out) + fmt.Print(string(b)) + } return nil } diff --git a/versions.go b/versions.go index faf02e0..fe8fc0d 100644 --- a/versions.go +++ b/versions.go @@ -17,7 +17,7 @@ import ( // VersionsOption represents options for Versions() type VersionsOption struct { - Output string `default:"table" enum:"table,json,tsv" help:"output format"` + Output string `default:"table" enum:"table,json,tsv" help:"output format (table,json,tsv)"` Delete bool `default:"false" help:"delete older versions"` KeepVersions int `default:"0" help:"Number of latest versions to keep. Older versions will be deleted with --delete."` }