From 2380f6395364d64f683f3b79652bc27aadb7a00d Mon Sep 17 00:00:00 2001 From: Hayden <22327045+hbjydev@users.noreply.github.com> Date: Fri, 10 May 2024 12:34:14 +0100 Subject: [PATCH] Fire-once templates for go-lambda (#16) * feat: added a fire-once entrypoint template for the go-lambda template * docs: add docs about init for go-lambda and go-cobra-cli * fix: resolve issue with imports being picked up from template --- README.md | 14 ++++++ internal/config/go_lambda.go | 44 ++++++++++++++++--- .../go-lambda/cmd__[lambda]__main.go.tmpl | 31 +++++++++++++ 3 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 internal/templates/templates/go-lambda/cmd__[lambda]__main.go.tmpl diff --git a/README.md b/README.md index 51945a7..1d4b4dc 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,20 @@ to skip templating: flake.nix ``` +## Templates + +### `go-cobra-cli` + +`go-cobra-cli` scaffolds a Cobra CLI tool with Go that can be used to build out +internal tooling. This is the template this CLI uses. + +### `go-lambda` + +`go-lambda` scaffolds an lambda setup in Go. + +To use it, you need to run the generator and then run `go mod init` and +`go mod tidy`, then update your `vendorHash`. + ## Examples Some example configurations for our template types exist in the diff --git a/internal/config/go_lambda.go b/internal/config/go_lambda.go index 32242fc..1596919 100644 --- a/internal/config/go_lambda.go +++ b/internal/config/go_lambda.go @@ -1,6 +1,10 @@ package config import ( + "errors" + "fmt" + "os" + "github.com/ALT-F4-LLC/build-configs/internal/templates" ) @@ -20,13 +24,14 @@ type GoLambdaConfig struct { func NewGoLambdaConfig(c Config) GoLambdaConfig { return GoLambdaConfig{ - Config: c, - GoVersion: "1.22", - Lint: NewGolangCiLintConfig(), - Quirk: NewQuirkConfig(c), - Deploy: []DeployConfig{}, - Lambdas: []string{c.Name}, - OpenAPI: NewOpenAPIConfig(), + Config: c, + GoVersion: "1.22", + PrivateModules: "github.com/ALT-F4-LLC/quirk-service-kit", + Lint: NewGolangCiLintConfig(), + Quirk: NewQuirkConfig(c), + Deploy: []DeployConfig{}, + Lambdas: []string{c.Name}, + OpenAPI: NewOpenAPIConfig(), Nix: NixGoConfig{ NixConfig: NewNixConfig(), @@ -64,5 +69,30 @@ func (c GoLambdaConfig) Render() error { return err } + // Create lambda entrypoints on first template run + for _, lambda := range c.Lambdas { + entry := fmt.Sprintf("cmd/%s/main.go", lambda) + + if _, err := os.Stat(entry); err != nil { + if errors.Is(err, os.ErrNotExist) { + out, err := templates.RenderTemplate( + templates.GoLambdaTemplates, + "cmd/[lambda]/main.go.tmpl", + c, + ) + if err != nil { + return err + } + files[entry] = out + } else { + // Error was unexpected + return err + } + } else { + // File exists + continue + } + } + return templates.WriteFiles(files) } diff --git a/internal/templates/templates/go-lambda/cmd__[lambda]__main.go.tmpl b/internal/templates/templates/go-lambda/cmd__[lambda]__main.go.tmpl new file mode 100644 index 0000000..16af427 --- /dev/null +++ b/internal/templates/templates/go-lambda/cmd__[lambda]__main.go.tmpl @@ -0,0 +1,31 @@ +package main + +import ( + "context" + + "github.com/ALT-F4-LLC/quirk-service-kit/config" + "github.com/ALT-F4-LLC/quirk-service-kit/lambda" + "github.com/ALT-F4-LLC/quirk-service-kit/telemetry" + "github.com/ALT-F4-LLC/quirk-service-kit/aws" + "github.com/aws/aws-lambda-go/events" +) + +type Config struct { + *config.BaseConfig +} + +var cfg Config + +func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) { + // l := telemetry.GetLogger(ctx) + // awsCfg := aws.GetConfig(ctx) + // env := cfg.BaseConfig.Environment + + return lambda.NewOkResponse(), nil +} + +func main() { + if err := lambda.Start(Handler, &Config{}); err != nil { + panic(err) + } +}