summaryrefslogtreecommitdiff
path: root/replacer.go
blob: 6e6cb45e0eb259e57d96a8f475a6edcee8c322e8 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package caddy

import (
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

// Replacer can replace values in strings.
type Replacer interface {
	Set(variable, value string)
	Delete(variable string)
	Map(func() map[string]string)
	ReplaceAll(input, empty string) string
}

// NewReplacer returns a new Replacer.
func NewReplacer() Replacer {
	rep := &replacer{
		static: make(map[string]string),
	}
	rep.providers = []ReplacementsFunc{
		defaultReplacements,
		func() map[string]string { return rep.static },
	}
	return rep
}

type replacer struct {
	providers []ReplacementsFunc
	static    map[string]string
}

// Map augments the map of replacements with those returned
// by the given replacements function. The function is only
// executed at replace-time.
func (r *replacer) Map(replacements func() map[string]string) {
	r.providers = append(r.providers, replacements)
}

// Set sets a custom variable to a static value.
func (r *replacer) Set(variable, value string) {
	r.static[variable] = value
}

// Delete removes a variable with a static value
// that was created using Set.
func (r *replacer) Delete(variable string) {
	delete(r.static, variable)
}

// ReplaceAll replaces placeholders in input with their values.
// Values that are empty string will be substituted with the
// empty parameter.
func (r *replacer) ReplaceAll(input, empty string) string {
	if !strings.Contains(input, phOpen) {
		return input
	}
	for _, replacements := range r.providers {
		for key, val := range replacements() {
			if val == "" {
				val = empty
			}
			input = strings.ReplaceAll(input, phOpen+key+phClose, val)
		}
	}
	return input
}

// ReplacementsFunc is a function that returns replacements,
// which is variable names mapped to their values. The
// function will be evaluated only at replace-time to ensure
// the most current values are mapped.
type ReplacementsFunc func() map[string]string

var defaultReplacements = func() map[string]string {
	m := map[string]string{
		"system.hostname": func() string {
			// OK if there is an error; just return empty string
			name, _ := os.Hostname()
			return name
		}(),
		"system.slash": string(filepath.Separator),
		"system.os":    runtime.GOOS,
		"system.arch":  runtime.GOARCH,
	}

	// add environment variables
	for _, keyval := range os.Environ() {
		parts := strings.SplitN(keyval, "=", 2)
		if len(parts) != 2 {
			continue
		}
		m["env."+strings.ToUpper(parts[0])] = parts[1]
	}

	return m
}

// ReplacerCtxKey is the context key for a replacer.
const ReplacerCtxKey CtxKey = "replacer"

const phOpen, phClose = "{", "}"