forked from vmware-archive/atc
/
string.go
48 lines (38 loc) · 975 Bytes
/
string.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
package creds
import (
"github.com/concourse/atc"
"github.com/mitchellh/mapstructure"
)
type String struct {
variablesResolver Variables
rawCredString string
}
func NewString(variables Variables, credString string) String {
return String{
variablesResolver: variables,
rawCredString: credString,
}
}
func (s String) Evaluate() (string, error) {
var untypedInput interface{}
err := evaluate(s.variablesResolver, s.rawCredString, &untypedInput)
if err != nil {
return s.rawCredString, err
}
var metadata mapstructure.Metadata
var credString string
msConfig := &mapstructure.DecoderConfig{
Metadata: &metadata,
Result: &credString,
WeaklyTypedInput: true,
DecodeHook: atc.SanitizeDecodeHook,
}
decoder, err := mapstructure.NewDecoder(msConfig)
if err != nil {
return s.rawCredString, err
}
if err := decoder.Decode(untypedInput); err != nil {
return s.rawCredString, err
}
return credString, nil
}