forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
67 lines (57 loc) · 2.02 KB
/
config.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
package couchdb
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/go-kivik/couchdb/chttp"
"github.com/go-kivik/kivik/driver"
)
// Couch1ConfigNode can be passed to any of the Config-related methods as the
// node name, to query the /_config endpoint in a CouchDB 1.x-compatible way.
const Couch1ConfigNode = "<Couch1Config>"
var _ driver.Configer = &client{}
func configURL(node string, parts ...string) string {
var components []string
if node == Couch1ConfigNode {
components = append(make([]string, 0, len(parts)+1),
"_config")
} else {
components = append(make([]string, 0, len(parts)+3),
"_node", node, "_config",
)
}
components = append(components, parts...)
return "/" + strings.Join(components, "/")
}
func (c *client) Config(ctx context.Context, node string) (driver.Config, error) {
cf := driver.Config{}
_, err := c.Client.DoJSON(ctx, http.MethodGet, configURL(node), nil, &cf)
return cf, err
}
func (c *client) ConfigSection(ctx context.Context, node, section string) (driver.ConfigSection, error) {
sec := driver.ConfigSection{}
_, err := c.Client.DoJSON(ctx, http.MethodGet, configURL(node, section), nil, &sec)
return sec, err
}
func (c *client) ConfigValue(ctx context.Context, node, section, key string) (string, error) {
var value string
_, err := c.Client.DoJSON(ctx, http.MethodGet, configURL(node, section, key), nil, &value)
return value, err
}
func (c *client) SetConfigValue(ctx context.Context, node, section, key, value string) (string, error) {
body, _ := json.Marshal(value) // Strings never cause JSON marshaling errors
var old string
opts := &chttp.Options{
Body: ioutil.NopCloser(bytes.NewReader(body)),
}
_, err := c.Client.DoJSON(ctx, http.MethodPut, configURL(node, section, key), opts, &old)
return old, err
}
func (c *client) DeleteConfigKey(ctx context.Context, node, section, key string) (string, error) {
var value string
_, err := c.Client.DoJSON(ctx, http.MethodDelete, configURL(node, section, key), nil, &value)
return value, err
}