summaryrefslogtreecommitdiff
path: root/internal/file/content.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-07-04 21:34:30 +0300
committerPaul Buetow <paul@buetow.org>2026-07-04 21:34:30 +0300
commit6ff28396bd501cdd3af2c6b9a8dfdb4e60618e4f (patch)
tree77801f9a29be1d7b4ff2b8283e56a2f4976c302d /internal/file/content.go
parent31c7a348184872a05bb5c130c09b91c44fc0e52d (diff)
support files and templates
Diffstat (limited to 'internal/file/content.go')
-rw-r--r--internal/file/content.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/internal/file/content.go b/internal/file/content.go
new file mode 100644
index 0000000..2bb787d
--- /dev/null
+++ b/internal/file/content.go
@@ -0,0 +1,52 @@
+package file
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "strings"
+ "text/template"
+)
+
+func resolveContent(param, targetPath string) ([]byte, error) {
+ var content []byte
+ var err error
+
+ if strings.HasPrefix(param, "source://") {
+ sourcePath := strings.TrimPrefix(param, "source://")
+ content, err = os.ReadFile(sourcePath)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read source file %s: %w", sourcePath, err)
+ }
+ } else {
+ content = []byte(param)
+ }
+
+ if strings.HasSuffix(targetPath, ".tmpl") || (strings.HasPrefix(param, "source://") && strings.HasSuffix(strings.TrimPrefix(param, "source://"), ".tmpl")) {
+ return applyTemplate(content, param)
+ }
+
+ return content, nil
+}
+
+func applyTemplate(content []byte, param string) ([]byte, error) {
+ data := make(map[string]string)
+ for _, env := range os.Environ() {
+ pair := strings.SplitN(env, "=", 2)
+ if len(pair) == 2 {
+ data[pair[0]] = pair[1]
+ }
+ }
+ data["Param"] = param
+
+ tmpl, err := template.New("resource").Parse(string(content))
+ if err != nil {
+ return nil, fmt.Errorf("template parse error: %w", err)
+ }
+
+ var buf bytes.Buffer
+ if err := tmpl.Execute(&buf, data); err != nil {
+ return nil, fmt.Errorf("template execute error: %w", err)
+ }
+ return buf.Bytes(), nil
+}