-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterpolate.go
233 lines (193 loc) · 5.06 KB
/
interpolate.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package interpolate
import (
"fmt"
"strings"
)
// Interpolate takes a set of environment and interpolates it into the provided string using shell script expansions
func Interpolate(env Env, str string) (string, error) {
if env == nil {
env = NewSliceEnv(nil)
}
expr, err := NewParser(str).Parse()
if err != nil {
return "", err
}
return expr.Expand(env)
}
// Indentifiers parses the identifiers from any expansions in the provided string
func Identifiers(str string) ([]string, error) {
expr, err := NewParser(str).Parse()
if err != nil {
return nil, err
}
return expr.Identifiers(), nil
}
// An expansion is something that takes in ENV and returns a string or an error
type Expansion interface {
// Expand expands the expansion using variables from env.
Expand(env Env) (string, error)
// Identifiers returns any variable names referenced within the expansion.
// Escaped expansions do something special and return identifiers
// (starting with $) that *would* become referenced after a round of
// unescaping.
Identifiers() []string
}
// VariableExpansion represents either $VAR or ${VAR}, our simplest expansion
type VariableExpansion struct {
Identifier string
}
func (e VariableExpansion) Identifiers() []string {
return []string{e.Identifier}
}
func (e VariableExpansion) Expand(env Env) (string, error) {
val, _ := env.Get(e.Identifier)
return val, nil
}
// EmptyValueExpansion returns either the value of an env, or a default value if it's unset or null
type EmptyValueExpansion struct {
Identifier string
Content Expression
}
func (e EmptyValueExpansion) Identifiers() []string {
return append([]string{e.Identifier}, e.Content.Identifiers()...)
}
func (e EmptyValueExpansion) Expand(env Env) (string, error) {
val, _ := env.Get(e.Identifier)
if val == "" {
return e.Content.Expand(env)
}
return val, nil
}
// UnsetValueExpansion returns either the value of an env, or a default value if it's unset
type UnsetValueExpansion struct {
Identifier string
Content Expression
}
func (e UnsetValueExpansion) Identifiers() []string {
return []string{e.Identifier}
}
func (e UnsetValueExpansion) Expand(env Env) (string, error) {
val, ok := env.Get(e.Identifier)
if !ok {
return e.Content.Expand(env)
}
return val, nil
}
// EscapedExpansion is an expansion that is delayed until later on (usually by a later process)
type EscapedExpansion struct {
// PotentialIdentifier is an identifier for the purpose of Identifiers,
// but not for the purpose of Expand.
PotentialIdentifier string
}
func (e EscapedExpansion) Identifiers() []string {
return []string{"$" + e.PotentialIdentifier}
}
func (e EscapedExpansion) Expand(Env) (string, error) {
return "$", nil
}
// SubstringExpansion returns a substring (or slice) of the env
type SubstringExpansion struct {
Identifier string
Offset int
Length int
HasLength bool
}
func (e SubstringExpansion) Identifiers() []string {
return []string{e.Identifier}
}
func (e SubstringExpansion) Expand(env Env) (string, error) {
val, _ := env.Get(e.Identifier)
from := e.Offset
// Negative offsets = from end
if from < 0 {
from += len(val)
}
// Still negative = too far from end? Truncate to start.
if from < 0 {
from = 0
}
// Beyond end? Truncate to end.
if from > len(val) {
from = len(val)
}
if !e.HasLength {
return val[from:], nil
}
to := e.Length
if to >= 0 {
// Positive length = from offset
to += from
} else {
// Negative length = from end
to += len(val)
// Too far? Truncate to offset.
if to < from {
to = from
}
}
// Beyond end? Truncate to end.
if to > len(val) {
to = len(val)
}
return val[from:to], nil
}
// RequiredExpansion returns an env value, or an error if it is unset
type RequiredExpansion struct {
Identifier string
Message Expression
}
func (e RequiredExpansion) Identifiers() []string {
return []string{e.Identifier}
}
func (e RequiredExpansion) Expand(env Env) (string, error) {
val, ok := env.Get(e.Identifier)
if !ok {
msg, err := e.Message.Expand(env)
if err != nil {
return "", err
}
if msg == "" {
msg = "not set"
}
return "", fmt.Errorf("$%s: %s", e.Identifier, msg)
}
return val, nil
}
// Expression is a collection of either Text or Expansions
type Expression []ExpressionItem
func (e Expression) Identifiers() []string {
identifiers := []string{}
for _, item := range e {
if item.Expansion != nil {
identifiers = append(identifiers, item.Expansion.Identifiers()...)
}
}
return identifiers
}
func (e Expression) Expand(env Env) (string, error) {
var buf strings.Builder
for _, item := range e {
if item.Expansion != nil {
result, err := item.Expansion.Expand(env)
if err != nil {
return "", err
}
buf.WriteString(result)
} else {
buf.WriteString(item.Text)
}
}
return buf.String(), nil
}
// ExpressionItem models either an Expansion or Text. Either/Or, never both.
type ExpressionItem struct {
Text string
// -- or --
Expansion Expansion
}
func (i ExpressionItem) String() string {
if i.Expansion != nil {
return fmt.Sprintf("%#v", i.Expansion)
}
return fmt.Sprintf("%q", i.Text)
}