-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
83 lines (58 loc) · 1.57 KB
/
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
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
package runtimevar
import (
"context"
"fmt"
"net/url"
"github.com/aaronland/go-aws-auth"
gc "gocloud.dev/runtimevar"
"gocloud.dev/runtimevar/awsparamstore"
_ "gocloud.dev/runtimevar/constantvar"
_ "gocloud.dev/runtimevar/filevar"
)
// StringVar returns the latest string value contained by 'uri', which is expected
// to be a valid `gocloud.dev/runtimevar` URI.
func StringVar(ctx context.Context, uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", fmt.Errorf("Failed to parse URI, %w", err)
}
if u.Scheme == "" {
return u.Path, nil
}
q := u.Query()
if q.Get("decoder") == "" {
q.Set("decoder", "string")
u.RawQuery = q.Encode()
}
var v *gc.Variable
var v_err error
switch u.Scheme {
case "awsparamstore":
// https://gocloud.dev/howto/runtimevar/#awsps-ctor
creds := q.Get("credentials")
region := q.Get("region")
if creds != "" {
aws_uri := fmt.Sprintf("aws://%s?credentials=%s", region, creds)
aws_auth, err := auth.NewSSMClient(ctx, aws_uri)
if err != nil {
return "", fmt.Errorf("Failed to create AWS session credentials, %w", err)
}
v, v_err = awsparamstore.OpenVariableV2(aws_auth, u.Host, gc.StringDecoder, nil)
}
default:
// pass
}
if v == nil {
uri = u.String()
v, v_err = gc.OpenVariable(ctx, uri)
}
if v_err != nil {
return "", fmt.Errorf("Failed to open variable, %w", v_err)
}
defer v.Close()
snapshot, err := v.Latest(ctx)
if err != nil {
return "", fmt.Errorf("Failed to derive latest snapshot for variable, %w", err)
}
return snapshot.Value.(string), nil
}