From 0f5193a86f16da5fc4d7aba3402b13feb09ea29f Mon Sep 17 00:00:00 2001 From: David Karlsson <35727626+dvdksn@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:32:12 +0100 Subject: [PATCH] Revert "releaser: get redirects from files instead of env vars" --- _releaser/Dockerfile | 6 +++--- _releaser/aws.go | 23 ++++++++++++++--------- layouts/index.redirects.json | 3 +-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/_releaser/Dockerfile b/_releaser/Dockerfile index 9d15b86c4f..5b945dd596 100644 --- a/_releaser/Dockerfile +++ b/_releaser/Dockerfile @@ -5,7 +5,7 @@ ARG GO_VERSION=1.19 FROM scratch AS sitedir FROM golang:${GO_VERSION}-alpine AS base -RUN apk add --no-cache openssl +RUN apk add --no-cache jq openssl ENV CGO_ENABLED=0 WORKDIR /src COPY go.mod go.sum ./ @@ -49,6 +49,6 @@ RUN --mount=type=bind,target=. \ --mount=type=secret,id=AWS_SECRET_ACCESS_KEY \ --mount=type=secret,id=AWS_SESSION_TOKEN \ AWS_LAMBDA_FUNCTION_FILE=cloudfront-lambda-redirects.js \ - REDIRECTS_FILE="/site/redirects.json" \ - REDIRECTS_PREFIXES_FILE="redirects-prefixes.json" \ + REDIRECTS_JSON=$(jq -c '.' /site/redirects.json) \ + REDIRECTS_PREFIXES_JSON=$(jq -c '.' redirects-prefixes.json) \ releaser aws cloudfront-update diff --git a/_releaser/aws.go b/_releaser/aws.go index 5e1714310e..3f79c2bd2e 100644 --- a/_releaser/aws.go +++ b/_releaser/aws.go @@ -98,17 +98,17 @@ func (s *AwsLambdaInvokeCmd) Run() error { type AwsCloudfrontUpdateCmd struct { Region string `kong:"name='region',env='AWS_REGION'"` Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"` - FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE',type=filecontent"` + FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"` CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"` - RedirectsFile string `kong:"name='redirects-file',env='REDIRECTS_FILE',type=filecontent"` - RedirectsPrefixesFile string `kong:"name='redirects-prefixes-file',env='REDIRECTS_PREFIXES_FILE',type=filecontent"` + RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"` + RedirectsPrefixesJSON string `kong:"name='redirects-prefixes-json',env='REDIRECTS_PREFIXES_JSON'"` } func (s *AwsCloudfrontUpdateCmd) Run() error { var err error ver := time.Now().UTC().Format(time.RFC3339) - zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsFile, s.RedirectsPrefixesFile) + zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON, s.RedirectsPrefixesJSON) if err != nil { return fmt.Errorf("cannot create lambda function zip: %w", err) } @@ -228,15 +228,20 @@ func (s *AwsCloudfrontUpdateCmd) Run() error { return nil } -func getLambdaFunctionZip(funcdt, redirects, redirectsPrefixes string) ([]byte, error) { +func getLambdaFunctionZip(funcFilename string, redirectsJSON string, redirectsPrefixesJSON string) ([]byte, error) { + funcdt, err := os.ReadFile(funcFilename) + if err != nil { + return nil, fmt.Errorf("failed to read lambda function file %q: %w", funcFilename, err) + } + var funcbuf bytes.Buffer - functpl := template.Must(template.New("").Parse(funcdt)) - if err := functpl.Execute(&funcbuf, struct { + functpl := template.Must(template.New("").Parse(string(funcdt))) + if err = functpl.Execute(&funcbuf, struct { RedirectsJSON string RedirectsPrefixesJSON string }{ - redirects, - redirectsPrefixes, + redirectsJSON, + redirectsPrefixesJSON, }); err != nil { return nil, err } diff --git a/layouts/index.redirects.json b/layouts/index.redirects.json index abf7111aca..8136b17c13 100644 --- a/layouts/index.redirects.json +++ b/layouts/index.redirects.json @@ -12,5 +12,4 @@ {{- $redirects.SetInMap "paths" . $target -}} {{- end -}} {{- end -}} -{{- $opts := dict "noHTMLEscape" true }} -{{- $redirects.Get "paths" | jsonify $opts }} +{{ $redirects.Get "paths" | jsonify }}