Skip to content

Commit

Permalink
go-lambda template updates (#14)
Browse files Browse the repository at this point in the history
* feat: update go-lambda to support openapi clients

* feat: update go-lambda to include env import from nix/default.nix

* feat: nix-based bootstrap zip creation
  • Loading branch information
hbjydev authored May 6, 2024
1 parent ec6b3be commit edb8cc4
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 33 deletions.
2 changes: 2 additions & 0 deletions examples/go-lambda/build-configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ parameters:
vendorHash: ''
goPackage: go_1_22
buildGoModule: buildGo122Module
openapi:
enable: true
13 changes: 11 additions & 2 deletions internal/config/go_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type GoLambdaConfig struct {
Deploy DeployConfig `json:"deploy,omitempty" yaml:"deploy,omitempty"`
PrivateModules string `json:"privateModules,omitempty" yaml:"privateModules,omitempty"`
Lambdas []string `json:"lambdas,omitempty" yaml:"lambdas,omitempty"`
OpenAPI OpenAPIConfig `json:"openapi,omitempty" yaml:"openapi,omitempty"`
}

func NewGoLambdaConfig(c Config) GoLambdaConfig {
Expand All @@ -23,6 +24,7 @@ func NewGoLambdaConfig(c Config) GoLambdaConfig {
Quirk: NewQuirkConfig(c),
Deploy: NewDeployConfig(),
Lambdas: []string{c.Name},
OpenAPI: NewOpenAPIConfig(),

Nix: NixGoConfig{
NixConfig: NewNixConfig(),
Expand All @@ -33,7 +35,7 @@ func NewGoLambdaConfig(c Config) GoLambdaConfig {
}

func (c GoLambdaConfig) Render() error {
files, err := templates.RenderTemplates(templates.RenderMap{
renderMap := templates.RenderMap{
templates.AllCommonTemplates: {
".envrc",
},
Expand All @@ -45,10 +47,17 @@ func (c GoLambdaConfig) Render() error {
templates.GoLambdaTemplates: {
".github/workflows/flake.yaml",
"nix/lambda.nix",
"nix/default.nix",
"flake.nix",
"justfile",
},
}, c)
}

if c.OpenAPI.Enable {
renderMap[templates.GoLambdaTemplates] = append(renderMap[templates.GoLambdaTemplates], "nix/client.nix")
}

files, err := templates.RenderTemplates(renderMap, c)
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions internal/config/openapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

type OpenAPIConfig struct {
Enable bool `json:"enable" yaml:"enable"`
}

func NewOpenAPIConfig() OpenAPIConfig {
return OpenAPIConfig{
Enable: false,
}
}
29 changes: 15 additions & 14 deletions internal/templates/templates/go-lambda/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@

perSystem = { config, pkgs, ... }:
let
inherit (pkgs)
{{ .Nix.GoPackage }}
just;

lambdas = {
{{- range $fn := .Lambdas }}
{{ $fn }} = pkgs.callPackage ./nix/lambda.nix { name = "{{ $fn }}"; };
{{- end }}
};
inherit (import ./nix) env lambda{{ if .OpenAPI.Enable }} client{{ end }};
inherit (pkgs) go-migrate golangci-lint just zip;
in
{
devShells.default = pkgs.mkShell {
{{ if .PrivateModules -}}
GOPRIVATE = "{{ .PrivateModules }}";
{{- end }}
buildInputs = [ just ];
inherit (env) CGO_ENABLED{{ if .PrivateModules }} GOPRIVATE{{ end }};
nativeBuildInputs = [ go-migrate golangci-lint just zip ];
inputsFrom = [
{{- range .Lambdas }}
config.packages.{{.}}
{{- end }}
{{- if .OpenAPI.Enable }}
config.packages.client
{{- end }}
];
};

packages = { } // lambdas;
packages = {
{{- range $fn := .Lambdas }}
{{ $fn }} = pkgs.callPackage lambda { name = "{{ $fn }}"; };
{{- end }}
{{- if .OpenAPI.Enable }}
client = pkgs.callPackage client {};
{{- end }}
};
};
};
}
15 changes: 12 additions & 3 deletions internal/templates/templates/go-lambda/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ check:
package profile{{ if .PrivateModules }} netrc="/tmp/.netrc=/tmp/.netrc"{{ end }}:
#!/usr/bin/env bash
set -euxo pipefail
DERIVATION=$(just build "{{"{{"}} profile {{"}}"}}" "{{"{{"}} netrc {{"}}"}}")
DERIVATION=$(just build "{{"{{"}} profile {{"}}"}}.bootstrap" "{{"{{"}} netrc {{"}}"}}")
OUTPUT=$(echo $DERIVATION | jq -r ".[0].outputs.out")
mkdir -p dist/{{"{{"}} profile {{"}}"}}
install -m 755 $OUTPUT/bin/{{"{{"}} profile {{"}}"}} dist/{{"{{"}} profile {{"}}"}}/bootstrap
(cd dist/{{"{{"}} profile {{"}}"}} && zip lambda.zip bootstrap)
cp --no-preserve=mode $OUTPUT dist/{{"{{"}} profile {{"}}"}}/lambda.zip
package-all{{ if .PrivateModules }} netrc="/tmp/.netrc=/tmp/.netrc"{{ end }}:
{{- range .Lambdas }}
Expand Down Expand Up @@ -54,3 +53,13 @@ deploy-all environment:
{{- range .Lambdas }}
just deploy "{{"{{"}} environment {{"}}"}}" "{{.}}"
{{- end }}

{{ if .OpenAPI.Enable -}}
build-client:
#!/usr/bin/env bash
DERIVATION=$(just build client "{{"{{"}} netrc {{"}}"}}")
OUTPUT=$(echo $DERIVATION | jq -r ".[0].outputs.out")
rm -rf pkg/client
cp -r --no-preserve=mode $OUTPUT pkg/client
git add pkg/client
{{- end }}
26 changes: 26 additions & 0 deletions internal/templates/templates/go-lambda/nix__client.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ runCommand, openapi-generator-cli }:
runCommand "client"
{
src = ../api;
buildInputs = [ openapi-generator-cli ];
} ''
mkdir -p $out
openapi-generator-cli generate \
-i $src/openapi.yaml \
-g go \
-o $out \
-p packageName="client" \
-p packageVersion="0.1.0"
rm -rf \
$out/.gitignore \
$out/.openapi-generator \
$out/.openapi-generator-ignore \
$out/.travis.yml \
$out/README.md \
$out/api \
$out/docs \
$out/git_push.sh \
$out/test
'';
14 changes: 14 additions & 0 deletions internal/templates/templates/go-lambda/nix__default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
rec {
env = {
CGO_ENABLED = 0;
{{- if .PrivateModules }}
GOPRIVATE = "{{ .PrivateModules }}";
{{- end }}
};

lambda = import ./lambda.nix { inherit env; };

{{- if .OpenAPI.Enable }}
client = import ./client.nix;
{{- end }}
}
39 changes: 25 additions & 14 deletions internal/templates/templates/go-lambda/nix__lambda.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
{ env }:

{
{{ .Nix.BuildGoModule }},
name,
runCommand,
}:

{{ .Nix.BuildGoModule }} {
inherit name;
ldflags = ["-s" "-w"];
src = ../.;
subPackages = ["cmd/${name}"];
tags = ["lambda.norpc"];
vendorHash = "";
let
pkg = {{ .Nix.BuildGoModule }} {
inherit name;
inherit (env) CGO_ENABLED{{ if .PrivateModules }} GOPRIVATE{{ end }};
ldflags = ["-s" "-w"];
src = ../.;
subPackages = ["cmd/${name}"];
tags = ["lambda.norpc"];
vendorHash = "";

{{ if .PrivateModules -}}
GOPRIVATE = "{{ .PrivateModules }}";
{{- end }}
preBuild = ''
export HOME=/tmp
ls -alh $HOME/.netrc
'';
};

preBuild = ''
export HOME=/tmp
ls -alh $HOME/.netrc
bootstrap = runCommand "${name}-bootstrap" {
buildInputs = [ zip ];
} ''
(cd ${pkg}/bin && zip $out ${name})
'';
}
in
pkg.overrideAttrs (final: {
passthru.bootstrap = bootstrap;
});

0 comments on commit edb8cc4

Please sign in to comment.