Home

tmpl @07ccb515849ca1a50462bf2e5059af23c14a516a - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / config / config.go
- raw
 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
package config

import (
	"fmt"
	"io"
	"os"
	"strings"

	"gopkg.in/yaml.v3"
)

// Config is a tmpl config
type Config struct {
	Prompts []Prompt `yaml:"prompts"`
}

// Prompt is a tmpl prompt
type Prompt struct {
	ID      string `yaml:"id"`
	Label   string `yaml:"label"`
	Help    string `yaml:"help"`
	Default any    `yaml:"default"`
}

// Load loads a tmpl config
func Load(r io.Reader) (*Config, error) {
	configBytes, err := io.ReadAll(r)
	if err != nil {
		return nil, err
	}

	configContents := os.Expand(string(configBytes), func(s string) string {
		if strings.HasPrefix(s, "TMPL_PROMPT") {
			return fmt.Sprintf("${%s}", s)
		}
		return os.Getenv(s)
	})

	var c Config
	return &c, yaml.Unmarshal([]byte(configContents), &c)
}