Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire-once templates for go-lambda #16

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 37 additions & 7 deletions internal/config/go_lambda.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import (
"errors"
"fmt"
"os"

"github.com/ALT-F4-LLC/build-configs/internal/templates"
)

Expand All @@ -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(),
Expand Down Expand Up @@ -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)
}
31 changes: 31 additions & 0 deletions internal/templates/templates/go-lambda/cmd__[lambda]__main.go.tmpl
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading