-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derefer.go
98 lines (91 loc) · 2.41 KB
/
derefer.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
package starenv
// DefaultDerefers is a mapping of default tags to derefer creator functions that
// use sensible default config.
// When using autoload package, these derefers are automatically loaded with
// default tags.
var DefaultDerefers = map[string]func() (Derefer, error){
"env": func() (Derefer, error) {
return DereferFunc(Env), nil
},
"b64": func() (Derefer, error) {
return DereferFunc(Base64), nil
},
"hex": func() (Derefer, error) {
return DereferFunc(Hex), nil
},
"ssm": func() (Derefer, error) {
return NewAWSParameterStore()
},
"pssm": func() (Derefer, error) {
d, err := NewAWSParameterStore()
if err != nil {
return nil, err
}
d.Plaintext = true
return d, nil
},
"s3": func() (Derefer, error) {
return NewS3()
},
"file": func() (Derefer, error) {
return &File{ExpandHome: true}, nil
},
"gpg": func() (Derefer, error) {
return DereferFunc(GPG), nil
},
"keyring": func() (Derefer, error) {
return DereferFunc(Keyring), nil
},
"https": func() (Derefer, error) {
return &HTTP{}, nil
},
"http": func() (Derefer, error) {
return &HTTP{DefaultInsecure: true}, nil
},
"tmpfile": func() (Derefer, error) {
return DereferFunc(TempFile), nil
},
"gz": func() (Derefer, error) {
return DereferFunc(Gzip), nil
},
"bz2": func() (Derefer, error) {
return DereferFunc(Bzip2), nil
},
"flate": func() (Derefer, error) {
return DereferFunc(Flate), nil
},
}
// Derefer is an interface that wraps Deref method.
//
// Deref method is called with the recursively derefed value of all subsequent
// derefers in the pipeline. ref is therefore a literal by the time it's passed
// to this method.
type Derefer interface {
Deref(ref string) (value string, err error)
}
// DereferFunc type is an adapter to allow use of ordinary functions as derefers.
type DereferFunc func(ref string) (string, error)
// Deref calls f(ref).
func (d DereferFunc) Deref(ref string) (string, error) {
return d(ref)
}
func literalDerefer(ref string) (string, error) {
return ref, nil
}
// LazyDerefer is a derefer that encapsulates a derefer creator function and delays
// its call until the first deref call.
type LazyDerefer struct {
New func() (Derefer, error)
d Derefer
}
// Deref calls the underlying derefer's Deref method.
func (l *LazyDerefer) Deref(ref string) (string, error) {
if l.d == nil {
var err error
l.d, err = l.New()
if err != nil {
return "", err
}
}
return l.d.Deref(ref)
}