Debian Codegraph @

runtimevar/etcdvar/example_test.go

raw
1 // Copyright 2018 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package etcdvar_test
16
17 import (
18 "context"
19 "log"
20
21 "go.etcd.io/etcd/client/v3"
22 "gocloud.dev/runtimevar"
23 "gocloud.dev/runtimevar/etcdvar"
24 )
25
26 func ExampleOpenVariable() {
27 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
28
29 // Connect to the etcd server.
30 client, err := clientv3.NewFromURL("http://your.etcd.server:9999")
31 if err != nil {
32 log.Fatal(err)
33 }
34
35 // Construct a *runtimevar.Variable that watches the variable.
36 v, err := etcdvar.OpenVariable(client, "cfg-variable-name", runtimevar.StringDecoder, nil)
37 if err != nil {
38 log.Fatal(err)
39 }
40 defer v.Close()
41 }
42
43 func Example_openVariableFromURL() {
44 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
45 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/etcdvar"
46 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
47 ctx := context.Background()
48
49 // runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
50 // The default opener connects to an etcd server based on the environment
51 // variable ETCD_SERVER_URL.
52
53 v, err := runtimevar.OpenVariable(ctx, "etcd://myvarname?decoder=string")
54 if err != nil {
55 log.Fatal(err)
56 }
57 defer v.Close()
58 }