-
Notifications
You must be signed in to change notification settings - Fork 53
/
ops.go
401 lines (363 loc) · 10.4 KB
/
ops.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package envars
import (
"encoding/json"
"fmt"
"hash/fnv"
"reflect"
"sort"
"strings"
"github.com/kballard/go-shellquote"
"github.com/cashapp/hermit/errors"
)
// Envars is a convenience alias
type Envars map[string]string
// Clone envars.
func (e Envars) Clone() Envars {
out := make(Envars, len(e))
for k, v := range e {
out[k] = v
}
return out
}
// System renders the Envars in the format expected by the system, ie. KEY=VALUE
func (e Envars) System() []string {
out := make([]string, 0, len(e))
for k, v := range e {
out = append(out, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(out)
return out
}
// Apply ops to these Envars and return the resulting Transform.
//
// Envars are not modified.
func (e Envars) Apply(envRoot string, ops Ops) *Transform {
transform := transform(envRoot, e)
for _, op := range ops {
op.Apply(transform)
}
return transform
}
// Revert creates a Transform that reverts the application of the provided Ops.
//
// Envars are not modified.
func (e Envars) Revert(envRoot string, ops Ops) *Transform {
transform := transform(envRoot, e)
for i := len(ops) - 1; i >= 0; i-- {
ops[i].Revert(transform)
}
return transform
}
// go:sumtype decl Op
// Op is an operation on an environment variable.
type Op interface {
fmt.Stringer
Envar() string
// Apply changes to transform.
//
// This may also add/remove extra housekeeping variables to support Revert.
Apply(transform *Transform)
// Revert the changes made by Apply.
Revert(transform *Transform)
sealed()
}
// Ops to apply to a set of environment variables.
type Ops []Op
// {<type>: <object>} - see marshalKeys for the key types
type encodedOp map[string]json.RawMessage
// MarshalOps to JSON.
func MarshalOps(ops Ops) ([]byte, error) {
encoded := make([]encodedOp, 0, len(ops))
for _, op := range ops {
key, ok := marshalKeys[reflect.TypeOf(op)]
if !ok {
panic(fmt.Sprintf("unsupported op type %T", op))
}
jop, err := json.Marshal(op)
if err != nil {
return nil, errors.WithStack(err)
}
encoded = append(encoded, encodedOp{key: jop})
}
return json.Marshal(encoded)
}
// UnmarshalOps from JSON.
func UnmarshalOps(data []byte) (Ops, error) {
var encoded []encodedOp
err := json.Unmarshal(data, &encoded)
if err != nil {
return nil, errors.WithStack(err)
}
ops := make(Ops, 0, len(encoded))
for _, enc := range encoded {
var key string
var encodedOp json.RawMessage
for key, encodedOp = range enc {
}
typ, ok := unmarshalKeys[key]
if !ok {
return nil, errors.Errorf("unsupported envar op key %q", key)
}
op := reflect.New(typ).Interface().(Op)
if err = json.Unmarshal(encodedOp, op); err != nil {
return nil, errors.WithStack(err)
}
ops = append(ops, op)
}
return ops, nil
}
// Infer uses simple heuristics to build a sequence of transformations for environment variables.
//
// Currently this consists of detecting prepend/append to :-separated lists, set and unset.
func Infer(env []string) Ops {
ops := make(Ops, 0, len(env))
for _, envar := range env {
parts := strings.SplitN(envar, "=", 2)
key := parts[0]
value := parts[1]
var op Op
switch {
case strings.HasPrefix(value, "${"+key+"}:") || strings.HasPrefix(value, "$"+key+":"): // Append
insertion := value[strings.Index(value, ":")+1:]
op = &Append{
Name: key,
Value: insertion,
}
case strings.HasSuffix(value, ":${"+key+"}") || strings.HasSuffix(value, ":$"+key): // Prepend
insertion := value[:strings.LastIndex(value, ":")]
op = &Prepend{
Name: key,
Value: insertion,
}
case value == "":
op = &Unset{Name: key}
default:
op = &Set{Name: key, Value: value}
}
ops = append(ops, op)
}
return ops
}
// These tables need to be kept in sync.
var (
marshalKeys = map[reflect.Type]string{
reflect.TypeOf(&Append{}): "a",
reflect.TypeOf(&Prepend{}): "p",
reflect.TypeOf(&Set{}): "s",
reflect.TypeOf(&Unset{}): "u",
reflect.TypeOf(&Force{}): "f",
reflect.TypeOf(&Prefix{}): "P",
}
unmarshalKeys = func() map[string]reflect.Type {
out := make(map[string]reflect.Type, len(marshalKeys))
for t, k := range marshalKeys {
out[k] = t.Elem() // deref all the pointers so reflect.New gives the correct type
}
return out
}()
_ Op = &Append{}
_ Op = &Prepend{}
_ Op = &Set{}
_ Op = &Unset{}
_ Op = &Force{}
_ Op = &Prefix{}
)
// Append ensures an element exists at the end of a colon separated list.
type Append struct {
Name string ` json:"n"`
Value string ` json:"v"`
}
func (e *Append) sealed() {}
func (e *Append) String() string {
return fmt.Sprintf(`%s="${%s}:%s"`, e.Name, e.Name, shellquote.Join(e.Value))
}
func (e *Append) Envar() string { return e.Name } // nolint: golint
func (e *Append) Apply(transform *Transform) { // nolint: golint
value, _ := transform.get(e.Name)
out := splitAndDrop(value, e.Value)
out = append(out, e.Value)
transform.set(e.Name, strings.Join(out, ":"))
return
}
func (e *Append) Revert(transform *Transform) { // nolint: golint
value, _ := transform.get(e.Name)
out := splitAndDrop(value, e.Value)
transform.set(e.Name, strings.Join(out, ":"))
return
}
// Prepend ensures an element exists at the beginning of a colon separated list.
type Prepend struct {
Name string ` json:"n"`
Value string ` json:"v"`
}
func (e *Prepend) sealed() {}
func (e *Prepend) String() string {
return fmt.Sprintf(`%s=%s:${%s}`, e.Name, shellquote.Join(e.Value), e.Name)
}
func (e *Prepend) Envar() string { return e.Name } // nolint: golint
func (e *Prepend) Apply(transform *Transform) { // nolint: golint
value, _ := transform.get(e.Name)
prepend := transform.expand(e.Value)
out := splitAndDrop(value, prepend)
out = append([]string{prepend}, out...)
transform.set(e.Name, strings.Join(out, ":"))
return
}
func (e *Prepend) Revert(transform *Transform) { // nolint: golint
value, _ := transform.get(e.Name)
prepend := transform.expand(e.Value)
out := splitAndDrop(value, prepend)
transform.set(e.Name, strings.Join(out, ":"))
return
}
// Prefix ensures the environment variable has the given prefix.
type Prefix struct {
Name string ` json:"n"`
Prefix string ` json:"p"`
}
func (p *Prefix) sealed() {}
func (p *Prefix) String() string {
return fmt.Sprintf(`%s=%s${%s}`, p.Name, shellquote.Join(p.Prefix), p.Name)
}
func (p *Prefix) Envar() string { return p.Name } // nolint: golint
func (p *Prefix) Apply(transform *Transform) { // nolint: golint
prefix := transform.expand(p.Prefix)
if value, ok := transform.get(p.Name); ok && !strings.HasPrefix(value, prefix) {
transform.set(p.Name, prefix+value)
}
return
}
func (p *Prefix) Revert(transform *Transform) { // nolint: golint
prefix := transform.expand(p.Prefix)
if value, ok := transform.get(p.Name); ok && strings.HasPrefix(value, prefix) {
transform.set(p.Name, strings.TrimPrefix(value, prefix))
}
return
}
// Set an environment variable.
type Set struct {
Name string ` json:"n"`
Value string ` json:"v"`
}
func (e *Set) sealed() {}
func (e *Set) String() string { return fmt.Sprintf(`%s="%s"`, e.Name, shellquote.Join(e.Value)) }
func (e *Set) Envar() string { return e.Name } // nolint: golint
func (e *Set) Apply(transform *Transform) { // nolint: golint
if value, ok := transform.get(e.Name); ok {
old := makeRevertKey(transform, e)
if _, keep := transform.get(old); !keep {
transform.set(old, value)
}
}
transform.set(e.Name, e.Value)
return
}
func (e *Set) Revert(transform *Transform) { // nolint: golint
old := makeRevertKey(transform, e)
// Check if the user has changed the value and if so, do nothing.
if currentValue, ok := transform.get(e.Name); ok && currentValue != transform.expand(e.Value) {
transform.unset(old)
return
}
transform.unset(e.Name)
if value, ok := transform.get(old); ok {
transform.set(e.Name, value)
transform.unset(old)
}
return
}
// Unset an environment variable.
type Unset struct {
Name string ` json:"n"`
}
func (e *Unset) sealed() {}
func (e *Unset) String() string { return fmt.Sprintf("unset %s", e.Name) }
func (e *Unset) Envar() string { return e.Name } // nolint: golint
func (e *Unset) Apply(transform *Transform) { // nolint: golint
if value, ok := transform.get(e.Name); ok {
old := makeRevertKey(transform, e)
transform.set(old, value)
}
transform.unset(e.Name)
return
}
func (e *Unset) Revert(transform *Transform) { // nolint: golint
old := makeRevertKey(transform, e) // nolint: ifshort
// If user has subsequently set the environment variable, do nothing.
if value, ok := transform.get(e.Name); ok && value != "" {
transform.unset(old)
return
}
transform.unset(e.Name)
if value, ok := transform.get(old); ok {
transform.set(e.Name, value)
transform.unset(old)
}
return
}
// Force set/unset an environment variable without preserving or restoring its previous value.
type Force struct {
Name string ` json:"n"`
Value string ` json:"v"`
}
func (f *Force) sealed() {}
func (f *Force) String() string {
return fmt.Sprintf(`%s="%s"`, f.Name, shellquote.Join(f.Value))
}
func (f *Force) Envar() string { return f.Name } // nolint: golint
func (f *Force) Apply(transform *Transform) { // nolint: golint
transform.set(f.Name, f.Value)
return
}
func (f *Force) Revert(transform *Transform) { // nolint: golint
transform.unset(f.Name)
return
}
// Split "envar" by ":" and drop "value" from it.
func splitAndDrop(envar string, value string) []string {
parts := strings.Split(envar, ":")
values := strings.Split(value, ":")
out := make([]string, 0, len(parts))
skip:
for _, elem := range parts {
for _, valel := range values {
if elem == valel {
continue skip
}
}
out = append(out, elem)
}
return out
}
var zero = []byte{0}
// Creates a unique and deterministic key for storing a revert, from an Op.
// nolint: errcheck
func makeRevertKey(transform *Transform, op Op) string {
hash := fnv.New64a()
hash.Write([]byte(transform.envRoot))
hash.Write(zero)
v := reflect.Indirect(reflect.ValueOf(op))
t := v.Type()
hash.Write([]byte(t.Name()))
hash.Write(zero)
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
if ft.Type.Kind() != reflect.String {
panic("field " + ft.Name + " must be a string")
}
value := v.Field(i).String()
hash.Write([]byte(ft.Name))
hash.Write(zero)
hash.Write([]byte(value))
hash.Write(zero)
}
return fmt.Sprintf("_HERMIT_OLD_%s_%X", op.Envar(), hash.Sum(nil))
}
// transform returns a Transform with e as the base.
func transform(envRoot string, e Envars) *Transform {
return &Transform{
envRoot: envRoot,
seed: e,
dest: Envars{},
}
}