Skip to content

Commit

Permalink
Fire-once templates for go-lambda (#16)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hbjydev committed May 10, 2024
1 parent 2ec29ea commit 2380f63
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
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)
}
}

0 comments on commit 2380f63

Please sign in to comment.