-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
112 lines (78 loc) · 2.23 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
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
package runtimevar
import (
"context"
"fmt"
"net/url"
_ "github.com/aaronland/gocloud-blob/s3"
_ "gocloud.dev/blob/fileblob"
_ "gocloud.dev/blob/memblob"
_ "gocloud.dev/blob/s3blob"
_ "gocloud.dev/runtimevar/constantvar"
_ "gocloud.dev/runtimevar/filevar"
"github.com/aaronland/go-aws-auth"
"github.com/aaronland/gocloud-blob/bucket"
gc "gocloud.dev/runtimevar"
"gocloud.dev/runtimevar/awsparamstore"
"gocloud.dev/runtimevar/blobvar"
)
// 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)
}
case "blobvar":
if !q.Has("bucket-uri") {
return "", fmt.Errorf("Missing ?bucket-uri parameter")
}
b_uri, err := url.QueryUnescape(q.Get("bucket-uri"))
if err != nil {
return "", fmt.Errorf("Failed to unescape bucket URI, %w", err)
}
b, err := bucket.OpenBucket(ctx, b_uri)
if err != nil {
return "", fmt.Errorf("Failed to open bucket, %w", err)
}
defer b.Close()
v, v_err = blobvar.OpenVariable(b, 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
}