1
2
3
4
5
6
7
8
9
10
11
12
13 package couchdb
14
15 import (
16 "bytes"
17 "context"
18 "encoding/json"
19 "io"
20 "net/http"
21 "strings"
22
23 "github.com/go-kivik/kivik/v4/couchdb/chttp"
24 "github.com/go-kivik/kivik/v4/driver"
25 )
26
27
28
29 const couch1ConfigNode = ""
30
31 var _ driver.Configer = &client{}
32
33 func configURL(node string, parts ...string) string {
34 var components []string
35 if node == couch1ConfigNode {
36 components = append(make([]string, 0, len(parts)+1),
37 "_config")
38 } else {
39 components = append(make([]string, 0, len(parts)+3),
40 "_node", node, "_config",
41 )
42 }
43 components = append(components, parts...)
44 return "/" + strings.Join(components, "/")
45 }
46
47 func (c *client) Config(ctx context.Context, node string) (driver.Config, error) {
48 cf := driver.Config{}
49 err := c.Client.DoJSON(ctx, http.MethodGet, configURL(node), nil, &cf)
50 return cf, err
51 }
52
53 func (c *client) ConfigSection(ctx context.Context, node, section string) (driver.ConfigSection, error) {
54 sec := driver.ConfigSection{}
55 err := c.Client.DoJSON(ctx, http.MethodGet, configURL(node, section), nil, &sec)
56 return sec, err
57 }
58
59 func (c *client) ConfigValue(ctx context.Context, node, section, key string) (string, error) {
60 var value string
61 err := c.Client.DoJSON(ctx, http.MethodGet, configURL(node, section, key), nil, &value)
62 return value, err
63 }
64
65 func (c *client) SetConfigValue(ctx context.Context, node, section, key, value string) (string, error) {
66 body, _ := json.Marshal(value)
67 var old string
68 opts := &chttp.Options{
69 Body: io.NopCloser(bytes.NewReader(body)),
70 }
71 err := c.Client.DoJSON(ctx, http.MethodPut, configURL(node, section, key), opts, &old)
72 return old, err
73 }
74
75 func (c *client) DeleteConfigKey(ctx context.Context, node, section, key string) (string, error) {
76 var value string
77 err := c.Client.DoJSON(ctx, http.MethodDelete, configURL(node, section, key), nil, &value)
78 return value, err
79 }
80
View as plain text