summaryrefslogtreecommitdiff
path: root/internal/file/content.go
blob: 2bb787d225b6c281ba45a0150c64a85a1c3aa238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
}