Skip to content

Commit

Permalink
initial attempt to move templates as embedded content
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Dec 29, 2023
1 parent d2e405c commit d549807
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
3 changes: 1 addition & 2 deletions application/lambda/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -58,7 +57,7 @@ func FromTemplate(ctx context.Context, template templates.Template, path string)
if err != nil {
return nil, fmt.Errorf("create file %s directory: %w", fileName, err)
}
err = ioutil.WriteFile(destFile, []byte(content), 0755)
err = os.WriteFile(destFile, []byte(content), 0755)
if err != nil {
return nil, fmt.Errorf("write file %s content: %w", fileName, err)
}
Expand Down
3 changes: 1 addition & 2 deletions application/lambda/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -173,7 +172,7 @@ func (local *localLambda) reloadManifest() error {
}

func (local *localLambda) readIgnore() ([]string, error) {
content, err := ioutil.ReadFile(filepath.Join(local.rootDir, internal.CGIIgnore))
content, err := os.ReadFile(filepath.Join(local.rootDir, internal.CGIIgnore))
if err == nil {
return strings.Split(string(content), "\n"), nil
}
Expand Down
1 change: 1 addition & 0 deletions templates/assets/python/.cgiignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
3 changes: 3 additions & 0 deletions templates/assets/python/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
install:
python3 -m venv venv
./venv/bin/pip install -r requirements.txt
6 changes: 6 additions & 0 deletions templates/assets/python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import json

request = json.load(sys.stdin)
response = ['hello', 'world']
json.dump(response, sys.stdout)
1 change: 1 addition & 0 deletions templates/assets/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
48 changes: 39 additions & 9 deletions templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ package templates

import (
"context"
"embed"
"encoding/json"
"github.com/reddec/trusted-cgi/internal"
"github.com/reddec/trusted-cgi/types"
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/reddec/trusted-cgi/internal"
"github.com/reddec/trusted-cgi/types"
)

//go:embed assets/**
var assets embed.FS

func Read(filename string) (*Template, error) {
f, err := os.Open(filename)
if err != nil {
Expand All @@ -29,7 +36,7 @@ type Template struct {
Manifest types.Manifest `json:"manifest" yaml:"manifest"` // manifest to copy
PostClone string `json:"post_clone,omitempty" yaml:"post_clone"` // action (make target) name that should be invoked after clone
Check [][]string `json:"check,omitempty" yaml:"check,omitempty"` // check availability (one line - one check)
Files map[string]string `json:"files" yaml:"files,omitempty"` //only for embedded
Files map[string]string `json:"files,omitempty"`
}

func (t *Template) IsAvailable(ctx context.Context) bool {
Expand Down Expand Up @@ -89,12 +96,7 @@ func ListEmbedded() map[string]*Template {
{"which", "python3"},
{"python3", "-m", "venv", "--help"},
},
Files: map[string]string{
"app.py": pythonScript,
"Makefile": pythonMake,
"requirements.txt": "requests",
".cgiignore": "venv",
},
Files: mustEmbed("assets/python"),
Manifest: types.Manifest{
Name: "Example Python Function",
Description: `### Usage
Expand Down Expand Up @@ -288,3 +290,31 @@ build:
mkdir -p bin
mv -f lambda bin/
`

func mustEmbed(root string) map[string]string {
sub, err := fs.Sub(assets, root)
if err != nil {
panic(err)
}
var out = make(map[string]string)
err = fs.WalkDir(sub, ".", func(asset string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
content, err := fs.ReadFile(sub, asset)
if err != nil {
return fmt.Errorf("read %q: %w", asset, err)
}
p := strings.Trim(asset, "/.")

out[p] = string(content)
return nil
})
if err != nil {
panic(err)
}
return out
}

0 comments on commit d549807

Please sign in to comment.