...

Source file src/github.com/go-kivik/kivik/v4/couchdb/config.go

Documentation: github.com/go-kivik/kivik/v4/couchdb

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    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  // couch1ConfigNode can be passed to any of the Config-related methods as the
    28  // node name, to query the /_config endpoint in a CouchDB 1.x-compatible way.
    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), // nolint:gomnd
    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) // Strings never cause JSON marshaling errors
    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